diff --git a/internal/graphics/vertex.go b/internal/graphics/vertex.go index 6d59f31c8..6efa85e2e 100644 --- a/internal/graphics/vertex.go +++ b/internal/graphics/vertex.go @@ -29,7 +29,7 @@ const ( ProjectionMatrixUniformVariableIndex = 6 - PreservedUniformUint32Count = 2 + // the destination texture size + PreservedUniformDwordCount = 2 + // the destination texture size 2*ShaderSrcImageCount + // the source texture sizes array 2 + // the destination image region origin 2 + // the destination image region size diff --git a/internal/graphicscommand/bench_test.go b/internal/graphicscommand/bench_test.go index 03a7cf0a1..177dbd79c 100644 --- a/internal/graphicscommand/bench_test.go +++ b/internal/graphicscommand/bench_test.go @@ -23,7 +23,7 @@ import ( ) func BenchmarkPrependPreservedUniforms(b *testing.B) { - var uniforms [graphics.PreservedUniformUint32Count]uint32 + var uniforms [graphics.PreservedUniformDwordCount]uint32 dst := graphicscommand.NewImage(16, 16, false, "") src := graphicscommand.NewImage(16, 16, false, "") dr := image.Rect(0, 0, 16, 16) diff --git a/internal/graphicscommand/commandqueue.go b/internal/graphicscommand/commandqueue.go index 2e30dd1cd..caaa31707 100644 --- a/internal/graphicscommand/commandqueue.go +++ b/internal/graphicscommand/commandqueue.go @@ -347,15 +347,15 @@ func imageRectangleToRectangleF32(r image.Rectangle) rectangleF32 { func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shader, dst *Image, srcs [graphics.ShaderSrcImageCount]*Image, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle) []uint32 { origUniforms := uniforms - uniforms = q.uint32sBuffer.alloc(len(origUniforms) + graphics.PreservedUniformUint32Count) - copy(uniforms[graphics.PreservedUniformUint32Count:], origUniforms) + uniforms = q.uint32sBuffer.alloc(len(origUniforms) + graphics.PreservedUniformDwordCount) + copy(uniforms[graphics.PreservedUniformDwordCount:], origUniforms) return prependPreservedUniforms(uniforms, shader, dst, srcs, dstRegion, srcRegions) } func prependPreservedUniforms(uniforms []uint32, shader *Shader, dst *Image, srcs [graphics.ShaderSrcImageCount]*Image, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle) []uint32 { // Set the destination texture size. // Hard-code indices for BCE optimization. - _ = uniforms[graphics.PreservedUniformUint32Count-1] + _ = uniforms[graphics.PreservedUniformDwordCount-1] dw, dh := dst.InternalSize() uniforms[0] = math.Float32bits(float32(dw)) @@ -469,7 +469,7 @@ func prependPreservedUniforms(uniforms []uint32, shader *Shader, dst *Image, src } // Confirm the concrete value of graphics.PreservedUniformUint32Count. -var _ [0]struct{} = [graphics.PreservedUniformUint32Count - 46]struct{}{} +var _ [0]struct{} = [graphics.PreservedUniformDwordCount - 46]struct{}{} type commandQueuePool struct { cache []*commandQueue diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 6e564f4cb..d8ff8dc05 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -513,7 +513,7 @@ func (g *graphics11) NewShader(program *shaderir.Program) (graphicsdriver.Shader graphics: g, id: g.genNextShaderID(), uniformTypes: program.Uniforms, - uniformOffsets: hlsl.UniformVariableOffsetsInDWords(program), + uniformOffsets: hlsl.UniformVariableOffsetsInDwords(program), vertexShaderBlob: vsh, pixelShaderBlob: psh, } diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 67a203734..2e4b54f65 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1073,7 +1073,7 @@ func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader graphics: g, id: g.genNextShaderID(), uniformTypes: program.Uniforms, - uniformOffsets: hlsl.UniformVariableOffsetsInDWords(program), + uniformOffsets: hlsl.UniformVariableOffsetsInDwords(program), vertexShader: vsh, pixelShader: psh, } diff --git a/internal/graphicsdriver/directx/shader_windows.go b/internal/graphicsdriver/directx/shader_windows.go index 184ce0073..232a0b2d7 100644 --- a/internal/graphicsdriver/directx/shader_windows.go +++ b/internal/graphicsdriver/directx/shader_windows.go @@ -209,7 +209,7 @@ func adjustUniforms(uniformTypes []shaderir.Type, uniformOffsets []int, uniforms fs = append(fs, make([]uint32, uniformOffsets[i]-len(fs))...) } - n := typ.Uint32Count() + n := typ.DwordCount() switch typ.Main { case shaderir.Float: fs = append(fs, uniforms[idx:idx+1]...) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index cac47988b..f0816b45b 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -224,7 +224,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. var idx int for i, typ := range shader.ir.Uniforms { - n := typ.Uint32Count() + n := typ.DwordCount() g.uniformVars[i].name = g.uniformVariableName(i) g.uniformVars[i].value = uniforms[idx : idx+n] g.uniformVars[i].typ = typ diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index ad37f373a..20fffaedf 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -292,7 +292,7 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu if u.value == nil { continue } - if got, expected := len(u.value), u.typ.Uint32Count(); got != expected { + if got, expected := len(u.value), u.typ.DwordCount(); got != expected { // Copy a shaderir.Type value once. Do not pass u.typ directly to fmt.Errorf arguments, or // the value u would be allocated on heap. typ := u.typ diff --git a/internal/shaderir/bench_test.go b/internal/shaderir/bench_test.go index ad7fabc8b..a0c8858d1 100644 --- a/internal/shaderir/bench_test.go +++ b/internal/shaderir/bench_test.go @@ -27,7 +27,7 @@ func BenchmarkFilter(b *testing.B) { if err != nil { b.Fatal(err) } - uniforms := make([]uint32, graphics.PreservedUniformUint32Count) + uniforms := make([]uint32, graphics.PreservedUniformDwordCount) for i := 0; i < b.N; i++ { s.FilterUniformVariables(uniforms) } diff --git a/internal/shaderir/hlsl/hlsl.go b/internal/shaderir/hlsl/hlsl.go index 3735bf58d..8042d2390 100644 --- a/internal/shaderir/hlsl/hlsl.go +++ b/internal/shaderir/hlsl/hlsl.go @@ -81,7 +81,7 @@ float4x4 float4x4FromScalar(float x) { }` func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) { - offsets := UniformVariableOffsetsInDWords(p) + offsets := UniformVariableOffsetsInDwords(p) c := &compileContext{ unit: p.Unit, @@ -119,8 +119,8 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) { lines = append(lines, "cbuffer Uniforms : register(b0) {") for i, t := range p.Uniforms { // packingoffset is not mandatory, but this is useful to ensure the correct offset is used. - offset := fmt.Sprintf("c%d", offsets[i]/UniformVariableBoundaryInDWords) - switch offsets[i] % UniformVariableBoundaryInDWords { + offset := fmt.Sprintf("c%d", offsets[i]/UniformVariableBoundaryInDwords) + switch offsets[i] % UniformVariableBoundaryInDwords { case 1: offset += ".y" case 2: diff --git a/internal/shaderir/hlsl/uniform.go b/internal/shaderir/hlsl/uniform.go index c295d23c6..4c2072ae8 100644 --- a/internal/shaderir/hlsl/uniform.go +++ b/internal/shaderir/hlsl/uniform.go @@ -20,10 +20,10 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) -const UniformVariableBoundaryInDWords = 4 +const UniformVariableBoundaryInDwords = 4 -// UniformVariableOffsetsInDWords returns the offsets of the uniform variables in DWROD units in the HLSL layout. -func UniformVariableOffsetsInDWords(program *shaderir.Program) []int { +// UniformVariableOffsetsInDwords returns the offsets of the uniform variables in DWROD units in the HLSL layout. +func UniformVariableOffsetsInDwords(program *shaderir.Program) []int { // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules // https://github.com/microsoft/DirectXShaderCompiler/wiki/Buffer-Packing @@ -31,59 +31,59 @@ func UniformVariableOffsetsInDWords(program *shaderir.Program) []int { if x == 0 { return 0 } - return ((x-1)/UniformVariableBoundaryInDWords + 1) * UniformVariableBoundaryInDWords + return ((x-1)/UniformVariableBoundaryInDwords + 1) * UniformVariableBoundaryInDwords } - var offsetsInDWords []int - var headInDWords int + var offsetsInDwords []int + var headInDwords int // TODO: Reorder the variables with packoffset. // See https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-variable-packoffset for _, u := range program.Uniforms { switch u.Main { case shaderir.Float: - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 1 + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 1 case shaderir.Int: - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 1 + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 1 case shaderir.Vec2, shaderir.IVec2: - if headInDWords%UniformVariableBoundaryInDWords >= 3 { - headInDWords = align(headInDWords) + if headInDwords%UniformVariableBoundaryInDwords >= 3 { + headInDwords = align(headInDwords) } - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 2 + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 2 case shaderir.Vec3, shaderir.IVec3: - if headInDWords%UniformVariableBoundaryInDWords >= 2 { - headInDWords = align(headInDWords) + if headInDwords%UniformVariableBoundaryInDwords >= 2 { + headInDwords = align(headInDwords) } - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 3 + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 3 case shaderir.Vec4, shaderir.IVec4: - if headInDWords%UniformVariableBoundaryInDWords >= 1 { - headInDWords = align(headInDWords) + if headInDwords%UniformVariableBoundaryInDwords >= 1 { + headInDwords = align(headInDwords) } - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 4 + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 4 case shaderir.Mat2: // For matrices, each column is aligned to the boundary. - headInDWords = align(headInDWords) - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 6 + headInDwords = align(headInDwords) + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 6 case shaderir.Mat3: - headInDWords = align(headInDWords) - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 11 + headInDwords = align(headInDwords) + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 11 case shaderir.Mat4: - headInDWords = align(headInDWords) - offsetsInDWords = append(offsetsInDWords, headInDWords) - headInDWords += 16 + headInDwords = align(headInDwords) + offsetsInDwords = append(offsetsInDwords, headInDwords) + headInDwords += 16 case shaderir.Array: // Each array is 16-byte aligned. // TODO: What if the array has 2 or more dimensions? - headInDWords = align(headInDWords) - offsetsInDWords = append(offsetsInDWords, headInDWords) - n := u.Sub[0].Uint32Count() + headInDwords = align(headInDwords) + offsetsInDwords = append(offsetsInDwords, headInDwords) + n := u.Sub[0].DwordCount() switch u.Sub[0].Main { case shaderir.Mat2: n = 6 @@ -92,9 +92,9 @@ func UniformVariableOffsetsInDWords(program *shaderir.Program) []int { case shaderir.Mat4: n = 16 } - headInDWords += (u.Length - 1) * align(n) + headInDwords += (u.Length - 1) * align(n) // The last element is not with a padding. - headInDWords += n + headInDwords += n case shaderir.Struct: // TODO: Implement this panic("hlsl: offset for a struct is not implemented yet") @@ -103,5 +103,5 @@ func UniformVariableOffsetsInDWords(program *shaderir.Program) []int { } } - return offsetsInDWords + return offsetsInDwords } diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index 8688057cb..10f2968ad 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -493,7 +493,7 @@ func (p *Program) FilterUniformVariables(uniforms []uint32) { p.uniformFactors = make([]uint32, len(uniforms)) var idx int for i, typ := range p.Uniforms { - c := typ.Uint32Count() + c := typ.DwordCount() if reachableUniforms[i] { for i := idx; i < idx+c; i++ { p.uniformFactors[i] = 1 diff --git a/internal/shaderir/type.go b/internal/shaderir/type.go index f5a9d3cee..3a72c1e9d 100644 --- a/internal/shaderir/type.go +++ b/internal/shaderir/type.go @@ -87,7 +87,7 @@ func (t Type) String() string { } } -func (t Type) Uint32Count() int { +func (t Type) DwordCount() int { switch t.Main { case Int: return 1 @@ -112,7 +112,7 @@ func (t Type) Uint32Count() int { case Mat4: return 16 case Array: - return t.Length * t.Sub[0].Uint32Count() + return t.Length * t.Sub[0].DwordCount() default: // TODO: Parse a struct correctly return -1 } diff --git a/internal/ui/shader.go b/internal/ui/shader.go index 10589e72d..b30a67f8c 100644 --- a/internal/ui/shader.go +++ b/internal/ui/shader.go @@ -27,9 +27,9 @@ import ( type Shader struct { shader *atlas.Shader - uniformNames []string - uniformTypes []shaderir.Type - uniformUint32Count int + uniformNames []string + uniformTypes []shaderir.Type + uniformDwordCount int } func NewShader(ir *shaderir.Program, name string) *Shader { @@ -45,20 +45,20 @@ func (s *Shader) Deallocate() { } func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 { - if s.uniformUint32Count == 0 { + if s.uniformDwordCount == 0 { for _, typ := range s.uniformTypes { - s.uniformUint32Count += typ.Uint32Count() + s.uniformDwordCount += typ.DwordCount() } } origLen := len(dst) - if cap(dst)-len(dst) >= s.uniformUint32Count { - dst = dst[:len(dst)+s.uniformUint32Count] + if cap(dst)-len(dst) >= s.uniformDwordCount { + dst = dst[:len(dst)+s.uniformDwordCount] for i := origLen; i < len(dst); i++ { dst[i] = 0 } } else { - dst = append(dst, make([]uint32, s.uniformUint32Count)...) + dst = append(dst, make([]uint32, s.uniformDwordCount)...) } idx := origLen @@ -71,23 +71,23 @@ func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 t := v.Type() switch t.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - if typ.Uint32Count() != 1 { + if typ.DwordCount() != 1 { panic(fmt.Sprintf("ui: unexpected uniform value for %s (%s)", name, typ.String())) } dst[idx] = uint32(v.Int()) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - if typ.Uint32Count() != 1 { + if typ.DwordCount() != 1 { panic(fmt.Sprintf("ui: unexpected uniform value for %s (%s)", name, typ.String())) } dst[idx] = uint32(v.Uint()) case reflect.Float32, reflect.Float64: - if typ.Uint32Count() != 1 { + if typ.DwordCount() != 1 { panic(fmt.Sprintf("ui: unexpected uniform value for %s (%s)", name, typ.String())) } dst[idx] = math.Float32bits(float32(v.Float())) case reflect.Slice, reflect.Array: l := v.Len() - if typ.Uint32Count() != l { + if typ.DwordCount() != l { panic(fmt.Sprintf("ui: unexpected uniform value for %s (%s)", name, typ.String())) } switch t.Elem().Kind() { @@ -111,7 +111,7 @@ func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 } } - idx += typ.Uint32Count() + idx += typ.DwordCount() } return dst