Skip to content

Commit 4f1c0d8

Browse files
committed
fix: Proper type coersion for vector & scalar operations
1 parent 974e9f9 commit 4f1c0d8

35 files changed

+114
-88
lines changed

packages/typegpu/src/data/matrix.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ function createMatSchema<
105105
const schema = Object.assign(construct, {
106106
[$internal]: {},
107107
type: options.type,
108+
primitive: f32,
108109
identity: identityFunctions[options.columns],
109110
translation: options.columns === 4 ? translation4 : undefined,
110111
scaling: options.columns === 4 ? scaling4 : undefined,

packages/typegpu/src/data/wgslTypes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ export interface Vec4b extends
12401240
*/
12411241
export interface Mat2x2f extends BaseData {
12421242
readonly type: 'mat2x2f';
1243+
readonly primitive: F32;
12431244

12441245
// Type-tokens, not available at runtime
12451246
readonly [$repr]: m2x2f;
@@ -1258,6 +1259,7 @@ export interface Mat2x2f extends BaseData {
12581259
*/
12591260
export interface Mat3x3f extends BaseData {
12601261
readonly type: 'mat3x3f';
1262+
readonly primitive: F32;
12611263

12621264
// Type-tokens, not available at runtime
12631265
readonly [$repr]: m3x3f;
@@ -1277,6 +1279,7 @@ export interface Mat3x3f extends BaseData {
12771279
*/
12781280
export interface Mat4x4f extends BaseData {
12791281
readonly type: 'mat4x4f';
1282+
readonly primitive: F32;
12801283

12811284
// Type-tokens, not available at runtime
12821285
readonly [$repr]: m4x4f;

packages/typegpu/src/tgsl/conversion.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,18 @@ export function unify<T extends (BaseData | UnknownData)[] | []>(
272272
return undefined;
273273
}
274274

275-
const conversion = getBestConversion(inTypes as BaseData[], restrictTo);
275+
const primitiveTypes = inTypes.map((type) =>
276+
isVec(type) || isMat(type) ? type.primitive : type as BaseData
277+
);
278+
279+
const conversion = getBestConversion(primitiveTypes, restrictTo);
276280
if (!conversion) {
277281
return undefined;
278282
}
279283

280-
return inTypes.map(() => conversion.targetType) as {
281-
[K in keyof T]: BaseData;
282-
};
284+
return inTypes.map((type) =>
285+
isVec(type) || isMat(type) ? type : conversion.targetType
286+
) as { [K in keyof T]: BaseData };
283287
}
284288

285289
export function convertToCommonType<T extends Snippet[]>(

packages/typegpu/tests/examples/individual/3d-fish.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ describe('3d fish example', () => {
186186
direction = (direction + (separation * fishBehavior.separationStr));
187187
direction = (direction + (alignment * fishBehavior.alignmentStr));
188188
direction = (direction + (cohesion * fishBehavior.cohesionStr));
189-
direction = (direction + (wallRepulsion * 1e-4));
190-
direction = (direction + (rayRepulsion * 0.0015));
189+
direction = (direction + (wallRepulsion * 1e-4f));
190+
direction = (direction + (rayRepulsion * 0.0015f));
191191
direction = (normalize(direction) * clamp(length((*fishData).direction), 0f, 0.01f));
192192
var translation = (direction * (min(999f, timePassed) / 8f));
193193
let nextFishData_1 = (&nextFishData[fishIndex]);
@@ -403,7 +403,7 @@ describe('3d fish example', () => {
403403
@fragment fn fragmentShader(input: fragmentShader_Input) -> @location(0) vec4f {
404404
var textureColorWithAlpha = textureSample(modelTexture, sampler_1, input.textureUV);
405405
var textureColor = textureColorWithAlpha.xyz;
406-
var ambient = (0.5 * (textureColor * vec3f(0.800000011920929, 0.800000011920929, 1)));
406+
var ambient = (0.5f * (textureColor * vec3f(0.800000011920929, 0.800000011920929, 1)));
407407
let cosTheta = dot(input.worldNormal, vec3f(-0.2357022613286972, 0.9428090453147888, -0.2357022613286972));
408408
var diffuse = (max(0f, cosTheta) * (textureColor * vec3f(0.800000011920929, 0.800000011920929, 1)));
409409
var viewSource = normalize((camera.position.xyz - input.worldPosition));

packages/typegpu/tests/examples/individual/ascii-filter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('ascii filter example', () => {
4646
@group(0) @binding(4) var<uniform> charsetExtended: u32;
4747
4848
fn characterFn(n: u32, p: vec2f) -> f32 {
49-
var pos = floor(((p * vec2f(-4, 4)) + 2.5));
49+
var pos = floor(((p * vec2f(-4, 4)) + 2.5f));
5050
if (((((pos.x < 0f) || (pos.x > 4f)) || (pos.y < 0f)) || (pos.y > 4f))) {
5151
return 0f;
5252
}
@@ -61,7 +61,7 @@ describe('ascii filter example', () => {
6161
}
6262
6363
@fragment fn fragmentFn(input: fragmentFn_Input) -> @location(0) vec4f {
64-
var uv2 = ((uvTransformBuffer * (input.uv - 0.5)) + 0.5);
64+
var uv2 = ((uvTransformBuffer * (input.uv - 0.5f)) + 0.5f);
6565
var textureSize = vec2f(textureDimensions(externalTexture));
6666
var pix = (uv2 * textureSize);
6767
let cellSize = f32(glyphSize);

packages/typegpu/tests/examples/individual/boids.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('boids example', () => {
130130
let angle = getRotationFromVelocity(input.velocity);
131131
var rotated = rotate(input.v, angle);
132132
var pos = vec4f((rotated + input.center), 0f, 1f);
133-
var color = vec4f(((sin((colorPalette + angle)) * 0.45) + 0.45), 1f);
133+
var color = vec4f(((sin((colorPalette + angle)) * 0.45f) + 0.45f), 1f);
134134
return mainVert_Output(pos, color);
135135
}
136136

packages/typegpu/tests/examples/individual/box-raytracing.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('box raytracing example', () => {
124124
@group(0) @binding(1) var<storage, read> boxMatrix: array<array<array<BoxStruct, 7>, 7>, 7>;
125125
126126
fn linearToSrgb(linear: vec3f) -> vec3f {
127-
return select((12.92 * linear), ((1.055 * pow(linear, vec3f(0.4166666567325592))) - vec3f(0.054999999701976776)), (linear > vec3f(0.0031308000907301903)));
127+
return select((12.92f * linear), ((1.055f * pow(linear, vec3f(0.4166666567325592))) - vec3f(0.054999999701976776)), (linear > vec3f(0.0031308000907301903)));
128128
}
129129
130130
struct fragmentFunction_Input {
@@ -134,12 +134,12 @@ describe('box raytracing example', () => {
134134
135135
@fragment fn fragmentFunction(input: fragmentFunction_Input) -> @location(0) vec4f {
136136
var boxSize3 = vec3f(uniforms.boxSize);
137-
var halfBoxSize3 = (0.5 * boxSize3);
138-
var halfCanvasDims = (0.5 * uniforms.canvasDims);
137+
var halfBoxSize3 = (0.5f * boxSize3);
138+
var halfCanvasDims = (0.5f * uniforms.canvasDims);
139139
let minDim = min(uniforms.canvasDims.x, uniforms.canvasDims.y);
140140
var viewCoords = ((input.position.xy - halfCanvasDims) / minDim);
141141
var ray = Ray(input.rayWorldOrigin, (uniforms.invViewMatrix * vec4f(normalize(vec3f(viewCoords, 1f)), 0f)).xyz);
142-
var bigBoxIntersection = getBoxIntersection(AxisAlignedBounds((-1 * halfBoxSize3), (vec3f(7) + halfBoxSize3)), ray);
142+
var bigBoxIntersection = getBoxIntersection(AxisAlignedBounds((-1f * halfBoxSize3), (vec3f(7) + halfBoxSize3)), ray);
143143
if (!bigBoxIntersection.intersects) {
144144
discard;;
145145
return vec4f();

packages/typegpu/tests/examples/individual/camera-thresholding.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('camera thresholding example', () => {
5050
}
5151
5252
@fragment fn mainFrag(input: mainFrag_Input) -> @location(0) vec4f {
53-
var uv2 = ((uvTransformUniform * (input.uv - 0.5)) + 0.5);
53+
var uv2 = ((uvTransformUniform * (input.uv - 0.5f)) + 0.5f);
5454
var col = textureSampleBaseClampToEdge(inputTexture, sampler_1, uv2);
5555
var ycbcr = (col.xyz * rgbToYcbcrMatrix);
5656
var colycbcr = (colorUniform * rgbToYcbcrMatrix);

packages/typegpu/tests/examples/individual/caustics.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('caustics example', () => {
3636
3737
fn tilePattern(uv: vec2f) -> f32 {
3838
var tiledUv = fract(uv);
39-
var proximity = abs(((tiledUv * 2) - 1));
39+
var proximity = abs(((tiledUv * 2f) - 1f));
4040
let maxProximity = max(proximity.x, proximity.y);
4141
return saturate((pow((1f - maxProximity), 0.6f) * 5f));
4242
}
@@ -73,7 +73,7 @@ describe('caustics example', () => {
7373
}
7474
7575
fn computeJunctionGradient(pos: vec3i) -> vec3f {
76-
randSeed3((1e-3 * vec3f(pos)));
76+
randSeed3((1e-3f * vec3f(pos)));
7777
return randOnUnitSphere();
7878
}
7979
@@ -84,7 +84,7 @@ describe('caustics example', () => {
8484
}
8585
8686
fn quinticInterpolationImpl(t: vec3f) -> vec3f {
87-
return ((t * (t * t)) * ((t * ((t * 6) - 15)) + 10));
87+
return ((t * (t * t)) * ((t * ((t * 6f) - 15f)) + 10f));
8888
}
8989
9090
fn sample(pos: vec3f) -> f32 {
@@ -109,9 +109,9 @@ describe('caustics example', () => {
109109
}
110110
111111
fn caustics(uv: vec2f, time2: f32, profile: vec3f) -> vec3f {
112-
let distortion = sample(vec3f((uv * 0.5), (time2 * 0.2f)));
112+
let distortion = sample(vec3f((uv * 0.5f), (time2 * 0.2f)));
113113
var uv2 = (uv + distortion);
114-
let noise = abs(sample(vec3f((uv2 * 5), time2)));
114+
let noise = abs(sample(vec3f((uv2 * 5f), time2)));
115115
return pow(vec3f((1f - noise)), profile);
116116
}
117117
@@ -130,15 +130,15 @@ describe('caustics example', () => {
130130
var albedo = mix(vec3f(0.10000000149011612), vec3f(1), tile);
131131
var cuv = vec2f(((_arg_0.uv.x * (pow((_arg_0.uv.y * 1.5f), 3f) + 0.1f)) * 5f), (pow((((_arg_0.uv.y * 1.5f) + 0.1f) * 1.5f), 3f) * 1f));
132132
var c1 = (caustics(cuv, (time * 0.2f), vec3f(4, 4, 1)) * vec3f(0.4000000059604645, 0.6499999761581421, 1));
133-
var c2 = (caustics((cuv * 2), (time * 0.4f), vec3f(16, 1, 4)) * vec3f(0.18000000715255737, 0.30000001192092896, 0.5));
133+
var c2 = (caustics((cuv * 2f), (time * 0.4f), vec3f(16, 1, 4)) * vec3f(0.18000000715255737, 0.30000001192092896, 0.5));
134134
var blendCoord = vec3f((_arg_0.uv * vec2f(5, 10)), ((time * 0.2f) + 5f));
135135
let blend = saturate((sample(blendCoord) + 0.3f));
136136
var noFogColor = (albedo * mix(vec3f(0.20000000298023224, 0.5, 1), (c1 + c2), blend));
137137
let fog = min((pow(_arg_0.uv.y, 0.5f) * 1.2f), 1f);
138138
var godRayUv = ((rotateXY(-0.3f) * _arg_0.uv) * vec2f(15, 3));
139139
let godRayFactor = pow(_arg_0.uv.y, 1f);
140140
var godRay1 = ((sample(vec3f(godRayUv, (time * 0.5f))) + 1f) * (vec3f(0.18000000715255737, 0.30000001192092896, 0.5) * godRayFactor));
141-
var godRay2 = ((sample(vec3f((godRayUv * 2), (time * 0.3f))) + 1f) * (vec3f(0.18000000715255737, 0.30000001192092896, 0.5) * (godRayFactor * 0.4f)));
141+
var godRay2 = ((sample(vec3f((godRayUv * 2f), (time * 0.3f))) + 1f) * (vec3f(0.18000000715255737, 0.30000001192092896, 0.5) * (godRayFactor * 0.4f)));
142142
var godRays = (godRay1 + godRay2);
143143
return vec4f((mix(noFogColor, vec3f(0.05000000074505806, 0.20000000298023224, 0.699999988079071), fog) + godRays), 1f);
144144
}"

packages/typegpu/tests/examples/individual/chroma-keying.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('chroma keying example', () => {
5050
}
5151
5252
@fragment fn fragment(_arg_0: fragment_Input) -> @location(0) vec4f {
53-
var uv2 = ((uvTransform * (_arg_0.uv - 0.5)) + 0.5);
53+
var uv2 = ((uvTransform * (_arg_0.uv - 0.5f)) + 0.5f);
5454
var col = textureSampleBaseClampToEdge(inputTexture, sampler_1, uv2);
5555
var ycbcr = (col.xyz * rgbToYcbcrMatrix);
5656
var colycbcr = (color * rgbToYcbcrMatrix);

0 commit comments

Comments
 (0)