From 4886acf69a1f3adbb2ec1b9d3ee2a841def83ed9 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 9 Dec 2024 19:00:26 -0800 Subject: [PATCH] Bug fixes after moving to type script. (#80) --- README.md | 4 +++- compiler.ts | 7 ++++--- serve.py | 28 ++++++++++++++++++++++++++++ try-slang.ts | 21 ++++++++++++++++++--- ui.ts | 2 +- 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 serve.py diff --git a/README.md b/README.md index d63bf32..0c0221c 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ git clone https://github.com/shader-slang/slang-playground.git 2. Start a Python web server to host the files: ```bash - python -m http.server 8000 + python serve.py ``` 3. Open `http://localhost:8000` in your browser to verify the server is running and the files are accessible. You should see the application loading the `.wasm` and `.js` files correctly. @@ -54,3 +54,5 @@ Run `npm install` to install dependencies. You can then run `npx tsc` and host the webserver from the main directory. You will either need to run `npx tsc` whenever you make a code change, or you can run `npx tsc --watch` to continuously compile. Now load or reload `localhost:8000` in your browser to see the results. + +When updating CSS or some transitively included files, the browser may use the out of date file stored in the cache. To prevent this, you can hold Shift key and click Refresh to force the browser to reload all files. \ No newline at end of file diff --git a/compiler.ts b/compiler.ts index aeed137..ac9de5b 100644 --- a/compiler.ts +++ b/compiler.ts @@ -345,7 +345,7 @@ export class SlangCompiler { return true; } - getBindingDescriptor(index: number, programReflection: ProgramLayout, parameter: VariableLayoutReflection): BindingDescriptor { + getBindingDescriptor(index: number, programReflection: ProgramLayout, parameter: VariableLayoutReflection): BindingDescriptor|null { const globalLayout = programReflection.getGlobalParamsTypeLayout(); if(globalLayout == null) { @@ -374,7 +374,7 @@ export class SlangCompiler { else if (bindingType == this.slangWasmModule.BindingType.MutableRawBuffer) { return { buffer: { type: 'storage' } }; } - throw new Error(`Binding type ${bindingType} not supported`) + return null; } getResourceBindings(linkedProgram: ComponentType): Bindings { @@ -401,7 +401,8 @@ export class SlangCompiler { const resourceInfo = this.getBindingDescriptor(parameter.getBindingIndex(), reflection, parameter); // extend binding with resourceInfo - Object.assign(binding, resourceInfo); + if (resourceInfo) + Object.assign(binding, resourceInfo); resourceDescriptors.set(name, binding); } diff --git a/serve.py b/serve.py new file mode 100644 index 0000000..68b0c73 --- /dev/null +++ b/serve.py @@ -0,0 +1,28 @@ +import http.server +import socketserver + +PORT = 8000 + +class HttpRequestHandler(http.server.SimpleHTTPRequestHandler): + extensions_map = { + '': 'application/octet-stream', + '.manifest': 'text/cache-manifest', + '.html': 'text/html', + '.png': 'image/png', + '.jpg': 'image/jpg', + '.svg': 'image/svg+xml', + '.css': 'text/css', + '.js':'application/x-javascript', + '.mjs':'application/x-javascript', + '.wasm': 'application/wasm', + '.json': 'application/json', + '.xml': 'application/xml', + } + +httpd = socketserver.TCPServer(("localhost", PORT), HttpRequestHandler) + +try: + print(f"serving at http://localhost:{PORT}") + httpd.serve_forever() +except KeyboardInterrupt: + pass \ No newline at end of file diff --git a/try-slang.ts b/try-slang.ts index 86b434b..1c96c4a 100644 --- a/try-slang.ts +++ b/try-slang.ts @@ -5,6 +5,8 @@ import { SlangCompiler, Bindings, isWholeProgramTarget } from './compiler.js'; import { initMonaco, userCodeURI, codeEditorChangeContent, initLanguageServer } from './language-server.js'; import { restoreSelectedTargetFromURL, restoreDemoSelectionFromURL, loadDemo, canvasCurrentMousePos, canvasLastMouseDownPos, canvasIsMouseDown, canvasMouseClicked, resetMouse, renderOutput, canvas, entryPointSelect, targetSelect } from './ui.js'; import { fetchWithProgress, configContext, parseResourceCommands, parseCallCommands, createOutputTexture, parsePrintfBuffer, CallCommand } from './util.js'; +import { updateEntryPointOptions } from "./ui.js"; + import type { LanguageServer, MainModule, ThreadGroupSize } from "./slang-wasm.js"; // ignore complaint about missing types - added via tsconfig @@ -21,6 +23,12 @@ declare let RequireJS: { require: typeof require }; +class NotReadyError extends Error { + constructor(message: string) { + super(message); + } +} + export var compiler: SlangCompiler | null = null; export var slangd: LanguageServer | null = null; var device: GPUDevice; @@ -169,7 +177,12 @@ function withRenderLock(setupFn: { (): Promise; }, renderFn: { (timeMS: nu setupFn().then(() => { requestAnimationFrame(newRenderLoop); }).catch((error: Error) => { - diagnosticsArea.setValue(error.message); + if (error instanceof NotReadyError) { + } + else { + if (diagnosticsArea != null) + diagnosticsArea.setValue(error.message); + } releaseRenderLock(); }); }); @@ -197,10 +210,10 @@ function startRendering() { // This is a lighter-weight setup function that doesn't need to re-compile the shader code. const setupRenderer = async () => { if (!computePipeline || !passThroughPipeline) - throw new Error("pipeline not ready"); + throw new NotReadyError("pipeline not ready"); if (!currentWindowSize || currentWindowSize[0] < 2 || currentWindowSize[1] < 2) - throw new Error("window not ready"); + throw new NotReadyError("window not ready"); allocatedResources = await processResourceCommands(computePipeline, resourceBindings, resourceCommands); @@ -929,7 +942,9 @@ export async function onCompile() { toggleDisplayMode(HIDDEN_MODE); const compileTarget = targetSelect.value; + await updateEntryPointOptions(); const entryPoint = entryPointSelect.value; + if (entryPoint == "" && !isWholeProgramTarget(compileTarget)) { diagnosticsArea.setValue("Please select the entry point name"); return; diff --git a/ui.ts b/ui.ts index 5623da4..7a1520b 100644 --- a/ui.ts +++ b/ui.ts @@ -98,7 +98,7 @@ function updateProfileOptions(targetSelect: HTMLSelectElement, profileSelect: HT } } -function updateEntryPointOptions() +export function updateEntryPointOptions() { if (!compiler) return;