Skip to content

Commit

Permalink
Fix includes and f32 buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidar committed Dec 22, 2024
1 parent cc27952 commit 5c362c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
18 changes: 14 additions & 4 deletions lib/wgputoy/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ class BufferBinding<H> implements Binding {
// bind: (buffer: GPUBuffer) => GPUBufferBinding;
decl: string;

constructor(params) {
constructor(params: {
host: H;
device: GPUBuffer;
layout: GPUBufferBindingLayout;
decl: string;
}) {
this.host = params.host;
this.device = params.device;
this.layout = params.layout;
Expand Down Expand Up @@ -124,7 +129,12 @@ class TextureBinding implements Binding {
layout: GPUTextureBindingLayout;
decl: string;

constructor(params) {
constructor(params: {
device: GPUTexture;
view: GPUTextureView;
layout: GPUTextureBindingLayout;
decl: string;
}) {
this.device = params.device;
this.view = params.view;
this.layout = params.layout;
Expand Down Expand Up @@ -383,7 +393,7 @@ export class Bindings {
size: USER_DATA_BYTES,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
}),
layout: { ...storageBuffer, buffer: { type: 'read-only-storage' } },
layout: { ...storageBuffer, type: 'read-only-storage' },
// bind: (buffer) => ({ offset: 0, size: USER_DATA_BYTES }),
decl: 'var<storage,read> data: Data'
});
Expand Down Expand Up @@ -454,7 +464,7 @@ export class Bindings {
dimension: '2d-array'
}),
layout: {
sampleType: 'float',
sampleType: passF32 ? 'unfilterable-float' : 'float',
viewDimension: '2d-array',
multisampled: false
},
Expand Down
10 changes: 4 additions & 6 deletions lib/wgputoy/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { fetchInclude, parseUint32, WGSLError } from './utils';

// Regular expressions for preprocessing
const RE_COMMENT = /(\/\/.*|\/\*[\s\S]*?\*\/)/g;
const RE_QUOTES = /"((?:[^\\"]|\\.)*)"/g;
const RE_CHEVRONS = /<(.*)>/;
const RE_WORD = /[a-zA-Z_][a-zA-Z0-9_]*/g;

const STRING_MAX_LEN = 20;
Expand Down Expand Up @@ -130,7 +128,7 @@ export class Preprocessor {
// Handle string literals if enabled
if (this.specialStrings) {
let error: WGSLError | null = null;
line = line.replace(RE_QUOTES, match => {
line = line.replace(/"((?:[^\\"]|\\.)*)"/g, match => {
try {
const unescaped = JSON.parse(match) as string;
const chars = Array.from(unescaped).map(c => c.charCodeAt(0));
Expand Down Expand Up @@ -173,17 +171,17 @@ export class Preprocessor {
throw new WGSLError('Invalid #include syntax', lineNum);
}

const nameMatcher = tokens[1].match(RE_QUOTES) || tokens[1].match(RE_CHEVRONS);
const nameMatcher = tokens[1].match(/"(.*)"/) || tokens[1].match(/<(.*)>/);
if (!nameMatcher) {
throw new WGSLError('Path must be enclosed in quotes or chevrons', lineNum);
}

const name = nameMatcher[1];
if (RE_CHEVRONS.test(tokens[1]) && name === 'string') {
if (/<.*>/.test(tokens[1]) && name === 'string') {
this.specialStrings = true;
}

const includePath = RE_CHEVRONS.test(tokens[1]) ? `std/${name}` : name;
const includePath = /<.*>/.test(tokens[1]) ? `std/${name}` : name;
const includeContent = await fetchInclude(includePath);

if (!includeContent) {
Expand Down

0 comments on commit 5c362c8

Please sign in to comment.