Skip to content

Commit 27495e8

Browse files
fix: Console.log in drawindexed (#1771)
1 parent c1f831e commit 27495e8

File tree

3 files changed

+122
-37
lines changed

3 files changed

+122
-37
lines changed

apps/typegpu-docs/src/examples/tests/log-test/index.ts

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ const root = await tgpu.init({
1212
},
1313
});
1414

15+
// setup for render tests
16+
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
17+
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
18+
19+
const mainVertex = tgpu['~unstable'].vertexFn({
20+
in: { vertexIndex: d.builtin.vertexIndex },
21+
out: { pos: d.builtin.position },
22+
})((input) => {
23+
const positions = [
24+
d.vec2f(0, 0.5),
25+
d.vec2f(-0.5, -0.5),
26+
d.vec2f(0.5, -0.5),
27+
];
28+
29+
return { pos: d.vec4f(positions[input.vertexIndex], 0, 1) };
30+
});
31+
32+
const mainFragment = tgpu['~unstable'].fragmentFn({
33+
in: { pos: d.builtin.position },
34+
out: d.vec4f,
35+
})(({ pos }) => {
36+
console.log('X:', pos.x, 'Y:', pos.y);
37+
return d.vec4f(0.769, 0.392, 1.0, 1);
38+
});
39+
40+
const context = canvas.getContext('webgpu') as GPUCanvasContext;
41+
42+
context.configure({
43+
device: root.device,
44+
format: presentationFormat,
45+
alphaMode: 'premultiplied',
46+
});
47+
1548
// #region Example controls and cleanup
1649

1750
export const controls = {
@@ -131,7 +164,6 @@ export const controls = {
131164
for (let i = 0; i < 100; i++) {
132165
indexUniform.write(i);
133166
test.dispatch();
134-
console.log(`dispatched ${i}`);
135167
}
136168
},
137169
},
@@ -152,29 +184,6 @@ export const controls = {
152184
},
153185
'Render pipeline': {
154186
onButtonClick: () => {
155-
const mainVertex = tgpu['~unstable'].vertexFn({
156-
in: { vertexIndex: d.builtin.vertexIndex },
157-
out: { pos: d.builtin.position },
158-
})((input) => {
159-
const positions = [
160-
d.vec2f(0, 0.5),
161-
d.vec2f(-0.5, -0.5),
162-
d.vec2f(0.5, -0.5),
163-
];
164-
165-
return { pos: d.vec4f(positions[input.vertexIndex], 0, 1) };
166-
});
167-
168-
const mainFragment = tgpu['~unstable'].fragmentFn({
169-
in: { pos: d.builtin.position },
170-
out: d.vec4f,
171-
})(({ pos }) => {
172-
console.log('X:', pos.x, 'Y:', pos.y);
173-
return d.vec4f(0.769, 0.392, 1.0, 1);
174-
});
175-
176-
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
177-
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
178187
const context = canvas.getContext('webgpu') as GPUCanvasContext;
179188

180189
context.configure({
@@ -198,6 +207,27 @@ export const controls = {
198207
.draw(3);
199208
},
200209
},
210+
'Draw indexed': {
211+
onButtonClick: () => {
212+
const pipeline = root['~unstable']
213+
.withVertex(mainVertex, {})
214+
.withFragment(mainFragment, { format: presentationFormat })
215+
.createPipeline();
216+
217+
const indexBuffer = root
218+
.createBuffer(d.arrayOf(d.u32, 3), [0, 1, 2])
219+
.$usage('index');
220+
221+
pipeline
222+
.withIndexBuffer(indexBuffer)
223+
.withColorAttachment({
224+
view: context.getCurrentTexture().createView(),
225+
clearValue: [0, 0, 0, 0],
226+
loadOp: 'clear',
227+
storeOp: 'store',
228+
}).drawIndexed(3);
229+
},
230+
},
201231
'Too many logs': {
202232
onButtonClick: () =>
203233
prepareDispatch(root, (x) => {
@@ -207,18 +237,6 @@ export const controls = {
207237
console.log('Log 3 from thread', x);
208238
}).dispatch(16),
209239
},
210-
'Too much data': {
211-
onButtonClick: () => {
212-
try {
213-
prepareDispatch(root, () => {
214-
'kernel';
215-
console.log(d.mat4x4f(), d.mat4x4f(), 1);
216-
}).dispatch();
217-
} catch (err) {
218-
console.log(err);
219-
}
220-
},
221-
},
222240
};
223241

224242
export function onCleanup() {

packages/typegpu/src/core/pipeline/renderPipeline.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ class TgpuRenderPipelineImpl implements TgpuRenderPipeline {
618618
internals.priors.indexBuffer;
619619

620620
const pass = this.setupRenderPass();
621+
const { logResources } = internals.core.unwrap();
621622
const { branch } = internals.core.options;
622623

623624
if (isGPUBuffer(buffer)) {
@@ -641,6 +642,10 @@ class TgpuRenderPipelineImpl implements TgpuRenderPipeline {
641642

642643
pass.end();
643644

645+
if (logResources) {
646+
logDataFromGPU(logResources);
647+
}
648+
644649
internals.priors.performanceCallback
645650
? triggerPerformanceCallback({
646651
root: branch,

packages/typegpu/tests/examples/individual/log-test.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ describe('console log example', () => {
2424
'100 dispatches',
2525
'Varying size logs',
2626
'Render pipeline',
27+
'Draw indexed',
2728
'Too many logs',
2829
],
29-
expectedCalls: 11,
30+
expectedCalls: 12,
3031
}, device);
3132

3233
// the resolution variant for when 'shader-f16' is not enabled
@@ -1301,6 +1302,67 @@ describe('console log example', () => {
13011302
return vec4f(0.7689999938011169, 0.3919999897480011, 1, 1);
13021303
}
13031304
1305+
struct mainVertex_Output_1 {
1306+
@builtin(position) pos: vec4f,
1307+
}
1308+
1309+
struct mainVertex_Input_2 {
1310+
@builtin(vertex_index) vertexIndex: u32,
1311+
}
1312+
1313+
@vertex fn mainVertex_0(input: mainVertex_Input_2) -> mainVertex_Output_1 {
1314+
var positions = array<vec2f, 3>(vec2f(0, 0.5), vec2f(-0.5, -0.5), vec2f(0.5, -0.5));
1315+
return mainVertex_Output_1(vec4f(positions[input.vertexIndex], 0, 1));
1316+
}
1317+
1318+
@group(0) @binding(0) var<storage, read_write> indexBuffer_5: atomic<u32>;
1319+
1320+
struct SerializedLogData_7 {
1321+
id: u32,
1322+
serializedData: array<u32, 32>,
1323+
}
1324+
1325+
@group(0) @binding(1) var<storage, read_write> dataBuffer_6: array<SerializedLogData_7, 40>;
1326+
1327+
var<private> dataBlockIndex_8: u32;
1328+
1329+
var<private> dataByteIndex_9: u32;
1330+
1331+
fn nextByteIndex_12() -> u32{
1332+
let i = dataByteIndex_9;
1333+
dataByteIndex_9 = dataByteIndex_9 + 1u;
1334+
return i;
1335+
}
1336+
1337+
fn serializeF32_11(n: f32) {
1338+
dataBuffer_6[dataBlockIndex_8].serializedData[nextByteIndex_12()] = bitcast<u32>(n);
1339+
}
1340+
1341+
fn log1serializer_10(_arg_0: f32, _arg_1: f32) {
1342+
serializeF32_11(_arg_0);
1343+
serializeF32_11(_arg_1);
1344+
}
1345+
1346+
fn log1_4(_arg_0: f32, _arg_1: f32) {
1347+
dataBlockIndex_8 = atomicAdd(&indexBuffer_5, 1);
1348+
if (dataBlockIndex_8 >= 40) {
1349+
return;
1350+
}
1351+
dataBuffer_6[dataBlockIndex_8].id = 1;
1352+
dataByteIndex_9 = 0;
1353+
1354+
log1serializer_10(_arg_0, _arg_1);
1355+
}
1356+
1357+
struct mainFragment_Input_13 {
1358+
@builtin(position) pos: vec4f,
1359+
}
1360+
1361+
@fragment fn mainFragment_3(_arg_0: mainFragment_Input_13) -> @location(0) vec4f {
1362+
log1_4(_arg_0.pos.x, _arg_0.pos.y);
1363+
return vec4f(0.7689999938011169, 0.3919999897480011, 1, 1);
1364+
}
1365+
13041366
@group(0) @binding(0) var<uniform> sizeUniform_1: vec3u;
13051367
13061368
@group(0) @binding(1) var<storage, read_write> indexBuffer_4: atomic<u32>;

0 commit comments

Comments
 (0)