mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
internal/graphicscommand: remove the dependency on a graphics driver from compileShader
Updates #2035
This commit is contained in:
parent
e05cfdb00a
commit
02db3bad53
@ -29,7 +29,8 @@ const (
|
||||
1 + // the texture destination region's size
|
||||
1 + // the offsets array of the second and the following images
|
||||
1 + // the texture source region's origin
|
||||
1 // the texture source region's size
|
||||
1 + // the texture source region's size
|
||||
1 // the projection matrix
|
||||
|
||||
TextureDestinationSizeUniformVariableIndex = 0
|
||||
TextureSourceSizesUniformVariableIndex = 1
|
||||
@ -38,6 +39,7 @@ const (
|
||||
TextureSourceOffsetsUniformVariableIndex = 4
|
||||
TextureSourceRegionOriginUniformVariableIndex = 5
|
||||
TextureSourceRegionSizeUniformVariableIndex = 6
|
||||
ProjectionMatrixUniformVariableIndex = 7
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -666,7 +666,7 @@ func (c *newShaderCommand) String() string {
|
||||
|
||||
// Exec executes a newShaderCommand.
|
||||
func (c *newShaderCommand) Exec(graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
|
||||
ir, err := compileShader(graphicsDriver, c.src)
|
||||
ir, err := compileShader(c.src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -106,33 +106,16 @@ func imageSrc%[1]dAt(pos vec2) vec4 {
|
||||
}
|
||||
}
|
||||
|
||||
func compileShader(graphicsDriver graphicsdriver.Graphics, src []byte) (*shaderir.Program, error) {
|
||||
func compileShader(src []byte) (*shaderir.Program, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.Write(src)
|
||||
buf.WriteString(shaderSuffix)
|
||||
if graphicsDriver.FramebufferYDirection() != graphicsDriver.NDCYDirection() {
|
||||
buf.WriteString(`
|
||||
buf.WriteString(`var __projectionMatrix mat4
|
||||
|
||||
func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, vec4) {
|
||||
return mat4(
|
||||
2/__imageDstTextureSize.x, 0, 0, 0,
|
||||
0, -2/__imageDstTextureSize.y, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
-1, 1, 0, 1,
|
||||
) * vec4(position, 0, 1), texCoord, color
|
||||
return __projectionMatrix * vec4(position, 0, 1), texCoord, color
|
||||
}
|
||||
`)
|
||||
} else {
|
||||
buf.WriteString(`
|
||||
func __vertex(position vec2, texCoord vec2, color vec4) (vec4, vec2, vec4) {
|
||||
return mat4(
|
||||
2/__imageDstTextureSize.x, 0, 0, 0,
|
||||
0, 2/__imageDstTextureSize.y, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
-1, -1, 0, 1,
|
||||
) * vec4(position, 0, 1), texCoord, color
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
fs := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fs, "", buf.Bytes(), parser.AllErrors)
|
||||
|
@ -1056,6 +1056,12 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcs [graphics.Sh
|
||||
us[graphics.TextureSourceRegionOriginUniformVariableIndex] = usorigin
|
||||
ussize := []float32{float32(srcRegion.Width), float32(srcRegion.Height)}
|
||||
us[graphics.TextureSourceRegionSizeUniformVariableIndex] = ussize
|
||||
us[graphics.ProjectionMatrixUniformVariableIndex] = []float32{
|
||||
2 / float32(dw), 0, 0, 0,
|
||||
0, -2 / float32(dh), 0, 0,
|
||||
0, 0, 1, 0,
|
||||
-1, 1, 0, 1,
|
||||
}
|
||||
|
||||
for i, u := range uniforms {
|
||||
us[graphics.PreservedUniformVariablesNum+i] = u
|
||||
|
@ -995,6 +995,13 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.
|
||||
ussize := []float32{float32(srcRegion.Width), float32(srcRegion.Height)}
|
||||
uniformVars[graphics.TextureSourceRegionSizeUniformVariableIndex] = ussize
|
||||
|
||||
uniformVars[graphics.ProjectionMatrixUniformVariableIndex] = []float32{
|
||||
2 / float32(dw), 0, 0, 0,
|
||||
0, -2 / float32(dh), 0, 0,
|
||||
0, 0, 1, 0,
|
||||
-1, 1, 0, 1,
|
||||
}
|
||||
|
||||
// Set the additional uniform variables.
|
||||
for i, v := range uniforms {
|
||||
const offset = graphics.PreservedUniformVariablesNum
|
||||
|
@ -320,6 +320,17 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.
|
||||
g.uniformVars[idx].value = size
|
||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||
}
|
||||
{
|
||||
const idx = graphics.ProjectionMatrixUniformVariableIndex
|
||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||
g.uniformVars[idx].value = []float32{
|
||||
2 / float32(dw), 0, 0, 0,
|
||||
0, 2 / float32(dh), 0, 0,
|
||||
0, 0, 1, 0,
|
||||
-1, -1, 0, 1,
|
||||
}
|
||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||
}
|
||||
|
||||
for i, v := range uniforms {
|
||||
const offset = graphics.PreservedUniformVariablesNum
|
||||
|
Loading…
Reference in New Issue
Block a user