internal/shaderir/hlsl: refactoring: separate calculation uniform offsets

This commit is contained in:
Hajime Hoshi 2024-05-05 20:47:35 +09:00
parent a41af4528b
commit 5d4a68b0ea
6 changed files with 15 additions and 13 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
)
var inputElementDescsForDX11 = []_D3D11_INPUT_ELEMENT_DESC{
@ -481,7 +482,7 @@ func (g *graphics11) MaxImageSize() int {
}
func (g *graphics11) NewShader(program *shaderir.Program) (graphicsdriver.Shader, error) {
vsh, psh, offsets, err := compileShader(program)
vsh, psh, err := compileShader(program)
if err != nil {
return nil, err
}
@ -490,7 +491,7 @@ func (g *graphics11) NewShader(program *shaderir.Program) (graphicsdriver.Shader
graphics: g,
id: g.genNextShaderID(),
uniformTypes: program.Uniforms,
uniformOffsets: offsets,
uniformOffsets: hlsl.CalcUniformMemoryOffsets(program),
vertexShaderBlob: vsh,
pixelShaderBlob: psh,
}

View File

@ -25,6 +25,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
)
type resourceWithSize struct {
@ -1063,7 +1064,7 @@ func (g *graphics12) MaxImageSize() int {
}
func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader, error) {
vsh, psh, offsets, err := compileShader(program)
vsh, psh, err := compileShader(program)
if err != nil {
return nil, err
}
@ -1072,7 +1073,7 @@ func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader
graphics: g,
id: g.genNextShaderID(),
uniformTypes: program.Uniforms,
uniformOffsets: offsets,
uniformOffsets: hlsl.CalcUniformMemoryOffsets(program),
vertexShader: vsh,
pixelShader: psh,
}

View File

@ -26,8 +26,8 @@ import (
var vertexShaderCache = map[string]*_ID3DBlob{}
func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, uniformOffsets []int, ferr error) {
vs, ps, offsets := hlsl.Compile(program)
func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, ferr error) {
vs, ps := hlsl.Compile(program)
var flag uint32 = uint32(_D3DCOMPILE_OPTIMIZATION_LEVEL3)
defer func() {
@ -76,10 +76,10 @@ func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, uniformOffse
})
if err := wg.Wait(); err != nil {
return nil, nil, nil, err
return nil, nil, err
}
return vsh, psh, offsets, nil
return vsh, psh, nil
}
func constantBufferSize(uniformTypes []shaderir.Type, uniformOffsets []int) int {

View File

@ -188,7 +188,7 @@ func TestCompile(t *testing.T) {
}
if tc.HLSL != nil {
vs, _, _ := hlsl.Compile(s)
vs, _ := hlsl.Compile(s)
if got, want := hlslNormalize(vs), hlslNormalize(string(tc.HLSL)); got != want {
compare(t, "HLSL", got, want)
}

View File

@ -86,8 +86,8 @@ 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, offsets []int) {
offsets = calculateMemoryOffsets(p.Uniforms)
func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
offsets := CalcUniformMemoryOffsets(p)
c := &compileContext{
unit: p.Unit,

View File

@ -22,7 +22,7 @@ import (
const boundaryInBytes = 16
func calculateMemoryOffsets(uniforms []shaderir.Type) []int {
func CalcUniformMemoryOffsets(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
@ -38,7 +38,7 @@ func calculateMemoryOffsets(uniforms []shaderir.Type) []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 uniforms {
for _, u := range program.Uniforms {
switch u.Main {
case shaderir.Float:
offsets = append(offsets, head)