internal/graphicscommand: remove the dependency on a graphics driver from compileShader

Updates #2035
This commit is contained in:
Hajime Hoshi 2022-04-04 01:55:51 +09:00
parent e05cfdb00a
commit 02db3bad53
6 changed files with 32 additions and 23 deletions

View File

@ -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 (

View File

@ -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
}

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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