Skip to content

Commit

Permalink
Bug fixes after moving to type script. (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe authored Dec 10, 2024
1 parent 810c6ce commit 4886acf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
7 changes: 4 additions & 3 deletions compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
28 changes: 28 additions & 0 deletions serve.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 18 additions & 3 deletions try-slang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -169,7 +177,12 @@ function withRenderLock(setupFn: { (): Promise<void>; }, 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();
});
});
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function updateProfileOptions(targetSelect: HTMLSelectElement, profileSelect: HT
}
}

function updateEntryPointOptions()
export function updateEntryPointOptions()
{
if (!compiler)
return;
Expand Down

0 comments on commit 4886acf

Please sign in to comment.