Revert "internal/shaderir: refactoring: remove Program.TextureNum"

This reverts commit c2bea16776.

Reason: test compilation errors
This commit is contained in:
Hajime Hoshi 2022-12-24 15:53:16 +09:00
parent c2bea16776
commit 88e5d5e059
7 changed files with 19 additions and 22 deletions

View File

@ -124,7 +124,7 @@ func CompileShader(src []byte) (*shaderir.Program, error) {
vert = "__vertex"
frag = "Fragment"
)
ir, err := shader.Compile(fs, f, vert, frag)
ir, err := shader.Compile(fs, f, vert, frag, ShaderImageCount)
if err != nil {
return nil, err
}

View File

@ -14,12 +14,8 @@
package graphics
import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
const (
ShaderImageCount = shaderir.TextureCount
ShaderImageCount = 4
// PreservedUniformVariablesCount represents the number of preserved uniform variables.
// Any shaders in Ebitengine must have these uniform variables.

View File

@ -180,7 +180,7 @@ func (p *ParseError) Error() string {
return strings.Join(p.errs, "\n")
}
func Compile(fs *token.FileSet, f *ast.File, vertexEntry, fragmentEntry string) (*shaderir.Program, error) {
func Compile(fs *token.FileSet, f *ast.File, vertexEntry, fragmentEntry string, textureNum int) (*shaderir.Program, error) {
s := &compileState{
fs: fs,
vertexEntry: vertexEntry,
@ -198,6 +198,7 @@ func Compile(fs *token.FileSet, f *ast.File, vertexEntry, fragmentEntry string)
// TODO: Make a call graph and reorder the elements.
s.ir.TextureNum = textureNum
return &s.ir, nil
}

View File

@ -128,12 +128,12 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh
{
vslines = append(vslines, strings.Split(VertexPrelude(version), "\n")...)
vslines = append(vslines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 || len(p.Attributes) > 0 || len(p.Varyings) > 0 {
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Attributes) > 0 || len(p.Varyings) > 0 {
vslines = append(vslines, "")
for i, t := range p.Uniforms {
vslines = append(vslines, fmt.Sprintf("uniform %s;", c.varDecl(p, &t, fmt.Sprintf("U%d", i))))
}
for i := 0; i < shaderir.TextureCount; i++ {
for i := 0; i < p.TextureNum; i++ {
vslines = append(vslines, fmt.Sprintf("uniform sampler2D T%d;", i))
}
for i, t := range p.Attributes {
@ -225,12 +225,12 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh
{
fslines = append(fslines, strings.Split(FragmentPrelude(version), "\n")...)
fslines = append(fslines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 || len(p.Varyings) > 0 {
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Varyings) > 0 {
fslines = append(fslines, "")
for i, t := range p.Uniforms {
fslines = append(fslines, fmt.Sprintf("uniform %s;", c.varDecl(p, &t, fmt.Sprintf("U%d", i))))
}
for i := 0; i < shaderir.TextureCount; i++ {
for i := 0; i < p.TextureNum; i++ {
fslines = append(fslines, fmt.Sprintf("uniform sampler2D T%d;", i))
}
for i, t := range p.Varyings {

View File

@ -111,12 +111,13 @@ func Compile(p *shaderir.Program) (string, []int) {
}
lines = append(lines, "}")
}
lines = append(lines, "")
for i := 0; i < shaderir.TextureCount; i++ {
lines = append(lines, fmt.Sprintf("Texture2D T%[1]d : register(t%[1]d);", i))
if p.TextureNum > 0 {
lines = append(lines, "")
for i := 0; i < p.TextureNum; i++ {
lines = append(lines, fmt.Sprintf("Texture2D T%[1]d : register(t%[1]d);", i))
}
lines = append(lines, "SamplerState samp : register(s0);")
}
lines = append(lines, "SamplerState samp : register(s0);")
if len(p.Funcs) > 0 {
lines = append(lines, "")

View File

@ -109,7 +109,7 @@ func Compile(p *shaderir.Program, vertex, fragment string) (shader string) {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.varDecl(p, &u, fmt.Sprintf("U%d", i), true), i+1))
}
for i := 0; i < shaderir.TextureCount; i++ {
for i := 0; i < p.TextureNum; i++ {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\ttexture2d<float> T%[1]d [[texture(%[1]d)]]", i))
}
@ -131,7 +131,7 @@ func Compile(p *shaderir.Program, vertex, fragment string) (shader string) {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.varDecl(p, &u, fmt.Sprintf("U%d", i), true), i+1))
}
for i := 0; i < shaderir.TextureCount; i++ {
for i := 0; i < p.TextureNum; i++ {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\ttexture2d<float> T%[1]d [[texture(%[1]d)]]", i))
}
@ -220,7 +220,7 @@ func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototy
for i, u := range p.Uniforms {
args = append(args, "constant "+c.varDecl(p, &u, fmt.Sprintf("U%d", i), true))
}
for i := 0; i < shaderir.TextureCount; i++ {
for i := 0; i < p.TextureNum; i++ {
args = append(args, fmt.Sprintf("texture2d<float> T%d", i))
}
@ -388,7 +388,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
for i := range p.Uniforms {
args = append(args, fmt.Sprintf("U%d", i))
}
for i := 0; i < shaderir.TextureCount; i++ {
for i := 0; i < p.TextureNum; i++ {
args = append(args, fmt.Sprintf("T%d", i))
}
}

View File

@ -22,11 +22,10 @@ import (
"strings"
)
const TextureCount = 4
type Program struct {
UniformNames []string
Uniforms []Type
TextureNum int
Attributes []Type
Varyings []Type
Funcs []Func