Skip to content

Commit

Permalink
Add other resource types
Browse files Browse the repository at this point in the history
storageTextures, textures, samplers, externalTextures
  • Loading branch information
greggman committed Feb 1, 2024
1 parent 277d506 commit 05ed493
Show file tree
Hide file tree
Showing 2 changed files with 370 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/data-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export type ArrayDefinition = TypeDefinition & {
numElements: number,
};

export type TextureDefinition = TypeDefinition & {
type: string,
};

export type SamplerDefinition = TypeDefinition & {
type: string,
};

/**
* @group(x) @binding(y) var<...> definition
*/
Expand Down Expand Up @@ -79,6 +87,10 @@ export type EntryPoints = {
type ShaderDataDefinitions = {
uniforms: VariableDefinitions,
storages: VariableDefinitions,
samplers: VariableDefinitions,
textures: VariableDefinitions,
storageTextures: VariableDefinitions,
externalTextures: VariableDefinitions,
structs: StructDefinitions,
entryPoints: EntryPoints,
};
Expand Down Expand Up @@ -166,7 +178,7 @@ export function makeBindGroupLayoutDescriptors(

function getNamedVariables(reflect: WgslReflect, variables: VariableInfo[]): VariableDefinitions {
return Object.fromEntries(variables.map(v => {
const typeDefinition = addType(reflect, v.type, 0);
const typeDefinition = addVariableType(reflect, v, 0);
return [
v.name,
{
Expand Down Expand Up @@ -365,7 +377,11 @@ export function makeShaderDataDefinitions(code: string): ShaderDataDefinitions {
}));

const uniforms = getNamedVariables(reflect, reflect.uniforms);
const storages = getNamedVariables(reflect, reflect.storage);
const storages = getNamedVariables(reflect, reflect.storage.filter(v => v.resourceType === ResourceType.Storage));
const storageTextures = getNamedVariables(reflect, reflect.storage.filter(v => v.resourceType === ResourceType.StorageTexture));
const textures = getNamedVariables(reflect, reflect.textures.filter(v => v.type.name !== 'texture_external'));
const externalTextures = getNamedVariables(reflect, reflect.textures.filter(v => v.type.name === 'texture_external'));
const samplers = getNamedVariables(reflect, reflect.samplers);

const entryPoints: EntryPoints = {
...addEntryPoints(reflect.entry.vertex, GPUShaderStage.VERTEX),
Expand All @@ -374,8 +390,12 @@ export function makeShaderDataDefinitions(code: string): ShaderDataDefinitions {
};

return {
externalTextures,
samplers,
structs,
storages,
storageTextures,
textures,
uniforms,
entryPoints,
};
Expand Down Expand Up @@ -435,6 +455,26 @@ function assert(cond: boolean, msg = '') {
]
*/

function addVariableType(reflect: WgslReflect, v: VariableInfo, offset: number):
StructDefinition |
IntrinsicDefinition |
ArrayDefinition |
TextureDefinition |
SamplerDefinition {
switch (v.resourceType) {
case ResourceType.Uniform:
case ResourceType.Storage:
case ResourceType.StorageTexture:
return addType(reflect, v.type, offset);
default:
return {
size: 0,
type: v.type.name,
};
}
}

function addType(reflect: WgslReflect, typeInfo: TypeInfo, offset: number):
StructDefinition |
IntrinsicDefinition |
Expand Down
Loading

0 comments on commit 05ed493

Please sign in to comment.