Skip to content

Add Math.h with HLSL-style vector and matrix types#3

Merged
jdolan merged 1 commit into
mainfrom
jdolan-add-math-header
Jun 27, 2026
Merged

Add Math.h with HLSL-style vector and matrix types#3
jdolan merged 1 commit into
mainfrom
jdolan-add-math-header

Conversation

@jdolan

@jdolan jdolan commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Overview

Adds Sources/ObjectivelyGPU/Math.h — a header-only GPU math library with HLSL-compatible type names, replacing the ad-hoc inline matrix math in Hello.c.

Types

Type Description
float2 / float3 / float4 Single-precision vector types with .x/.y/.z/.w component access, array accessor, and swizzle members (.xy, .xyz, .zw)
float4x4 Column-major 4×4 matrix (m[col][row]); flat .f[16] and .cols[4] accessors

API

Scalar: float_radians, float_degrees, float_clamp, float_saturate, float_lerp, float_smoothstep, float_sign

Vectors: float3_new, _add, _sub, _mul, _scale, _negate, _dot, _cross, _length, _distance, _normalize, _lerp, _reflect, _min, _max, _fma, _equal (same shape for float2 and float4)

Matrix: float4x4_identity, _mul, _translation, _rotation, _scale, _scale3, _perspective, _ortho, _look_at, _inverse, _transform, _transform4

Hello.c cleanup

The three hand-rolled C functions (rotate_matrix, perspective_matrix, multiply_matrix) are replaced:

// Before: 46 lines of raw float[16] arithmetic
// After:
float4x4 mv = float4x4_rotation(angleX, float3_new(1.f, 0.f, 0.f));
mv = float4x4_mul(float4x4_rotation(angleY, float3_new(0.f, 1.f, 0.f)), mv);
mv = float4x4_mul(float4x4_translation(float3_new(0.f, 0.f, -2.5f)), mv);
const float4x4 proj = float4x4_perspective(45.f, aspect, 0.01f, 100.f);
const float4x4 matrixFinal = float4x4_mul(proj, mv);

Notes

  • No SDL or Objectively dependency — pure C99 + <math.h>
  • Type names match HLSL intrinsics exactly, so the mental model transfers directly between shader and host code
  • float_* / float2_* etc. prefixes avoid any collision with <math.h> or future libc additions

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>
Copilot AI review requested due to automatic review settings June 27, 2026 01:18
@jdolan
jdolan merged commit 7f825a2 into main Jun 27, 2026
3 of 4 checks passed
@jdolan
jdolan deleted the jdolan-add-math-header branch June 27, 2026 01:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a header-only math utility (Math.h) providing HLSL-style vector/matrix types and helpers, and updates the Hello example to use the new float4x4 APIs instead of ad-hoc matrix code.

Changes:

  • Add Sources/ObjectivelyGPU/Math.h with float2/3/4 and float4x4 types plus scalar/vector/matrix helper functions.
  • Export/install the new header via Sources/ObjectivelyGPU/Makefile.am and the umbrella Sources/ObjectivelyGPU.h.
  • Refactor Examples/Hello.c to build model-view-projection using float4x4_* helpers and upload the matrix as uniform data.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
Sources/ObjectivelyGPU/Math.h Adds HLSL-style vector/matrix types and inline math helpers for host-side GPU code.
Sources/ObjectivelyGPU/Makefile.am Installs/exports Math.h as part of the public headers.
Sources/ObjectivelyGPU.h Exposes Math.h via the umbrella include for consumers.
Examples/Hello.c Switches the example’s matrix construction to the new float4x4 API and uploads the final matrix.

Comment on lines +464 to +470
static inline float3 float4x4_transform(float4x4 m, float3 v) {
return float3_new(
v.x*m.m[0][0] + v.y*m.m[1][0] + v.z*m.m[2][0] + m.m[3][0],
v.x*m.m[0][1] + v.y*m.m[1][1] + v.z*m.m[2][1] + m.m[3][1],
v.x*m.m[0][2] + v.y*m.m[1][2] + v.z*m.m[2][2] + m.m[3][2]
);
}
Comment on lines +435 to +439
float det = b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06;
if (!det) {
return float4x4_identity();
}
det = 1.f / det;
Comment thread Examples/Hello.c
.offset = 0,
}, 1);
$(cmd, pushVertexUniformData, 0, matrixFinal, sizeof(matrixFinal));
$(cmd, pushVertexUniformData, 0, matrixFinal.f, sizeof(matrixFinal));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants