Compare commits

...

9 Commits

Author SHA1 Message Date
Hajime Hoshi
dc68152f17 internal/shader: better error message for the fragment entry point 2024-08-25 19:38:57 +09:00
Hajime Hoshi
9693ce8382 internal/shader: allow less arguments at Fragment
Closes #3073
2024-08-25 19:17:41 +09:00
Hajime Hoshi
e2662a8af7 internal/shader: bug fix: wrong indexing 2024-08-25 18:58:05 +09:00
Hajime Hoshi
41b762ba2c ebiten: update comments
Updates #2640
2024-08-25 18:45:26 +09:00
Hajime Hoshi
2cc809516f ebiten: add Vertex.Custom0 to Custom3
Closes #2640
2024-08-25 18:11:39 +09:00
Hajime Hoshi
bff760af01 internal/shader: bug fix: test failures
Updates #2640
2024-08-25 17:43:44 +09:00
Hajime Hoshi
fef487e09d internal/shaderir/hlsl: refactoring: more flexible generation
Updates #2640
2024-08-25 17:27:00 +09:00
Hajime Hoshi
ed45843c13 internal/graphicsdriver/opengl: assume custom attributes are vec4
Due to HLSL restrictions, all the attributes must have a semantics.
Always assuming custom attributes are vec4 makes things simpler.

Updates #2640
2024-08-25 17:11:28 +09:00
Hajime Hoshi
107189a00d internal/shader: use strings.TrimPrefix 2024-08-25 15:43:44 +09:00
13 changed files with 424 additions and 130 deletions

View File

@ -295,6 +295,16 @@ type Vertex struct {
ColorG float32
ColorB float32
ColorA float32
// Custom0/Custom1/Custom2/Custom3 represents general-purpose values passed to the shader.
// In order to use them, Fragment must have an additional vec4 argument.
//
// These values are valid only when DrawTrianglesShader is used.
// In other cases, these values are ignored.
Custom0 float32
Custom1 float32
Custom2 float32
Custom3 float32
}
var _ [0]byte = [unsafe.Sizeof(Vertex{}) - unsafe.Sizeof(float32(0))*graphics.VertexFloatCount]byte{}
@ -668,6 +678,10 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader
vs[i*graphics.VertexFloatCount+5] = v.ColorG
vs[i*graphics.VertexFloatCount+6] = v.ColorB
vs[i*graphics.VertexFloatCount+7] = v.ColorA
vs[i*graphics.VertexFloatCount+8] = v.Custom0
vs[i*graphics.VertexFloatCount+9] = v.Custom1
vs[i*graphics.VertexFloatCount+10] = v.Custom2
vs[i*graphics.VertexFloatCount+11] = v.Custom3
}
is := i.ensureTmpIndices(len(indices))

View File

@ -170,7 +170,7 @@ var ScreenShaderSource = []byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
func Fragment(dstPos vec4, srcPos vec2) vec4 {
// Blend source colors in a square region, which size is 1/scale.
scale := imageDstSize()/imageSrc0Size()
pos := srcPos
@ -193,7 +193,7 @@ var ClearShaderSource = []byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
func Fragment() vec4 {
return vec4(0)
}
`)

View File

@ -154,8 +154,8 @@ func imageSrc%[1]dAt(pos vec2) vec4 {
shaderSuffix += `
var __projectionMatrix mat4
func __vertex(dstPos vec2, srcPos vec2, color vec4) (vec4, vec2, vec4) {
return __projectionMatrix * vec4(dstPos, 0, 1), srcPos, color
func __vertex(dstPos vec2, srcPos vec2, color vec4, custom vec4) (vec4, vec2, vec4, vec4) {
return __projectionMatrix * vec4(dstPos, 0, 1), srcPos, color, custom
}
`
return shaderSuffix, nil

View File

@ -39,7 +39,7 @@ const (
)
const (
VertexFloatCount = 8
VertexFloatCount = 12
)
var (

View File

@ -27,7 +27,10 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
)
var inputElementDescsForDX11 = []_D3D11_INPUT_ELEMENT_DESC{
var inputElementDescsForDX11 []_D3D11_INPUT_ELEMENT_DESC
func init() {
inputElementDescsForDX11 = []_D3D11_INPUT_ELEMENT_DESC{
{
SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0,
@ -55,6 +58,25 @@ var inputElementDescsForDX11 = []_D3D11_INPUT_ELEMENT_DESC{
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
}
diff := graphics.VertexFloatCount - 8
if diff == 0 {
return
}
if diff%4 != 0 {
panic("directx: unexpected attribute layout")
}
for i := 0; i < diff/4; i++ {
inputElementDescsForDX11 = append(inputElementDescsForDX11, _D3D11_INPUT_ELEMENT_DESC{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: uint32(i) + 1,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
})
}
}
func blendFactorToBlend11(f graphicsdriver.BlendFactor, alpha bool) _D3D11_BLEND {

View File

@ -23,7 +23,10 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
)
var inputElementDescsForDX12 = []_D3D12_INPUT_ELEMENT_DESC{
var inputElementDescsForDX12 []_D3D12_INPUT_ELEMENT_DESC
func init() {
inputElementDescsForDX12 = []_D3D12_INPUT_ELEMENT_DESC{
{
SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0,
@ -51,6 +54,25 @@ var inputElementDescsForDX12 = []_D3D12_INPUT_ELEMENT_DESC{
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
}
diff := graphics.VertexFloatCount - 8
if diff == 0 {
return
}
if diff%4 != 0 {
panic("directx: unexpected attribute layout")
}
for i := 0; i < diff/4; i++ {
inputElementDescsForDX12 = append(inputElementDescsForDX12, _D3D12_INPUT_ELEMENT_DESC{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: uint32(i) + 1,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
})
}
}
const numDescriptorsPerFrame = 32

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

@ -114,17 +114,17 @@ func init() {
},
}
n := theArrayBufferLayout.float32Count()
if n > graphics.VertexFloatCount {
panic("opengl: the array buffer layout is too large")
diff := graphics.VertexFloatCount - n
if diff == 0 {
return
}
if n < graphics.VertexFloatCount {
d := graphics.VertexFloatCount - n
if d > 4 {
panic("opengl: the array buffer layout is too small")
if diff%4 != 0 {
panic("opengl: unexpected attribute layout")
}
for i := 0; i < diff/4; i++ {
theArrayBufferLayout.addPart(arrayBufferLayoutPart{
name: "A3",
num: d,
name: fmt.Sprintf("A%d", i+3),
num: 4,
})
}
}

View File

@ -290,10 +290,10 @@ func (cs *compileState) parse(f *ast.File) {
// Parse function names so that any other function call the others.
// The function data is provisional and will be updated soon.
var vertexInParams []shaderir.Type
var vertexOutParams []shaderir.Type
var fragmentInParams []shaderir.Type
var fragmentOutParams []shaderir.Type
var vertexInParams []variable
var vertexOutParams []variable
var fragmentInParams []variable
var fragmentOutParams []variable
var fragmentReturnType shaderir.Type
for _, d := range f.Decls {
fd, ok := d.(*ast.FuncDecl)
@ -310,6 +310,19 @@ func (cs *compileState) parse(f *ast.File) {
}
inParams, outParams, ret := cs.parseFuncParams(&cs.global, n, fd)
if n == cs.vertexEntry {
vertexInParams = inParams
vertexOutParams = outParams
continue
}
if n == cs.fragmentEntry {
fragmentInParams = inParams
fragmentOutParams = outParams
fragmentReturnType = ret
continue
}
var inT, outT []shaderir.Type
for _, v := range inParams {
inT = append(inT, v.typ)
@ -317,19 +330,6 @@ func (cs *compileState) parse(f *ast.File) {
for _, v := range outParams {
outT = append(outT, v.typ)
}
if n == cs.vertexEntry {
vertexInParams = inT
vertexOutParams = outT
continue
}
if n == cs.fragmentEntry {
fragmentInParams = inT
fragmentOutParams = outT
fragmentReturnType = ret
continue
}
cs.funcs = append(cs.funcs, function{
name: n,
ir: shaderir.Func{
@ -345,25 +345,21 @@ func (cs *compileState) parse(f *ast.File) {
// Check varying variables.
// In testings, there might not be vertex and fragment entry points.
if len(vertexOutParams) > 0 && len(fragmentInParams) > 0 {
if len(vertexOutParams) != len(fragmentInParams) {
cs.addError(0, "the number of vertex entry point's returning values and the number of fragment entry point's params must be the same")
for i, p := range vertexOutParams {
if len(fragmentInParams) <= i {
break
}
for i, t := range vertexOutParams {
if !t.Equal(&fragmentInParams[i]) {
cs.addError(0, "vertex entry point's returning value types and fragment entry point's param types must match")
t := fragmentInParams[i].typ
if !p.typ.Equal(&t) {
name := fragmentInParams[i].name
cs.addError(0, fmt.Sprintf("fragment argument %s must be %s but was %s", name, p.typ.String(), t.String()))
}
}
// The first out-param is treated as gl_Position in GLSL.
if vertexOutParams[0].Main != shaderir.Vec4 {
if vertexOutParams[0].typ.Main != shaderir.Vec4 {
cs.addError(0, "vertex entry point must have at least one returning vec4 value for a position")
}
if len(fragmentInParams) == 0 {
cs.addError(0, "fragment entry point must have at least one vec4 parameter for a position")
}
if fragmentInParams[0].Main != shaderir.Vec4 {
cs.addError(0, "fragment entry point must have at least one vec4 parameter for a position")
}
if len(fragmentOutParams) != 0 || fragmentReturnType.Main != shaderir.Vec4 {
cs.addError(0, "fragment entry point must have one returning vec4 value for a color")
}
@ -373,12 +369,16 @@ func (cs *compileState) parse(f *ast.File) {
return
}
// Set attribute varying veraibles.
cs.ir.Attributes = append(cs.ir.Attributes, vertexInParams...)
// Set attribute and varying veraibles.
for _, p := range vertexInParams {
cs.ir.Attributes = append(cs.ir.Attributes, p.typ)
}
if len(vertexOutParams) > 0 {
// TODO: Check that these params are not arrays or structs
// The 0th argument is a special variable for position and is not included in varying variables.
cs.ir.Varyings = append(cs.ir.Varyings, vertexOutParams[1:]...)
for _, p := range vertexOutParams[1:] {
cs.ir.Varyings = append(cs.ir.Varyings, p.typ)
}
}
// Parse functions.
@ -820,6 +820,25 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
}
inParams, outParams, returnType := cs.parseFuncParams(block, d.Name.Name, d)
if d.Name.Name == cs.fragmentEntry {
if len(inParams) == 0 {
inParams = append(inParams, variable{
name: "_",
typ: shaderir.Type{Main: shaderir.Vec4},
})
}
// The 0th inParams is a special variable for position and is not included in varying variables.
if diff := len(cs.ir.Varyings) - (len(inParams) - 1); diff > 0 {
// inParams is not enough when the vertex shader has more returning values than the fragment shader's arguments.
orig := len(inParams) - 1
for i := 0; i < diff; i++ {
inParams = append(inParams, variable{
name: "_",
typ: cs.ir.Varyings[orig+i],
})
}
}
}
b, ok := cs.parseBlock(block, d.Name.Name, d.Body.List, inParams, outParams, returnType, true)
if !ok {
return function{}, false

View File

@ -31,32 +31,24 @@ import (
func glslVertexNormalize(str string) string {
p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {
str = str[len(p):]
}
str = strings.TrimPrefix(str, p)
return strings.TrimSpace(str)
}
func glslFragmentNormalize(str string) string {
p := glsl.FragmentPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {
str = str[len(p):]
}
str = strings.TrimPrefix(str, p)
return strings.TrimSpace(str)
}
func hlslNormalize(str string) string {
if strings.HasPrefix(str, hlsl.Prelude) {
str = str[len(hlsl.Prelude):]
}
func hlslNormalize(str string, prelude string) string {
str = strings.TrimPrefix(str, prelude)
return strings.TrimSpace(str)
}
func metalNormalize(str string) string {
prelude := msl.Prelude(shaderir.Texels)
if strings.HasPrefix(str, prelude) {
str = str[len(prelude):]
}
str = strings.TrimPrefix(str, prelude)
return strings.TrimSpace(str)
}
@ -188,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,13 +52,7 @@ func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) strin
return n
}
const Prelude = `struct Varyings {
float4 Position : SV_POSITION;
float2 M0 : TEXCOORD0;
float4 M1 : COLOR;
};
float mod(float x, float y) {
const utilFuncs = `float mod(float x, float y) {
return x - y * floor(x/y);
}
@ -86,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{
@ -94,7 +88,30 @@ 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;")
if len(p.Varyings) > 0 {
for i, v := range p.Varyings {
switch i {
case 0:
lines = append(lines, fmt.Sprintf("\tfloat2 M%d : TEXCOORD;", i))
case 1:
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR0;", i))
default:
// Use COLOR[n] as a general purpose varying.
if v.Main != shaderir.Vec4 {
lines = append(lines, fmt.Sprintf("\t?(unexpected type: %s) M%d : COLOR%d;", v, i, i-1))
} else {
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR%d;", i, i-1))
}
}
}
}
lines = append(lines, "};")
prelude = strings.Join(lines, "\n")
lines = append(lines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 {
@ -156,7 +173,25 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
}
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
vslines = append(vslines, "")
vslines = append(vslines, "Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {")
var args []string
for i, a := range p.Attributes {
switch i {
case 0:
args = append(args, fmt.Sprintf("float2 A%d : POSITION", i))
case 1:
args = append(args, fmt.Sprintf("float2 A%d : TEXCOORD", i))
case 2:
args = append(args, fmt.Sprintf("float4 A%d : COLOR0", i))
default:
// Use COLOR[n] as a general purpose varying.
if a.Main != shaderir.Vec4 {
args = append(args, fmt.Sprintf("?(unexpected type: %s) A%d : COLOR%d", a, i, i-2))
} else {
args = append(args, fmt.Sprintf("float4 A%d : COLOR%d", i, i-2))
}
}
}
vslines = append(vslines, "Varyings VSMain("+strings.Join(args, ", ")+") {")
vslines = append(vslines, fmt.Sprintf("\tVaryings %s;", vsOut))
vslines = append(vslines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...)
if last := fmt.Sprintf("\treturn %s;", vsOut); vslines[len(vslines)-1] != last {

View File

@ -2595,3 +2595,193 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
}
}
}
func TestShaderCustomValues(t *testing.T) {
const w, h = 16, 16
dst := ebiten.NewImage(w, h)
s, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4, custom vec4) vec4 {
return custom
}
`))
if err != nil {
t.Fatal(err)
}
clr := color.RGBA{R: 0x10, G: 0x20, B: 0x30, A: 0x40}
dst.DrawTrianglesShader([]ebiten.Vertex{
{
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
{
DstX: w,
DstY: 0,
SrcX: w,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
{
DstX: 0,
DstY: h,
SrcX: 0,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
{
DstX: w,
DstY: h,
SrcX: w,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
}, []uint16{0, 1, 2, 1, 2, 3}, s, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
want := clr
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}
func TestShaderFragmentLessArguments(t *testing.T) {
const w, h = 16, 16
s0, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment() vec4 {
return vec4(1, 0, 0, 1)
}
`))
if err != nil {
t.Fatal(err)
}
s1, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4) vec4 {
return vec4(0, 1, 0, 1)
}
`))
if err != nil {
t.Fatal(err)
}
s2, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2) vec4 {
return vec4(0, 0, 1, 1)
}
`))
if err != nil {
t.Fatal(err)
}
dst := ebiten.NewImage(w, h)
for idx, s := range []*ebiten.Shader{s0, s1, s2} {
dst.Clear()
dst.DrawTrianglesShader([]ebiten.Vertex{
{
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: w,
DstY: 0,
SrcX: w,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: 0,
DstY: h,
SrcX: 0,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: w,
DstY: h,
SrcX: w,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
}, []uint16{0, 1, 2, 1, 2, 3}, s, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
var want color.RGBA
switch idx {
case 0:
want = color.RGBA{R: 0xff, A: 0xff}
case 1:
want = color.RGBA{G: 0xff, A: 0xff}
case 2:
want = color.RGBA{B: 0xff, A: 0xff}
}
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}
}