Compare commits

..

No commits in common. "dc68152f171a10f45db6cf4335d6051c4f5f5ed7" and "7142a3bcd90328a18311ba23a94a9cb43e04dd01" have entirely different histories.

13 changed files with 130 additions and 424 deletions

View File

@ -295,16 +295,6 @@ type Vertex struct {
ColorG float32 ColorG float32
ColorB float32 ColorB float32
ColorA 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{} var _ [0]byte = [unsafe.Sizeof(Vertex{}) - unsafe.Sizeof(float32(0))*graphics.VertexFloatCount]byte{}
@ -678,10 +668,6 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader
vs[i*graphics.VertexFloatCount+5] = v.ColorG vs[i*graphics.VertexFloatCount+5] = v.ColorG
vs[i*graphics.VertexFloatCount+6] = v.ColorB vs[i*graphics.VertexFloatCount+6] = v.ColorB
vs[i*graphics.VertexFloatCount+7] = v.ColorA 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)) is := i.ensureTmpIndices(len(indices))

View File

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

View File

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

View File

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

View File

@ -27,10 +27,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl" "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], SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0, SemanticIndex: 0,
@ -58,25 +55,6 @@ func init() {
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA, InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0, 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 { func blendFactorToBlend11(f graphicsdriver.BlendFactor, alpha bool) _D3D11_BLEND {

View File

@ -23,10 +23,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "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], SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0, SemanticIndex: 0,
@ -54,25 +51,6 @@ func init() {
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0, 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 const numDescriptorsPerFrame = 32

View File

@ -102,7 +102,7 @@ func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, ferr error)
return vsh, psh, nil return vsh, psh, nil
} }
vs, ps, _ := hlsl.Compile(program) vs, ps := hlsl.Compile(program)
var flag uint32 = uint32(_D3DCOMPILE_OPTIMIZATION_LEVEL3) var flag uint32 = uint32(_D3DCOMPILE_OPTIMIZATION_LEVEL3)
var wg errgroup.Group var wg errgroup.Group

View File

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

View File

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

View File

@ -31,24 +31,32 @@ import (
func glslVertexNormalize(str string) string { func glslVertexNormalize(str string) string {
p := glsl.VertexPrelude(glsl.GLSLVersionDefault) p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
str = strings.TrimPrefix(str, p) if strings.HasPrefix(str, p) {
str = str[len(p):]
}
return strings.TrimSpace(str) return strings.TrimSpace(str)
} }
func glslFragmentNormalize(str string) string { func glslFragmentNormalize(str string) string {
p := glsl.FragmentPrelude(glsl.GLSLVersionDefault) p := glsl.FragmentPrelude(glsl.GLSLVersionDefault)
str = strings.TrimPrefix(str, p) if strings.HasPrefix(str, p) {
str = str[len(p):]
}
return strings.TrimSpace(str) return strings.TrimSpace(str)
} }
func hlslNormalize(str string, prelude string) string { func hlslNormalize(str string) string {
str = strings.TrimPrefix(str, prelude) if strings.HasPrefix(str, hlsl.Prelude) {
str = str[len(hlsl.Prelude):]
}
return strings.TrimSpace(str) return strings.TrimSpace(str)
} }
func metalNormalize(str string) string { func metalNormalize(str string) string {
prelude := msl.Prelude(shaderir.Texels) prelude := msl.Prelude(shaderir.Texels)
str = strings.TrimPrefix(str, prelude) if strings.HasPrefix(str, prelude) {
str = str[len(prelude):]
}
return strings.TrimSpace(str) return strings.TrimSpace(str)
} }
@ -180,8 +188,8 @@ func TestCompile(t *testing.T) {
} }
if tc.HLSL != nil { if tc.HLSL != nil {
vs, _, prelude := hlsl.Compile(s) vs, _ := hlsl.Compile(s)
if got, want := hlslNormalize(vs, prelude), hlslNormalize(string(tc.HLSL), prelude); got != want { if got, want := hlslNormalize(vs), hlslNormalize(string(tc.HLSL)); got != want {
compare(t, "HLSL", got, want) compare(t, "HLSL", got, want)
} }
} }

View File

@ -2,7 +2,7 @@ cbuffer Uniforms : register(b0) {
float2 U0 : packoffset(c0); float2 U0 : packoffset(c0);
} }
Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR0) { Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {
Varyings varyings; Varyings varyings;
float4x4 l0 = 0.0; float4x4 l0 = 0.0;
varyings.Position = 0.0; varyings.Position = 0.0;

View File

@ -52,7 +52,13 @@ func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) strin
return n return n
} }
const utilFuncs = `float mod(float x, float y) { const Prelude = `struct Varyings {
float4 Position : SV_POSITION;
float2 M0 : TEXCOORD0;
float4 M1 : COLOR;
};
float mod(float x, float y) {
return x - y * floor(x/y); return x - y * floor(x/y);
} }
@ -80,7 +86,7 @@ float4x4 float4x4FromScalar(float x) {
return float4x4(x, 0, 0, 0, 0, x, 0, 0, 0, 0, x, 0, 0, 0, 0, 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, prelude string) { func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
offsets := CalcUniformMemoryOffsets(p) offsets := CalcUniformMemoryOffsets(p)
c := &compileContext{ c := &compileContext{
@ -88,30 +94,7 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) {
} }
var lines []string var lines []string
lines = append(lines, strings.Split(utilFuncs, "\n")...) lines = append(lines, strings.Split(Prelude, "\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}}") lines = append(lines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 { if len(p.Uniforms) > 0 {
@ -173,25 +156,7 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) {
} }
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 { if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
vslines = append(vslines, "") vslines = append(vslines, "")
var args []string vslines = append(vslines, "Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {")
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, fmt.Sprintf("\tVaryings %s;", vsOut))
vslines = append(vslines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...) 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 { if last := fmt.Sprintf("\treturn %s;", vsOut); vslines[len(vslines)-1] != last {

View File

@ -2595,193 +2595,3 @@ 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)
}
}
}
}
}