Skip to content
Merged
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
22 changes: 13 additions & 9 deletions packages/typegpu-gl/src/glslGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { NodeTypeCatalog as NODE } from 'tinyest';
import type { Return } from 'tinyest';
import tgpu, { d, ShaderGenerator, WgslGenerator } from 'typegpu';

type ResolutionCtx = ShaderGenerator.ResolutionCtx;

const UnknownData: typeof ShaderGenerator.UnknownData = ShaderGenerator.UnknownData;
import { tgpu, d } from 'typegpu';
import {
WgslGenerator,
UnknownData,
getName,
type ResolutionCtx,
type TgpuShaderStage,
type FunctionDefinitionOptions,
} from 'typegpu/~internal';

// ----------
// WGSL → GLSL type name mapping
Expand Down Expand Up @@ -52,7 +56,7 @@ export function translateWgslTypeToGlsl(wgslType: string): string {
* @returns The resolved struct name.
*/
function resolveStruct(ctx: ResolutionCtx, struct: d.WgslStruct) {
const id = ctx.makeUniqueIdentifier(ShaderGenerator.getName(struct), 'global');
const id = ctx.makeUniqueIdentifier(getName(struct), 'global');

ctx.addDeclaration(`\
struct ${id} {
Expand All @@ -77,7 +81,7 @@ interface EntryFnState {
* and overrides variable declaration emission to use `type name = rhs` syntax.
*/
export class GlslGenerator extends WgslGenerator {
#functionType: ShaderGenerator.TgpuShaderStage | 'normal' | undefined;
#functionType: TgpuShaderStage | 'normal' | undefined;
#entryFnState: EntryFnState | undefined;

override typeAnnotation(data: d.BaseData): string {
Expand All @@ -100,7 +104,7 @@ export class GlslGenerator extends WgslGenerator {
override _emitVarDecl(
_keyword: 'var' | 'let' | 'const',
name: string,
dataType: d.BaseData | ShaderGenerator.UnknownData,
dataType: d.BaseData | UnknownData,
rhsStr: string,
): string {
const glslTypeName = dataType !== UnknownData ? this.ctx.resolve(dataType).value : 'auto';
Expand Down Expand Up @@ -185,7 +189,7 @@ export class GlslGenerator extends WgslGenerator {
return super._return(statement);
}

override functionDefinition(options: ShaderGenerator.FunctionDefinitionOptions): string {
override functionDefinition(options: FunctionDefinitionOptions): string {
if (options.functionType !== 'normal') {
this.ctx.reserveIdentifier('gl_Position', 'global');
}
Expand Down
3 changes: 2 additions & 1 deletion packages/typegpu-gl/src/tgpuRootWebGL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Compute operations, storage buffers, textures, etc. throw WebGLFallbackUnsupportedError.
*/

import tgpu, { d, ShaderGenerator, type TgpuFragmentFn, type TgpuVertexFn } from 'typegpu';
import tgpu, { d, type TgpuFragmentFn, type TgpuVertexFn } from 'typegpu';
import type { ShaderGenerator } from 'typegpu/~internal';
import glslGenerator, { translateWgslTypeToGlsl } from './glslGenerator.ts';

// ----------
Expand Down
9 changes: 9 additions & 0 deletions packages/typegpu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"./data": "./src/data/index.ts",
"./std": "./src/std/index.ts",
"./common": "./src/common/index.ts",
"./~internal": "./src/internal.ts",
"./$built$": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
Expand All @@ -49,6 +50,10 @@
"./common/$built$": {
"types": "./dist/common/index.d.ts",
"default": "./dist/common/index.js"
},
"./~internal/$built$": {
"types": "./dist/internal.d.ts",
"default": "./dist/internal.js"
}
},
"publishConfig": {
Expand All @@ -71,6 +76,10 @@
"./common": {
"types": "./dist/common/index.d.ts",
"default": "./dist/common/index.js"
},
"./~internal": {
"types": "./dist/internal.d.ts",
"default": "./dist/internal.js"
}
},
"linkDirectory": false,
Expand Down
11 changes: 9 additions & 2 deletions packages/typegpu/src/indexNamedExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ export { isTgpuFragmentFn } from './core/function/tgpuFragmentFn.ts';
export { isTgpuVertexFn } from './core/function/tgpuVertexFn.ts';
export { isTgpuComputeFn } from './core/function/tgpuComputeFn.ts';
export { isVariable } from './core/variable/tgpuVariable.ts';
export { ShaderGenerator } from './tgsl/shaderGenerator.ts';
export { WgslGenerator } from './tgsl/wgslGenerator.ts';
export type {
/** @deprecated Import from 'typegpu/~internal' instead */ ShaderGenerator,
} from './tgsl/shaderGenerator.ts';
export {
/** @deprecated Import from 'typegpu/~internal' instead */ WgslGenerator,
} from './tgsl/wgslGenerator.ts';
export { readFromArrayBuffer, writeToArrayBuffer } from './data/dataIO.ts';
export { patchArrayBuffer } from './data/partialIO.ts';

Expand Down Expand Up @@ -60,6 +64,9 @@ export type {
ValidUsagesFor,
Vertex,
VertexFlag,
BufferWriteOptions,
BufferInitCallback,
BufferInitialData,
} from './core/buffer/buffer.ts';
export type {
TgpuBufferMutable,
Expand Down
18 changes: 18 additions & 0 deletions packages/typegpu/src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Each export here is available as a member on the 'typegpu/~internal' import.

export { abstractInt, abstractFloat } from './data/numeric.ts';
export { UnknownData } from './data/dataTypes.ts';
export { getName } from './shared/meta.ts';
export { WgslGenerator } from './tgsl/wgslGenerator.ts';
export { snip } from './data/snippet.ts';

// types
export type { ResolutionCtx, FunctionArgument, TgpuShaderStage } from './types.ts';
export type { Snippet, ResolvedSnippet, Origin } from './data/snippet.ts';

export type {
ShaderGenerator,
FunctionDefinitionOptions,
ConstantDefinitionOptions,
VariableDefinitionOptions,
} from './tgsl/shaderGenerator.ts';
3 changes: 1 addition & 2 deletions packages/typegpu/src/resolutionCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import type {
TgpuShaderStage,
Wgsl,
} from './types.ts';
import { CodegenState, isSelfResolvable, NormalState } from './types.ts';
import { CodegenState, isSelfResolvable, NormalState, type FunctionArgument } from './types.ts';
import type { WgslEnableExtension } from './wgslExtensions.ts';
import { getName, hasTinyestMetadata, isNamable, setName } from './shared/meta.ts';
import { FuncParameterType } from 'tinyest';
Expand All @@ -59,7 +59,6 @@ import { isTgpuFn } from './core/function/tgpuFn.ts';
import type { IOData } from './core/function/fnTypes.ts';
import { AutoStruct } from './data/autoStruct.ts';
import { EntryInputRouter } from './core/function/entryInputRouter.ts';
import type { FunctionArgument } from './tgsl/shaderGenerator_members.ts';
import { validateIdentifier, sanitizePrimer, bannedTokens } from './nameUtils.ts';

/**
Expand Down
35 changes: 28 additions & 7 deletions packages/typegpu/src/tgsl/shaderGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import type { Block } from 'tinyest';
import type { BaseData } from '../data/wgslTypes.ts';
import type { GenerationCtx } from './generationHelpers.ts';
import type { ResolvedSnippet, Snippet } from '../data/snippet.ts';
import type {
ConstantDefinitionOptions,
FunctionDefinitionOptions,
VariableDefinitionOptions,
} from './shaderGenerator_members.ts';
import type { VariableScope } from '../core/variable/tgpuVariable.ts';
import type { BindableBufferUsage, FunctionArgument, TgpuShaderStage } from '../types.ts';

export interface FunctionDefinitionOptions {
readonly functionType: 'normal' | TgpuShaderStage;
readonly name: string;
readonly workgroupSize?: readonly number[] | undefined;
readonly args: readonly FunctionArgument[];
readonly body: Block;

determineReturnType(): BaseData;
}

export interface ConstantDefinitionOptions {
readonly id: string;
readonly dataType: BaseData;
readonly init: Snippet;
}

export interface VariableDefinitionOptions {
readonly scope: VariableScope | BindableBufferUsage | 'handle';
readonly id: string;
readonly dataType: BaseData;
readonly init: Snippet | undefined;
readonly group?: string | undefined;
readonly binding?: number | undefined;
}

/**
* **NOTE: This is an unstable API and may change in the future.**
Expand All @@ -24,5 +47,3 @@ export interface ShaderGenerator {
typeAnnotation(schema: BaseData): string;
numericLiteral(value: number, schema: BaseData): ResolvedSnippet;
}

export * as ShaderGenerator from './shaderGenerator_members.ts';
38 changes: 0 additions & 38 deletions packages/typegpu/src/tgsl/shaderGenerator_members.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import {
} from './generationHelpers.ts';
import { accessIndex } from './accessIndex.ts';
import { accessProp } from './accessProp.ts';
import type { ShaderGenerator } from './shaderGenerator.ts';
import type {
ShaderGenerator,
ConstantDefinitionOptions,
FunctionDefinitionOptions,
VariableDefinitionOptions,
} from './shaderGenerator.ts';
import { resolveData } from '../core/resolve/resolveData.ts';
import { createPtrFromOrigin, implicitFrom, ptrFn } from '../data/ptr.ts';
import { _ref, RefOperator } from '../data/ref.ts';
Expand All @@ -49,11 +54,6 @@ import type { ExternalMap } from '../core/resolve/externals.ts';
import * as forOfUtils from './forOfUtils.ts';
import { isTgpuRange } from '../std/range.ts';
import { stringifyNode } from '../shared/tseynit.ts';
import type {
ConstantDefinitionOptions,
FunctionDefinitionOptions,
VariableDefinitionOptions,
} from './shaderGenerator_members.ts';
import { getAttributesString } from '../data/attributes.ts';
import { validSelectBranchTypes } from '../std/boolean.ts';
import { isInfixDispatch } from './infixDispatch.ts';
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
import type { TgpuBindGroupLayout, TgpuLayoutEntry } from './tgpuBindGroupLayout.ts';
import type { WgslEnableExtension } from './wgslExtensions.ts';
import type { Infer } from './shared/repr.ts';
import { ShaderGenerator } from './tgsl/shaderGenerator.ts';
import type { ShaderGenerator } from './tgsl/shaderGenerator.ts';

export type ResolvableObject =
| SelfResolvable
Expand Down
15 changes: 9 additions & 6 deletions packages/typegpu/tests/utils/parseResolved.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type * as tinyest from 'tinyest';
import { NodeTypeCatalog as NODE } from 'tinyest';
import { type Assertion, expect } from 'vitest';
import tgpu, { d, ShaderGenerator, WgslGenerator, type TgpuFn } from 'typegpu';

type Snippet = ShaderGenerator.Snippet;
type UnknownData = ShaderGenerator.UnknownData;
type Origin = ShaderGenerator.Origin;
import tgpu, { d, type TgpuFn } from 'typegpu';
import {
type Snippet,
UnknownData,
type Origin,
WgslGenerator,
type FunctionDefinitionOptions,
} from 'typegpu/~internal';

class ExtractingGenerator extends WgslGenerator {
#fnDepth: number;
Expand All @@ -17,7 +20,7 @@ class ExtractingGenerator extends WgslGenerator {
this.#fnDepth = 0;
}

public functionDefinition(options: ShaderGenerator.FunctionDefinitionOptions): string {
public functionDefinition(options: FunctionDefinitionOptions): string {
this.#fnDepth++;
try {
return super.functionDefinition(options);
Expand Down
1 change: 1 addition & 0 deletions packages/typegpu/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const entry = [
'src/data/index.ts',
'src/std/index.ts',
'src/common/index.ts',
'src/internal.ts',
];

export default defineConfig({
Expand Down
Loading