Skip to content

Commit 42525a8

Browse files
jdolanCopilot
andcommitted
Add Math.h with HLSL-style float2/3/4/float4x4 types
Header-only GPU math library with HLSL-compatible type names and float_/float2_/float3_/float4_/float4x4_ prefixed functions. - float2/3/4: constructors, arithmetic, dot/cross/normalize/length/ distance/lerp/reflect with swizzle accessors (.xy, .xyz, .zw) - float4x4: column-major (m[col][row]); identity, mul, translation, rotation, scale/scale3, perspective, ortho, look_at, inverse, transform/transform4 - Scalar helpers: float_radians/degrees/clamp/saturate/lerp/ smoothstep/sign - No SDL or Objectively dependency; replaces ad-hoc inline math in Hello.c Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 62b2ec3 commit 42525a8

4 files changed

Lines changed: 490 additions & 46 deletions

File tree

Examples/Hello.c

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -149,40 +149,6 @@ static void upload_vertex_buffer(const RenderDevice *renderDevice, SDL_GPUBuffer
149149
$(renderDevice, releaseTransferBuffer, transferBuffer);
150150
}
151151

152-
static void rotate_matrix(float angle, float x, float y, float z, float *r) {
153-
float radians = angle * SDL_PI_F / 180.0f;
154-
float c = SDL_cosf(radians), s = SDL_sinf(radians), c1 = 1.0f - c;
155-
float length = SDL_sqrtf(x*x + y*y + z*z);
156-
float u[3] = { x/length, y/length, z/length };
157-
for (int i = 0; i < 16; i++) r[i] = 0.0f;
158-
r[15] = 1.0f;
159-
for (int i = 0; i < 3; i++) {
160-
r[i*4 + (i+1)%3] = u[(i+2)%3] * s;
161-
r[i*4 + (i+2)%3] = -u[(i+1)%3] * s;
162-
}
163-
for (int i = 0; i < 3; i++)
164-
for (int j = 0; j < 3; j++)
165-
r[i*4+j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
166-
}
167-
168-
static void perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r) {
169-
float f = 1.0f / SDL_tanf((fovy / 180.0f) * SDL_PI_F * 0.5f);
170-
for (int i = 0; i < 16; i++) r[i] = 0.0f;
171-
r[0] = f / aspect; r[5] = f;
172-
r[10] = (znear + zfar) / (znear - zfar); r[11] = -1.0f;
173-
r[14] = (2.0f * znear * zfar) / (znear - zfar);
174-
}
175-
176-
static void multiply_matrix(const float *lhs, const float *rhs, float *r) {
177-
float tmp[16];
178-
for (int i = 0; i < 4; i++)
179-
for (int j = 0; j < 4; j++) {
180-
tmp[j*4+i] = 0.0f;
181-
for (int k = 0; k < 4; k++)
182-
tmp[j*4+i] += lhs[k*4+i] * rhs[j*4+k];
183-
}
184-
for (int i = 0; i < 16; i++) r[i] = tmp[i];
185-
}
186152

187153
int main(int argc, char **argv) {
188154
(void) argc;
@@ -343,17 +309,11 @@ int main(int argc, char **argv) {
343309
depthTexture = create_depth_texture(renderDevice, depthSize);
344310
}
345311

346-
float matrixModelView[16];
347-
float matrixRotate[16];
348-
float matrixPerspective[16];
349-
float matrixFinal[16];
350-
351-
rotate_matrix(angleX, 1.0f, 0.0f, 0.0f, matrixModelView);
352-
rotate_matrix(angleY, 0.0f, 1.0f, 0.0f, matrixRotate);
353-
multiply_matrix(matrixRotate, matrixModelView, matrixModelView);
354-
matrixModelView[14] -= 2.5f;
355-
perspective_matrix(45.0f, (float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.0f, matrixPerspective);
356-
multiply_matrix(matrixPerspective, matrixModelView, matrixFinal);
312+
float4x4 mv = float4x4_rotation(angleX, float3_new(1.f, 0.f, 0.f));
313+
mv = float4x4_mul(float4x4_rotation(angleY, float3_new(0.f, 1.f, 0.f)), mv);
314+
mv = float4x4_mul(float4x4_translation(float3_new(0.f, 0.f, -2.5f)), mv);
315+
const float4x4 proj = float4x4_perspective(45.f, (float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.f);
316+
const float4x4 matrixFinal = float4x4_mul(proj, mv);
357317

358318
SDL_GPUColorTargetInfo colorTarget = {
359319
.texture = swapchain.texture,
@@ -376,7 +336,7 @@ int main(int argc, char **argv) {
376336
.buffer = vertexBuffer,
377337
.offset = 0,
378338
}, 1);
379-
$(cmd, pushVertexUniformData, 0, matrixFinal, sizeof(matrixFinal));
339+
$(cmd, pushVertexUniformData, 0, matrixFinal.f, sizeof(matrixFinal));
380340
$(renderPass, drawPrimitives, (Uint32) SDL_arraysize(vertex_data), 1, 0, 0);
381341
release(renderPass);
382342

Sources/ObjectivelyGPU.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
#include <ObjectivelyGPU/CommandBuffer.h>
2727
#include <ObjectivelyGPU/ComputePass.h>
2828
#include <ObjectivelyGPU/CopyPass.h>
29+
#include <ObjectivelyGPU/Math.h>
2930
#include <ObjectivelyGPU/RenderDevice.h>
3031
#include <ObjectivelyGPU/RenderPass.h>

Sources/ObjectivelyGPU/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pkginclude_HEADERS = \
1010
CommandBuffer.h \
1111
ComputePass.h \
1212
CopyPass.h \
13+
Math.h \
1314
RenderDevice.h \
1415
RenderPass.h \
1516
Types.h

0 commit comments

Comments
 (0)