From 2379a22f00a03125d9be891067f43de28ecaece5 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Mon, 29 Jun 2026 13:09:54 +0200 Subject: [PATCH] feat: Export internals from 'typegpu/~internal' --- packages/typegpu-gl/src/glslGenerator.ts | 22 ++++++----- packages/typegpu-gl/src/tgpuRootWebGL.ts | 3 +- packages/typegpu/package.json | 9 +++++ packages/typegpu/src/indexNamedExports.ts | 11 +++++- packages/typegpu/src/internal.ts | 18 +++++++++ packages/typegpu/src/resolutionCtx.ts | 3 +- packages/typegpu/src/tgsl/shaderGenerator.ts | 35 +++++++++++++---- .../src/tgsl/shaderGenerator_members.ts | 38 ------------------- packages/typegpu/src/tgsl/wgslGenerator.ts | 12 +++--- packages/typegpu/src/types.ts | 2 +- packages/typegpu/tests/utils/parseResolved.ts | 15 +++++--- packages/typegpu/tsdown.config.ts | 1 + 12 files changed, 97 insertions(+), 72 deletions(-) create mode 100644 packages/typegpu/src/internal.ts delete mode 100644 packages/typegpu/src/tgsl/shaderGenerator_members.ts diff --git a/packages/typegpu-gl/src/glslGenerator.ts b/packages/typegpu-gl/src/glslGenerator.ts index 4c549bdac7..82850c284e 100644 --- a/packages/typegpu-gl/src/glslGenerator.ts +++ b/packages/typegpu-gl/src/glslGenerator.ts @@ -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 @@ -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} { @@ -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 { @@ -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'; @@ -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'); } diff --git a/packages/typegpu-gl/src/tgpuRootWebGL.ts b/packages/typegpu-gl/src/tgpuRootWebGL.ts index c010352abe..239644283b 100644 --- a/packages/typegpu-gl/src/tgpuRootWebGL.ts +++ b/packages/typegpu-gl/src/tgpuRootWebGL.ts @@ -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'; // ---------- diff --git a/packages/typegpu/package.json b/packages/typegpu/package.json index 46400bd2da..cc0c3e7cde 100644 --- a/packages/typegpu/package.json +++ b/packages/typegpu/package.json @@ -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" @@ -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": { @@ -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, diff --git a/packages/typegpu/src/indexNamedExports.ts b/packages/typegpu/src/indexNamedExports.ts index 8a4ba4d469..959a059340 100644 --- a/packages/typegpu/src/indexNamedExports.ts +++ b/packages/typegpu/src/indexNamedExports.ts @@ -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'; @@ -60,6 +64,9 @@ export type { ValidUsagesFor, Vertex, VertexFlag, + BufferWriteOptions, + BufferInitCallback, + BufferInitialData, } from './core/buffer/buffer.ts'; export type { TgpuBufferMutable, diff --git a/packages/typegpu/src/internal.ts b/packages/typegpu/src/internal.ts new file mode 100644 index 0000000000..348233398a --- /dev/null +++ b/packages/typegpu/src/internal.ts @@ -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'; diff --git a/packages/typegpu/src/resolutionCtx.ts b/packages/typegpu/src/resolutionCtx.ts index c03bf1c509..2b616c5eb9 100644 --- a/packages/typegpu/src/resolutionCtx.ts +++ b/packages/typegpu/src/resolutionCtx.ts @@ -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'; @@ -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'; /** diff --git a/packages/typegpu/src/tgsl/shaderGenerator.ts b/packages/typegpu/src/tgsl/shaderGenerator.ts index aa4f3ded6a..04ee023ccd 100644 --- a/packages/typegpu/src/tgsl/shaderGenerator.ts +++ b/packages/typegpu/src/tgsl/shaderGenerator.ts @@ -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.** @@ -24,5 +47,3 @@ export interface ShaderGenerator { typeAnnotation(schema: BaseData): string; numericLiteral(value: number, schema: BaseData): ResolvedSnippet; } - -export * as ShaderGenerator from './shaderGenerator_members.ts'; diff --git a/packages/typegpu/src/tgsl/shaderGenerator_members.ts b/packages/typegpu/src/tgsl/shaderGenerator_members.ts deleted file mode 100644 index 0dddb1a85f..0000000000 --- a/packages/typegpu/src/tgsl/shaderGenerator_members.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { Block } from 'tinyest'; -import type { BaseData } from '../data/wgslTypes.ts'; -import type { BindableBufferUsage, FunctionArgument, TgpuShaderStage } from '../types.ts'; -import type { VariableScope } from '../core/variable/tgpuVariable.ts'; -import type { Snippet } from '../data/snippet.ts'; - -export { UnknownData } from '../data/dataTypes.ts'; -export { getName } from '../shared/meta.ts'; - -// types -export type { ResolutionCtx, FunctionArgument, TgpuShaderStage } from '../types.ts'; -export type { Snippet } from '../data/snippet.ts'; -export type { Origin } from '../data/snippet.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; -} diff --git a/packages/typegpu/src/tgsl/wgslGenerator.ts b/packages/typegpu/src/tgsl/wgslGenerator.ts index f8e9fc256e..2bedc1e94a 100644 --- a/packages/typegpu/src/tgsl/wgslGenerator.ts +++ b/packages/typegpu/src/tgsl/wgslGenerator.ts @@ -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'; @@ -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'; diff --git a/packages/typegpu/src/types.ts b/packages/typegpu/src/types.ts index 86c40a3467..6f042f0510 100644 --- a/packages/typegpu/src/types.ts +++ b/packages/typegpu/src/types.ts @@ -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 diff --git a/packages/typegpu/tests/utils/parseResolved.ts b/packages/typegpu/tests/utils/parseResolved.ts index a197e5ec97..8eb8d1e7a1 100644 --- a/packages/typegpu/tests/utils/parseResolved.ts +++ b/packages/typegpu/tests/utils/parseResolved.ts @@ -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; @@ -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); diff --git a/packages/typegpu/tsdown.config.ts b/packages/typegpu/tsdown.config.ts index ae102ec2c4..d9fd572763 100644 --- a/packages/typegpu/tsdown.config.ts +++ b/packages/typegpu/tsdown.config.ts @@ -6,6 +6,7 @@ const entry = [ 'src/data/index.ts', 'src/std/index.ts', 'src/common/index.ts', + 'src/internal.ts', ]; export default defineConfig({