Skip to content

Commit 6f800a2

Browse files
Added overloaded operators in Slang to match GLSL matrix operations
1 parent 225240c commit 6f800a2

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

lvk/vulkan/VulkanClasses.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5840,6 +5840,14 @@ lvk::ShaderModuleState lvk::VulkanContext::createShaderModuleFromSlang(ShaderSta
58405840
}
58415841
};
58425842

5843+
// overloaded operators to mimic GLSL matrix operations
5844+
sourcePatched +=
5845+
"float2x2 operator*(float2x2 a, float2x2 b) { return mul(b, a); }\n"
5846+
"float2 operator*(float2x2 a, float2 b) { return mul(b, a); }\n"
5847+
"float3x3 operator*(float3x3 a, float3x3 b) { return mul(b, a); }\n"
5848+
"float3 operator*(float3x3 a, float3 b) { return mul(b, a); }\n"
5849+
"float4x4 operator*(float4x4 a, float4x4 b) { return mul(b, a); }\n"
5850+
"float4 operator*(float4x4 a, float4 b) { return mul(b, a); }\n";
58435851
// bindless texture and sampler arrays
58445852
sourcePatched +=
58455853
"[[vk::binding(0, 0)]] Texture2D kTextures2D[];\n"

samples/002_RenderToCubeMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static const float3 vertices[8] = {
113113
VertexStageOutput vertexMain(uint vertexID : SV_VertexID) {
114114
VertexStageOutput out;
115115
float3 v = vertices[vertexID];
116-
out.sv_Position = mul(float4(v, 1.0), pc.mvp);
116+
out.sv_Position = pc.mvp * float4(v, 1.0);
117117
out.dir = v;
118118
out.textureId = pc.texture0;
119119
return out;

samples/003_RenderToCubeMapSinglePass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static const float3 vertices[8] = {
111111
VertexStageOutput vertexMain(uint vertexID : SV_VertexID) {
112112
VertexStageOutput out;
113113
float3 v = vertices[vertexID];
114-
out.sv_Position = mul(float4(v, 1.0), pc.mvp);
114+
out.sv_Position = pc.mvp * float4(v, 1.0);
115115
out.dir = v;
116116
out.textureId = pc.texture0;
117117
return out;

samples/009_TriplanarMapping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ VertexStageOutput vertexMain(uint vertexID : SV_VertexID,
6464
6565
VertexStageOutput out;
6666
67-
out.sv_Position = mul(float4(v.x, v.y, v.z, 1.0), mul(model, mul(view, proj)));
67+
out.sv_Position = proj * view * model * float4(v.x, v.y, v.z, 1.0);
6868
out.color = float3(v.r, v.g, v.b);
6969
out.normal = normalize(float3(v.x, v.y, v.z)); // object space normal
7070

samples/RTX_001_Hello.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ void rayGenMain() {
3636
float2 pixelCenter = float2(launchID.xy) + float2(0.5, 0.5);
3737
float2 d = 2.0 * (pixelCenter / float2(launchSize.xy)) - 1.0;
3838
39-
float4 origin = mul(float4(0, 0, 0, 1), pc.cam->viewInverse);
40-
float4 target = mul(float4(d, 1, 1), pc.cam->projInverse);
41-
float4 direction = mul(float4(normalize(target.xyz), 0), pc.cam->viewInverse);
39+
float4 origin = pc.cam->viewInverse * float4(0, 0, 0, 1);
40+
float4 target = pc.cam->projInverse * float4(d, 1, 1);
41+
float4 direction = pc.cam->viewInverse * float4(normalize(target.xyz), 0);
4242
4343
RayDesc ray;
4444
ray.Origin = origin.xyz;
@@ -65,7 +65,7 @@ void rayGenMain() {
6565
float2 rotate(float2 v, float angle) {
6666
float2x2 r = float2x2(cos(angle), -sin(angle),
6767
sin(angle), cos(angle));
68-
return mul(v-0.5*DispatchRaysDimensions().xy, r);
68+
return r * (v-0.5*DispatchRaysDimensions().xy);
6969
}
7070
7171
// helper function for GLSL-style mod() that works with negative numbers
@@ -121,8 +121,8 @@ void main() {
121121
vec2 pixelCenter = gl_LaunchIDEXT.xy + vec2(0.5);
122122
vec2 d = 2.0 * (pixelCenter / gl_LaunchSizeEXT.xy) - 1.0;
123123
124-
vec4 origin = cam.viewInverse * vec4(0,0,0,1);
125-
vec4 target = cam.projInverse * vec4(d, 1, 1);
124+
vec4 origin = cam.viewInverse * vec4(0,0,0,1);
125+
vec4 target = cam.projInverse * vec4(d, 1, 1);
126126
vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0);
127127
128128
payload = vec3(0.0);

samples/RTX_004_Textures.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ void rayGenMain() {
4646
float2 pixelCenter = float2(launchID.xy) + float2(0.5, 0.5);
4747
float2 d = 2.0 * (pixelCenter / float2(launchSize.xy)) - 1.0;
4848
49-
float4 origin = mul(float4(0, 0, 0, 1), pc.cam->viewInverse);
50-
float4 target = mul(float4(d, 1, 1), pc.cam->projInverse);
51-
float4 direction = mul(float4(normalize(target.xyz), 0), pc.cam->viewInverse);
49+
float4 origin = pc.cam->viewInverse * float4(0, 0, 0, 1);
50+
float4 target = pc.cam->projInverse * float4(d, 1, 1);
51+
float4 direction = pc.cam->viewInverse * float4(normalize(target.xyz), 0);
5252
5353
RayDesc ray;
5454
ray.Origin = origin.xyz;

0 commit comments

Comments
 (0)