internal/shader: bug fix: test failures

Updates #2640
This commit is contained in:
Hajime Hoshi 2024-08-25 17:43:40 +09:00
parent fef487e09d
commit bff760af01
4 changed files with 13 additions and 11 deletions

View File

@ -102,7 +102,7 @@ func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, ferr error)
return vsh, psh, nil
}
vs, ps := hlsl.Compile(program)
vs, ps, _ := hlsl.Compile(program)
var flag uint32 = uint32(_D3DCOMPILE_OPTIMIZATION_LEVEL3)
var wg errgroup.Group

View File

@ -41,8 +41,8 @@ func glslFragmentNormalize(str string) string {
return strings.TrimSpace(str)
}
func hlslNormalize(str string) string {
str = strings.TrimPrefix(str, hlsl.Prelude)
func hlslNormalize(str string, prelude string) string {
str = strings.TrimPrefix(str, prelude)
return strings.TrimSpace(str)
}
@ -180,8 +180,8 @@ func TestCompile(t *testing.T) {
}
if tc.HLSL != nil {
vs, _ := hlsl.Compile(s)
if got, want := hlslNormalize(vs), hlslNormalize(string(tc.HLSL)); got != want {
vs, _, prelude := hlsl.Compile(s)
if got, want := hlslNormalize(vs, prelude), hlslNormalize(string(tc.HLSL), prelude); got != want {
compare(t, "HLSL", got, want)
}
}

View File

@ -2,7 +2,7 @@ cbuffer Uniforms : register(b0) {
float2 U0 : packoffset(c0);
}
Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {
Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR0) {
Varyings varyings;
float4x4 l0 = 0.0;
varyings.Position = 0.0;

View File

@ -52,7 +52,7 @@ func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) strin
return n
}
const Prelude = `float mod(float x, float y) {
const utilFuncs = `float mod(float x, float y) {
return x - y * floor(x/y);
}
@ -80,7 +80,7 @@ float4x4 float4x4FromScalar(float x) {
return float4x4(x, 0, 0, 0, 0, x, 0, 0, 0, 0, x, 0, 0, 0, 0, x);
}`
func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) {
offsets := CalcUniformMemoryOffsets(p)
c := &compileContext{
@ -88,7 +88,7 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
}
var lines []string
lines = append(lines, strings.Split(Prelude, "\n")...)
lines = append(lines, strings.Split(utilFuncs, "\n")...)
lines = append(lines, "", "struct Varyings {")
lines = append(lines, "\tfloat4 Position : SV_POSITION;")
@ -96,7 +96,7 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
for i, v := range p.Varyings {
switch i {
case 0:
lines = append(lines, fmt.Sprintf("\tfloat2 M%d : TEXCOORD0;", i))
lines = append(lines, fmt.Sprintf("\tfloat2 M%d : TEXCOORD;", i))
case 1:
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR0;", i))
default:
@ -110,6 +110,8 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
}
}
lines = append(lines, "};")
prelude = strings.Join(lines, "\n")
lines = append(lines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 {
@ -177,7 +179,7 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
case 0:
args = append(args, fmt.Sprintf("float2 A%d : POSITION", i))
case 1:
args = append(args, fmt.Sprintf("float2 A%d : TEXCOORD0", i))
args = append(args, fmt.Sprintf("float2 A%d : TEXCOORD", i))
case 2:
args = append(args, fmt.Sprintf("float4 A%d : COLOR0", i))
default: