Skip to content

Commit

Permalink
internal/shaderir/hlsl: refactoring: separate calculation uniform off…
Browse files Browse the repository at this point in the history
…sets
  • Loading branch information
hajimehoshi committed May 5, 2024
1 parent a41af45 commit 5d4a68b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
5 changes: 3 additions & 2 deletions internal/graphicsdriver/directx/graphics11_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
}
Expand Down
5 changes: 3 additions & 2 deletions internal/graphicsdriver/directx/graphics12_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
}
Expand Down
8 changes: 4 additions & 4 deletions internal/graphicsdriver/directx/shader_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/shader/shader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/shaderir/hlsl/hlsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions internal/shaderir/hlsl/packing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 5d4a68b

Please sign in to comment.