shader: Add a predefined uniform variable: Internal_ViewportSize

This commit is contained in:
Hajime Hoshi 2020-06-05 03:00:43 +09:00
parent 3dbf4c0a83
commit 6f411842f0
4 changed files with 27 additions and 4 deletions

View File

@ -29,17 +29,20 @@ const (
const shaderSrc = `package main
// Internal_ViewportSize is a predefined uniform variable.
// TODO: Hide this and create a function for the projection matrix?
func Vertex(position vec2, texCoord vec2, color vec4) vec4 {
return mat4(
2.0/640, 0, 0, 0,
0, 2.0/480, 0, 0,
2.0/Internal_ViewportSize.x, 0, 0, 0,
0, 2.0/Internal_ViewportSize.y, 0, 0,
0, 0, 1, 0,
-1, -1, 0, 1,
) * vec4(position, 0, 1)
}
func Fragment(position vec4) vec4 {
return vec4(position.x/640, position.y/480, 0, 1)
return vec4(position.x/Internal_ViewportSize.x, position.y/Internal_ViewportSize.y, 0, 1)
}`
type Game struct {

View File

@ -384,6 +384,10 @@ func (i *Image) DrawTrianglesWithShader(vertices []Vertex, indices []uint16, sha
}
}
// The last uniform variable is Internal_ViewportSize.
// The actual value is set at graphicscommand package.
us = append(us, []float32{0, 0})
var bx0, by0, bx1, by1 float32
if firstImage != nil {
b := firstImage.Bounds()

View File

@ -453,6 +453,13 @@ func (c *drawTrianglesCommand) Exec(indexOffset int) error {
us[i] = v
}
}
// The last uniform variables are added at /shader.go and represents a viewport size.
w, h := c.dst.InternalSize()
viewport := us[len(us)-1].([]float32)
viewport[0] = float32(w)
viewport[1] = float32(h)
return theGraphicsDriver.DrawShader(c.dst.image.ID(), c.shader.shader.ID(), c.nindices, indexOffset, c.mode, us)
}
return theGraphicsDriver.Draw(c.dst.image.ID(), c.src.image.ID(), c.nindices, indexOffset, c.mode, c.color, c.filter, c.address)

View File

@ -15,16 +15,25 @@
package ebiten
import (
"bytes"
"github.com/hajimehoshi/ebiten/internal/buffered"
"github.com/hajimehoshi/ebiten/internal/shader"
)
const shaderSuffix = `
var Internal_ViewportSize vec2`
type Shader struct {
shader *buffered.Shader
}
func NewShader(src []byte) (*Shader, error) {
s, err := shader.Compile(src, "Vertex", "Fragment")
var b bytes.Buffer
b.Write(src)
b.Write([]byte(shaderSuffix))
s, err := shader.Compile(b.Bytes(), "Vertex", "Fragment")
if err != nil {
return nil, err
}