Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions packages/typegpu-noise/src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,34 @@ import { randomGeneratorSlot } from './generator.ts';
const TWO_PI = Math.PI * 2;
const EPS = 1e-7; // don't ever get any lower than this

const seedNotEmpty = tgpu.comptime(
const warnIfNotProvided = tgpu.comptime(
(seedFnName: keyof typeof randomGeneratorSlot.$) => {
if (randomGeneratorSlot.$[seedFnName]) {
return true;
if (!randomGeneratorSlot.$[seedFnName]) {
console.warn(`Called \`randf.${seedFnName}\`, but it wasn't provided`);
}
console.warn(`Called \`randf.${seedFnName}\`, but it wasn't provided`);
return false;

return undefined;
},
);

export const randSeed = tgpu.fn([d.f32])((seed) => {
if (seedNotEmpty('seed')) {
// @ts-expect-error trust me
randomGeneratorSlot.$.seed(seed);
}
warnIfNotProvided('seed');
randomGeneratorSlot.$.seed ? randomGeneratorSlot.$.seed(seed) : undefined;
});

export const randSeed2 = tgpu.fn([d.vec2f])((seed) => {
if (seedNotEmpty('seed2')) {
// @ts-expect-error trust me
randomGeneratorSlot.$.seed2(seed);
}
warnIfNotProvided('seed2');
randomGeneratorSlot.$.seed2 ? randomGeneratorSlot.$.seed2(seed) : undefined;
});

export const randSeed3 = tgpu.fn([d.vec3f])((seed) => {
if (seedNotEmpty('seed3')) {
// @ts-expect-error trust me
randomGeneratorSlot.$.seed3(seed);
}
warnIfNotProvided('seed3');
randomGeneratorSlot.$.seed3 ? randomGeneratorSlot.$.seed3(seed) : undefined;
});

export const randSeed4 = tgpu.fn([d.vec4f])((seed) => {
if (seedNotEmpty('seed4')) {
// @ts-expect-error trust me
randomGeneratorSlot.$.seed4(seed);
}
warnIfNotProvided('seed4');
randomGeneratorSlot.$.seed4 ? randomGeneratorSlot.$.seed4(seed) : undefined;
});

export const randFloat01: TgpuFn<() => d.F32> = tgpu
Expand Down
17 changes: 12 additions & 5 deletions packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function operatorToType<

const unaryOpCodeToCodegen = {
'-': neg[$gpuCallable].call,
'void': () => snip('', wgsl.Void, 'constant'),
'void': () => snip(undefined, wgsl.Void, 'constant'),
} satisfies Partial<
Record<tinyest.UnaryOperator, (...args: never[]) => unknown>
>;
Expand Down Expand Up @@ -195,6 +195,7 @@ class WgslGenerator implements ShaderGenerator {
try {
this.ctx.indent();
const body = statements.map((statement) => this.statement(statement))
.filter((statement) => statement.length > 0)
.join('\n');
this.ctx.dedent();
return `{
Expand Down Expand Up @@ -263,6 +264,10 @@ ${this.ctx.pre}}`;
if (!id) {
throw new Error('Cannot resolve an empty identifier');
}
if (id === 'undefined') {
return snip(undefined, wgsl.Void, 'constant');
}

const res = this.ctx.getById(id);

if (!res) {
Expand Down Expand Up @@ -770,8 +775,9 @@ ${this.ctx.pre}}`;
statement: tinyest.Statement,
): string {
if (typeof statement === 'string') {
const resolved = this.ctx.resolve(this.identifier(statement).value).value;
return resolved.length === 0 ? '' : `${this.ctx.pre}${resolved};`;
const id = this.identifier(statement);
const resolved = id.value && this.ctx.resolve(id.value).value;
return resolved ? `${this.ctx.pre}${resolved};` : '';
}

if (typeof statement === 'boolean') {
Expand Down Expand Up @@ -1047,8 +1053,9 @@ ${this.ctx.pre}else ${alternate}`;
return `${this.ctx.pre}break;`;
}

const resolved = this.ctx.resolve(this.expression(statement).value).value;
return resolved.length === 0 ? '' : `${this.ctx.pre}${resolved};`;
const expr = this.expression(statement);
const resolved = expr.value && this.ctx.resolve(expr.value).value;
return resolved ? `${this.ctx.pre}${resolved};` : '';
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/typegpu/tests/computePipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,6 @@ describe('TgpuComputePipeline', () => {
{
a[1i] = 1h;
}

}"
`);
});
Expand Down
1 change: 0 additions & 1 deletion packages/typegpu/tests/declare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ struct Output {
"@group(0) @binding(0) var<uniform> val: f32;

fn main() -> f32 {

return 2f;
}"
`);
Expand Down
4 changes: 1 addition & 3 deletions packages/typegpu/tests/examples/individual/3d-fish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ describe('3d fish example', () => {
}
fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}
fn sample() -> f32 {
Expand Down
4 changes: 1 addition & 3 deletions packages/typegpu/tests/examples/individual/caustics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ describe('caustics example', () => {
}

fn randSeed3(seed: vec3f) {
{
seed3(seed);
}
seed3(seed);
}

fn sample_1() -> f32 {
Expand Down
4 changes: 1 addition & 3 deletions packages/typegpu/tests/examples/individual/clouds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ describe('clouds example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

@group(0) @binding(0) var<uniform> resolutionUniform: vec2f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ describe('fluid double buffering example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

@group(0) @binding(2) var<storage, read> gridBetaBuffer: array<vec4f, 1048576>;
Expand Down Expand Up @@ -285,9 +283,7 @@ describe('fluid double buffering example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

@group(0) @binding(2) var<storage, read> gridAlphaBuffer: array<vec4f, 1048576>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ describe('jelly-slider example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

struct Camera {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ describe('jelly switch example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

struct Camera {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ describe('jump flood (voronoi) example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

fn sample() -> f32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ describe('perlin noise example', () => {
}

fn randSeed3(seed: vec3f) {
{
seed3(seed);
}
seed3(seed);
}

fn sample() -> f32 {
Expand Down
52 changes: 13 additions & 39 deletions packages/typegpu/tests/examples/individual/probability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -83,9 +81,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -129,9 +125,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -176,9 +170,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -222,9 +214,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -263,9 +253,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -310,9 +298,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -374,9 +360,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -430,9 +414,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -476,9 +458,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -521,9 +501,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -571,9 +549,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -622,9 +598,7 @@ describe('probability distribution plot example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down
12 changes: 3 additions & 9 deletions packages/typegpu/tests/examples/individual/slime-mold-3d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ describe('slime mold 3d example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

fn sample() -> f32 {
Expand Down Expand Up @@ -135,9 +133,7 @@ describe('slime mold 3d example', () => {
}

fn randSeed(seed: f32) {
{
seed_1(seed);
}
seed_1(seed);
}

@group(1) @binding(1) var oldState: texture_storage_3d<r32float, read>;
Expand Down Expand Up @@ -329,9 +325,7 @@ describe('slime mold 3d example', () => {
}

fn randSeed2(seed: vec2f) {
{
seed2(seed);
}
seed2(seed);
}

struct Camera {
Expand Down
Loading