Skip to content

Commit 9b27e91

Browse files
committed
more compiler improvements
1 parent f3598b6 commit 9b27e91

File tree

9 files changed

+82
-235
lines changed

9 files changed

+82
-235
lines changed
Lines changed: 1 addition & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -1,231 +1,17 @@
11
using SimulationFramework;
22
using SimulationFramework.Drawing;
3-
using SimulationFramework.Drawing.Shaders;
4-
using static SimulationFramework.Drawing.Shaders.ShaderIntrinsics;
5-
using SimulationFramework.Drawing.Shaders.Compiler;
6-
using System;
7-
using System.Numerics;
8-
using System.Runtime.CompilerServices;
9-
using System.Runtime.InteropServices;
3+
using SimulationFramework.Input;
104

115
Start<Program>();
126

137
partial class Program : Simulation
148
{
159
public override unsafe void OnInitialize()
1610
{
17-
ShaderCompiler.DumpShaders = true;
1811
}
1912

2013
public override void OnRender(ICanvas canvas)
2114
{
2215
canvas.Clear(Color.Black);
23-
canvas.Fill(new Shader());
24-
canvas.DrawRect(1, 1, 100, 100);
2516
}
2617
}
27-
28-
class Shader : CanvasShader
29-
{
30-
int[,,] voxels = new int[100, 100, 100];
31-
32-
public override ColorF GetPixelColor(Vector2 position)
33-
{
34-
ColorF r = ColorF.Red;
35-
if (position == new Vector2(0, 0))
36-
{
37-
r = ColorF.Green;
38-
}
39-
else
40-
{
41-
r = ColorF.Orange;
42-
}
43-
44-
raycast(default, out float t, out var n, out var v);
45-
46-
return r;
47-
}
48-
bool raycast(Ray ray, out float outT, out Vector3 outNormal, out int3 outVoxel)
49-
{
50-
outNormal = default;
51-
52-
int3 voxel = new int3(Floor(ray.Origin));
53-
int3 step = new int3(Sign(ray.Direction));
54-
55-
if (step.x == 0 && step.y == 0 && step.z == 0)
56-
{
57-
outT = default;
58-
outNormal = default;
59-
outVoxel = default;
60-
return false;
61-
}
62-
63-
float tNear = 0, tFar = 0;
64-
65-
// if (!PartialBoxRaycast(ray, Vector3.Zero, new(100, 100, 100), out tNear, out tFar))
66-
// {
67-
// outT = default;
68-
// outNormal = default;
69-
// outVoxel = default;
70-
// return false;
71-
// }
72-
73-
// if (voxels[0, 0, 0] != 0)
74-
// {
75-
// outT = default;
76-
// outNormal = default;
77-
// outVoxel = default;
78-
// return true;
79-
// }
80-
81-
Vector3 start = ray.At(tNear);
82-
Vector3 end = ray.At(tFar);
83-
84-
Vector3 d = end - start;
85-
Vector3 tDelta = step.ToVec3() / d;
86-
Vector3 tMax = tDelta * getMax(start, step.ToVec3());
87-
88-
voxel = new int3(start);
89-
90-
int dist = 100;
91-
92-
float t = ray.MinT;
93-
94-
//outT = default;
95-
//outNormal = default;
96-
//outVoxel = default;
97-
//return false;
98-
while (--dist > 0 && t < ray.MaxT)
99-
{
100-
int3 v = voxel;
101-
102-
if (v.x < 0 || v.x >= voxels.GetLength(0) || v.y < 0 || v.y >= voxels.GetLength(1) || v.z < 0 || v.z >= voxels.GetLength(2))
103-
{
104-
outT = 0;
105-
outVoxel = new(-69, -69, -69);
106-
return false;
107-
}
108-
109-
if (voxels[v.x, v.y, v.z] != 0)
110-
{
111-
outT = Max(Max(tMax.X - tDelta.X, tMax.Y - tDelta.Y), tMax.Z - tDelta.Z) * Length(d);
112-
outVoxel = voxel;
113-
return t <= ray.MaxT;
114-
}
115-
116-
if (tMax.X < tMax.Y)
117-
{
118-
if (tMax.X < tMax.Z)
119-
{
120-
voxel.x += step.x;
121-
tMax.X += tDelta.X;
122-
outNormal = Vec3(-step.x, 0, 0);
123-
}
124-
else
125-
{
126-
voxel.z += step.z;
127-
tMax.Z += tDelta.Z;
128-
outNormal = Vec3(0, 0, -step.z);
129-
}
130-
}
131-
else
132-
{
133-
if (tMax.Y < tMax.Z)
134-
{
135-
voxel.y += step.y;
136-
tMax.Y += tDelta.Y;
137-
outNormal = Vec3(0, -step.y, 0);
138-
}
139-
else
140-
{
141-
voxel.z += step.z;
142-
tMax.Z += tDelta.Z;
143-
outNormal = Vec3(0, 0, -step.z);
144-
}
145-
}
146-
}
147-
148-
outT = default;
149-
outVoxel = default;
150-
return false;
151-
}
152-
153-
Vector3 getMax(Vector3 start, Vector3 step)
154-
{
155-
float x, y, z;
156-
if (step.X > 0)
157-
{
158-
x = 1 - Fract(start.X);
159-
}
160-
else
161-
{
162-
x = Fract(start.X);
163-
}
164-
165-
if (step.Y > 0)
166-
{
167-
y = 1 - Fract(start.Y);
168-
}
169-
else
170-
{
171-
y = Fract(start.Y);
172-
}
173-
174-
if (step.Z > 0)
175-
{
176-
z = 1 - Fract(start.Z);
177-
}
178-
else
179-
{
180-
z = Fract(start.Z);
181-
}
182-
183-
return new Vector3(x, y, z);
184-
}
185-
}
186-
187-
struct Ray
188-
{
189-
public Vector3 Origin;
190-
public float MinT;
191-
public Vector3 Direction;
192-
public float MaxT;
193-
194-
public Vector3 At(float t)
195-
{
196-
return Origin + Direction * t;
197-
}
198-
}
199-
200-
struct int3
201-
{
202-
public int x, y, z;
203-
204-
public int3(int x, int y, int z)
205-
{
206-
this.x = x;
207-
this.y = y;
208-
this.z = z;
209-
}
210-
211-
public int3(Vector3 vec)
212-
{
213-
x = (int)vec.X;
214-
y = (int)vec.Y;
215-
z = (int)vec.Z;
216-
}
217-
218-
public Vector3 ToVec3()
219-
{
220-
return new(x, y, z);
221-
}
222-
223-
public static int3 operator +(int3 a, int3 b)
224-
{
225-
return new(
226-
a.x + b.x,
227-
a.y + b.y,
228-
a.z + b.z
229-
);
230-
}
231-
}

src/SimulationFramework.OpenGL.BindngsGenerator/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"glGetUniformLocation",
118118
"glEnableVertexAttribArray",
119119
"glVertexAttribPointer",
120+
"glVertexAttribIPointer",
120121
"glVertexAttribDivisor",
121122
"glGenerateMipmap",
122123
"glDeleteTextures",

src/SimulationFramework.OpenGL/OpenGL.gen.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static nint LoadOptionalFunction(FunctionLoader functionLoader, string name)
100100
pfn_glDeleteVertexArrays = LoadRequiredFunction(functionLoader, "glDeleteVertexArrays");
101101
pfn_glGenVertexArrays = LoadRequiredFunction(functionLoader, "glGenVertexArrays");
102102
pfn_glBindBufferBase = LoadRequiredFunction(functionLoader, "glBindBufferBase");
103+
pfn_glVertexAttribIPointer = LoadRequiredFunction(functionLoader, "glVertexAttribIPointer");
103104
pfn_glUniform1ui = LoadRequiredFunction(functionLoader, "glUniform1ui");
104105
pfn_glUniform2ui = LoadRequiredFunction(functionLoader, "glUniform2ui");
105106
pfn_glUniform3ui = LoadRequiredFunction(functionLoader, "glUniform3ui");
@@ -1344,6 +1345,9 @@ static nint LoadOptionalFunction(FunctionLoader functionLoader, string name)
13441345
private static nint pfn_glBindBufferBase;
13451346
public static void glBindBufferBase(uint target, uint index, uint buffer) => ((delegate* unmanaged[Stdcall]<uint, uint, uint, void>)pfn_glBindBufferBase)(target, index, buffer);
13461347

1348+
private static nint pfn_glVertexAttribIPointer;
1349+
public static void glVertexAttribIPointer(uint index, int size, uint type, int stride, void* pointer) => ((delegate* unmanaged[Stdcall]<uint, int, uint, int, void*, void>)pfn_glVertexAttribIPointer)(index, size, type, stride, pointer);
1350+
13471351
private static nint pfn_glUniform1ui;
13481352
public static void glUniform1ui(int location, uint v0) => ((delegate* unmanaged[Stdcall]<int, uint, void>)pfn_glUniform1ui)(location, v0);
13491353

src/SimulationFramework.OpenGL/ShaderProgram.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public unsafe ShaderProgram(string shaderVersion, string vsSource, string fsSour
3838
glGetProgramiv(programID, GL_LINK_STATUS, &success);
3939
if (success == 0)
4040
{
41-
byte[] infoLog = new byte[512];
41+
byte[] infoLog = new byte[1024];
4242
fixed (byte* infoLogPtr = infoLog)
4343
{
44-
glGetProgramInfoLog(programID, 512, null, infoLogPtr);
44+
glGetProgramInfoLog(programID, 1024, null, infoLogPtr);
4545
throw new(Marshal.PtrToStringUTF8((nint)infoLogPtr));
4646
}
4747
}

src/SimulationFramework.OpenGL/Shaders/Compute/AutoGroupSizeComputeShaderProgram.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SimulationFramework.Drawing.Shaders;
1+
using SimulationFramework.Drawing;
2+
using SimulationFramework.Drawing.Shaders;
23
using SimulationFramework.Drawing.Shaders.Compiler;
34
using System.Diagnostics;
45
using System.Runtime.CompilerServices;
@@ -14,24 +15,24 @@ public AutoGroupSizeComputeShaderProgram(string shaderVersion, ShaderCompilation
1415
{
1516
}
1617

17-
1818
public override ComputeShaderEffect GetEffect(ComputeShader shader, int threadCountX, int threadCountY, int threadCountZ)
1919
{
2020
int index = GetIndexForThreadCount(threadCountX, threadCountY, threadCountZ);
2121
ComputeShaderEffect?[] effectArray = effects.GetValue(shader, _ => new ComputeShaderEffect[8]);
2222

2323
if (effectArray[index] is null)
2424
{
25-
var (groupSizeX, groupSizeY, groupSizeZ) = index switch
25+
var (groupSizeZ, groupSizeY, groupSizeX) = index switch
2626
{
27-
0 => (1, 1, 1),
28-
1 => (64, 1, 1),
29-
2 => (1, 64, 1),
30-
3 => (8, 8, 1),
31-
4 => (1, 1, 64),
32-
5 => (8, 1, 8),
33-
6 => (1, 8, 8),
34-
7 => (4, 4, 4),
27+
// zyx => Z Y X
28+
0b000 => (1, 1, 1),
29+
0b001 => (1, 1, 64),
30+
0b010 => (1, 64, 1),
31+
0b011 => (1, 8, 8),
32+
0b100 => (64, 1, 1),
33+
0b101 => (8, 1, 8),
34+
0b110 => (8, 8, 1),
35+
0b111 => (4, 4, 4),
3536
_ => throw new UnreachableException()
3637
};
3738

@@ -45,6 +46,7 @@ public override ComputeShaderEffect GetEffect(ComputeShader shader, int threadCo
4546

4647
return effectArray[index]!;
4748
}
49+
4850
public int GetIndexForThreadCount(int countX, int countY, int countZ)
4951
{
5052
int index = 0;
@@ -62,4 +64,4 @@ public int GetIndexForThreadCount(int countX, int countY, int countZ)
6264
}
6365
return index;
6466
}
65-
}
67+
}

src/SimulationFramework.OpenGL/Shaders/GLSLExpressionEmitter.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ public override ShaderExpression VisitShaderIntrinsicCall(ShaderIntrinsicCall ex
221221
if (expression.Intrinsic.Name == nameof(ShaderIntrinsics.GetElement))
222222
{
223223
expression.Arguments[0].Accept(this);
224-
writer.Write('[');
225-
expression.Arguments[1].Accept(this);
226-
writer.Write(']');
224+
for (int i = 1; i < expression.Arguments.Count; i++)
225+
{
226+
writer.Write('[');
227+
expression.Arguments[i].Accept(this);
228+
writer.Write(']');
229+
}
227230
return expression;
228231
}
229232

@@ -404,10 +407,22 @@ public override ShaderExpression VisitShaderIntrinsicCall(ShaderIntrinsicCall ex
404407

405408
if (expression.Intrinsic.Name == nameof(ShaderIntrinsics.Transform))
406409
{
410+
// glsl uses column major matrices, but System.Numerics uses row major.
411+
// emit multiplication backwards to account for this
407412
writer.Write('(');
408413
expression.Arguments[1].Accept(this);
409414
writer.Write(" * ");
410-
expression.Arguments[0].Accept(this);
415+
// matrix3x2 has an implied 3rd column (0, 0, 1), so we expand to vec3
416+
if (expression.Arguments[1].ExpressionType == ShaderType.Matrix3x2)
417+
{
418+
writer.Write("vec3(");
419+
expression.Arguments[0].Accept(this);
420+
writer.Write(", 1)");
421+
}
422+
else
423+
{
424+
expression.Arguments[0].Accept(this);
425+
}
411426
writer.Write(')');
412427
return expression;
413428
}

src/SimulationFramework.OpenGL/Shaders/ShaderTypeLayout.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Immutable;
55
using System.Diagnostics;
66
using System.Linq;
7+
using System.Numerics;
78
using System.Reflection;
89
using System.Runtime.CompilerServices;
910
using System.Runtime.InteropServices;
@@ -57,6 +58,8 @@ void CreateMemberFields(List<Field> fields, Type memberType, int memberOffset, s
5758
ShaderPrimitiveKind.Float2 or ShaderPrimitiveKind.Int2 or ShaderPrimitiveKind.UInt2 => 2,
5859
ShaderPrimitiveKind.Float3 or ShaderPrimitiveKind.Int3 or ShaderPrimitiveKind.UInt3 => 3,
5960
ShaderPrimitiveKind.Float4 or ShaderPrimitiveKind.Int4 or ShaderPrimitiveKind.UInt4 => 4,
61+
ShaderPrimitiveKind.Matrix3x2 => 8,
62+
ShaderPrimitiveKind.Matrix4x4 => 16,
6063
_ => throw new NotSupportedException("type " + memberType.Name + " is not supported!"),
6164
};
6265

0 commit comments

Comments
 (0)