Add Math.h with HLSL-style vector and matrix types#3
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
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.hwithfloat2/3/4andfloat4x4types plus scalar/vector/matrix helper functions. - Export/install the new header via
Sources/ObjectivelyGPU/Makefile.amand the umbrellaSources/ObjectivelyGPU.h. - Refactor
Examples/Hello.cto build model-view-projection usingfloat4x4_*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; |
| .offset = 0, | ||
| }, 1); | ||
| $(cmd, pushVertexUniformData, 0, matrixFinal, sizeof(matrixFinal)); | ||
| $(cmd, pushVertexUniformData, 0, matrixFinal.f, sizeof(matrixFinal)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds
Sources/ObjectivelyGPU/Math.h— a header-only GPU math library with HLSL-compatible type names, replacing the ad-hoc inline matrix math inHello.c.Types
float2/float3/float4.x/.y/.z/.wcomponent access, array accessor, and swizzle members (.xy,.xyz,.zw)float4x4m[col][row]); flat.f[16]and.cols[4]accessorsAPI
Scalar:
float_radians,float_degrees,float_clamp,float_saturate,float_lerp,float_smoothstep,float_signVectors:
float3_new,_add,_sub,_mul,_scale,_negate,_dot,_cross,_length,_distance,_normalize,_lerp,_reflect,_min,_max,_fma,_equal(same shape forfloat2andfloat4)Matrix:
float4x4_identity,_mul,_translation,_rotation,_scale,_scale3,_perspective,_ortho,_look_at,_inverse,_transform,_transform4Hello.c cleanup
The three hand-rolled C functions (
rotate_matrix,perspective_matrix,multiply_matrix) are replaced:Notes
<math.h>float_*/float2_*etc. prefixes avoid any collision with<math.h>or future libc additions