internal/shaderir/hlsl: refactoring

This commit is contained in:
Hajime Hoshi 2024-11-24 18:30:34 +09:00
parent ee5b6a3680
commit c5d664ccf7
5 changed files with 47 additions and 47 deletions

View File

@ -513,7 +513,7 @@ func (g *graphics11) NewShader(program *shaderir.Program) (graphicsdriver.Shader
graphics: g, graphics: g,
id: g.genNextShaderID(), id: g.genNextShaderID(),
uniformTypes: program.Uniforms, uniformTypes: program.Uniforms,
uniformOffsets: hlsl.CalcUniformMemoryOffsets(program), uniformOffsets: hlsl.CalcUniformMemoryOffsetsInDWords(program),
vertexShaderBlob: vsh, vertexShaderBlob: vsh,
pixelShaderBlob: psh, pixelShaderBlob: psh,
} }

View File

@ -1073,7 +1073,7 @@ func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader
graphics: g, graphics: g,
id: g.genNextShaderID(), id: g.genNextShaderID(),
uniformTypes: program.Uniforms, uniformTypes: program.Uniforms,
uniformOffsets: hlsl.CalcUniformMemoryOffsets(program), uniformOffsets: hlsl.CalcUniformMemoryOffsetsInDWords(program),
vertexShader: vsh, vertexShader: vsh,
pixelShader: psh, pixelShader: psh,
} }

View File

@ -148,8 +148,8 @@ func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, ferr error)
func constantBufferSize(uniformTypes []shaderir.Type, uniformOffsets []int) int { func constantBufferSize(uniformTypes []shaderir.Type, uniformOffsets []int) int {
var size int var size int
for i, typ := range uniformTypes { for i, typ := range uniformTypes {
if size < uniformOffsets[i]/4 { if size < uniformOffsets[i] {
size = uniformOffsets[i] / 4 size = uniformOffsets[i]
} }
switch typ.Main { switch typ.Main {
@ -202,8 +202,8 @@ func adjustUniforms(uniformTypes []shaderir.Type, uniformOffsets []int, uniforms
var fs []uint32 var fs []uint32
var idx int var idx int
for i, typ := range uniformTypes { for i, typ := range uniformTypes {
if len(fs) < uniformOffsets[i]/4 { if len(fs) < uniformOffsets[i] {
fs = append(fs, make([]uint32, uniformOffsets[i]/4-len(fs))...) fs = append(fs, make([]uint32, uniformOffsets[i]-len(fs))...)
} }
n := typ.Uint32Count() n := typ.Uint32Count()

View File

@ -81,7 +81,7 @@ float4x4 float4x4FromScalar(float x) {
}` }`
func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) { func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) {
offsets := CalcUniformMemoryOffsets(p) offsets := CalcUniformMemoryOffsetsInDWords(p)
c := &compileContext{ c := &compileContext{
unit: p.Unit, unit: p.Unit,
@ -119,13 +119,13 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) {
lines = append(lines, "cbuffer Uniforms : register(b0) {") lines = append(lines, "cbuffer Uniforms : register(b0) {")
for i, t := range p.Uniforms { for i, t := range p.Uniforms {
// packingoffset is not mandatory, but this is useful to ensure the correct offset is used. // packingoffset is not mandatory, but this is useful to ensure the correct offset is used.
offset := fmt.Sprintf("c%d", offsets[i]/boundaryInBytes) offset := fmt.Sprintf("c%d", offsets[i]/boundaryInDWords)
switch offsets[i] % boundaryInBytes { switch offsets[i] % boundaryInDWords {
case 4: case 1:
offset += ".y" offset += ".y"
case 8: case 2:
offset += ".z" offset += ".z"
case 12: case 3:
offset += ".w" offset += ".w"
} }
lines = append(lines, fmt.Sprintf("\t%s : packoffset(%s);", c.varDecl(p, &t, fmt.Sprintf("U%d", i)), offset)) lines = append(lines, fmt.Sprintf("\t%s : packoffset(%s);", c.varDecl(p, &t, fmt.Sprintf("U%d", i)), offset))

View File

@ -20,20 +20,20 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
) )
const boundaryInBytes = 16 const boundaryInDWords = 4
func CalcUniformMemoryOffsets(program *shaderir.Program) []int { func CalcUniformMemoryOffsetsInDWords(program *shaderir.Program) []int {
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules
// https://github.com/microsoft/DirectXShaderCompiler/wiki/Buffer-Packing // https://github.com/microsoft/DirectXShaderCompiler/wiki/Buffer-Packing
var offsets []int var offsetsInDWords []int
var head int var headInDWords int
align := func(x int) int { align := func(x int) int {
if x == 0 { if x == 0 {
return 0 return 0
} }
return ((x-1)/boundaryInBytes + 1) * boundaryInBytes return ((x-1)/boundaryInDWords + 1) * boundaryInDWords
} }
// TODO: Reorder the variables with packoffset. // TODO: Reorder the variables with packoffset.
@ -41,47 +41,47 @@ func CalcUniformMemoryOffsets(program *shaderir.Program) []int {
for _, u := range program.Uniforms { for _, u := range program.Uniforms {
switch u.Main { switch u.Main {
case shaderir.Float: case shaderir.Float:
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 headInDWords += 1
case shaderir.Int: case shaderir.Int:
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 headInDWords += 1
case shaderir.Vec2, shaderir.IVec2: case shaderir.Vec2, shaderir.IVec2:
if head%boundaryInBytes >= 4*3 { if headInDWords%boundaryInDWords >= 3 {
head = align(head) headInDWords = align(headInDWords)
} }
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 * 2 headInDWords += 2
case shaderir.Vec3, shaderir.IVec3: case shaderir.Vec3, shaderir.IVec3:
if head%boundaryInBytes >= 4*2 { if headInDWords%boundaryInDWords >= 2 {
head = align(head) headInDWords = align(headInDWords)
} }
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 * 3 headInDWords += 3
case shaderir.Vec4, shaderir.IVec4: case shaderir.Vec4, shaderir.IVec4:
if head%boundaryInBytes >= 4*1 { if headInDWords%boundaryInDWords >= 1 {
head = align(head) headInDWords = align(headInDWords)
} }
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 * 4 headInDWords += 4
case shaderir.Mat2: case shaderir.Mat2:
// For matrices, each column is aligned to the boundary. // For matrices, each column is aligned to the boundary.
head = align(head) headInDWords = align(headInDWords)
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 * 6 headInDWords += 6
case shaderir.Mat3: case shaderir.Mat3:
head = align(head) headInDWords = align(headInDWords)
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 * 11 headInDWords += 11
case shaderir.Mat4: case shaderir.Mat4:
head = align(head) headInDWords = align(headInDWords)
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
head += 4 * 16 headInDWords += 16
case shaderir.Array: case shaderir.Array:
// Each array is 16-byte aligned. // Each array is 16-byte aligned.
// TODO: What if the array has 2 or more dimensions? // TODO: What if the array has 2 or more dimensions?
head = align(head) headInDWords = align(headInDWords)
offsets = append(offsets, head) offsetsInDWords = append(offsetsInDWords, headInDWords)
n := u.Sub[0].Uint32Count() n := u.Sub[0].Uint32Count()
switch u.Sub[0].Main { switch u.Sub[0].Main {
case shaderir.Mat2: case shaderir.Mat2:
@ -91,9 +91,9 @@ func CalcUniformMemoryOffsets(program *shaderir.Program) []int {
case shaderir.Mat4: case shaderir.Mat4:
n = 16 n = 16
} }
head += (u.Length - 1) * align(4*n) headInDWords += (u.Length - 1) * align(n)
// The last element is not with a padding. // The last element is not with a padding.
head += 4 * n headInDWords += n
case shaderir.Struct: case shaderir.Struct:
// TODO: Implement this // TODO: Implement this
panic("hlsl: offset for a struct is not implemented yet") panic("hlsl: offset for a struct is not implemented yet")
@ -102,5 +102,5 @@ func CalcUniformMemoryOffsets(program *shaderir.Program) []int {
} }
} }
return offsets return offsetsInDWords
} }