Skip to content

Commit

Permalink
Merge pull request #44 from shader-slang/lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe authored Nov 9, 2024
2 parents 4fde89d + 4dd302e commit 26bb0d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 62 deletions.
85 changes: 31 additions & 54 deletions compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ class SlangCompiler
}
}

findCompileTarget(compileTargetStr)
{
for (var i = 0; i < this.compileTargetMap.length; i++)
{
var target = this.compileTargetMap[i];
if (target.name == compileTargetStr)
return target.value;
}
return 0;
}

// In our playground, we only allow to run shaders with two entry points: renderMain and printMain
findRunnableEntryPoint(module)
{
Expand Down Expand Up @@ -194,7 +205,7 @@ class SlangCompiler

try {
var slangSession = this.globalSlangSession.createSession(
this.compileTargetMap.findCompileTarget("SPIRV"));
this.findCompileTarget("SPIRV"));
if(!slangSession) {
return [];
}
Expand All @@ -209,21 +220,14 @@ class SlangCompiler
for (var i = 0; i < count; i++)
{
var entryPoint = module.getDefinedEntryPoint(i);
const entryPointName = entryPoint.getName();

result.push(entryPoint.getName());
entryPoint.delete();
}
} catch (e) {
return [];
}
finally {
if(module) {
module.delete();
}
if (slangSession) {
finally{
if (slangSession)
slangSession.delete();
}
}
return result;
}
Expand Down Expand Up @@ -283,16 +287,11 @@ class SlangCompiler
for (var i = 0; i < count; i++)
{
var entrypoint = userModule.getDefinedEntryPoint(i);
try {
var name = userModule.getDefinedEntryPoint(i).getName();
if (name == "imageMain" || name == "printMain")
{
this.diagnosticsMsg+=("error: Entry point name 'imageMain' or 'printMain' is reserved");
return false;
}
}
finally {
entrypoint.delete();
var name = userModule.getDefinedEntryPoint(i).getName();
if (name == "imageMain" || name == "printMain")
{
this.diagnosticsMsg+=("error: Entry point name 'imageMain' or 'printMain' is reserved");
return false;
}
}

Expand All @@ -310,8 +309,8 @@ class SlangCompiler
this.shaderType = entryPointName == "imageMain" ?
SlangCompiler.RENDER_SHADER : SlangCompiler.PRINT_SHADER;

componentList.push_back(mainProgram.module);
componentList.push_back(mainProgram.entryPoint);
componentList.push(mainProgram.module);
componentList.push(mainProgram.entryPoint);
}
else
{
Expand All @@ -320,7 +319,7 @@ class SlangCompiler
if (!entryPoint)
return false;

componentList.push_back(entryPoint);
componentList.push(entryPoint);
}
}
// otherwise, it's a whole program compilation, we will find all active entry points in the user code
Expand All @@ -333,8 +332,8 @@ class SlangCompiler
if (results[i] == "imageMain" || results[i] == "printMain")
{
var mainProgram = this.getPrecompiledProgram(slangSession, results[i]);
componentList.push_back(mainProgram.module);
componentList.push_back(mainProgram.entryPoint);
componentList.push(mainProgram.module);
componentList.push(mainProgram.entryPoint);
return true;
}
else
Expand All @@ -343,7 +342,7 @@ class SlangCompiler
if (!entryPoint)
return false;

componentList.push_back(entryPoint);
componentList.push(entryPoint);
}
}
}
Expand Down Expand Up @@ -420,21 +419,17 @@ class SlangCompiler
this.diagnosticsMsg+=(error.type + " error: " + error.message);
return false;
}
componentTypeList.push_back(module);
componentTypeList.push(module);
return true;
}

compile(shaderSource, entryPointName, compileTargetStr)
{
this.diagnosticsMsg = "";
if (this.hashedString)
{
this.hashedString.delete();
this.hashedString = null;
}

let shouldLinkPlaygroundModule = (shaderSource.match(/printMain|imageMain/) != null);

const compileTarget = this.compileTargetMap.findCompileTarget(compileTargetStr);
const compileTarget = this.findCompileTarget(compileTargetStr);
let isWholeProgram = isWholeProgramTarget(compileTargetStr);

if(!compileTarget) {
Expand All @@ -451,7 +446,7 @@ class SlangCompiler
return null;
}

var components = new this.slangWasmModule.ComponentTypeList();
var components = [];

var userModuleIndex = 0;
if (shouldLinkPlaygroundModule)
Expand All @@ -462,7 +457,7 @@ class SlangCompiler
}
if (!this.loadModule(slangSession, "user", shaderSource, components))
return null;
if (this.addActiveEntryPoints(slangSession, shaderSource, entryPointName, isWholeProgram, components.get(userModuleIndex), components) == false)
if (this.addActiveEntryPoints(slangSession, shaderSource, entryPointName, isWholeProgram, components[userModuleIndex], components) == false)
return null;
var program = slangSession.createCompositeComponentType(components);
var linkedProgram = program.link();
Expand Down Expand Up @@ -499,26 +494,8 @@ class SlangCompiler
return null;
}
finally {
if(linkedProgram) {
linkedProgram.delete();
}
if(program) {
program.delete();
}

if (components)
{
for (let i = 0; i < components.size(); i++)
{
components.get(i).delete();
}
components.delete();
}

if (slangSession) {
if (slangSession)
slangSession.delete();
}

if (!outCode || outCode == "")
return null;

Expand Down
2 changes: 1 addition & 1 deletion demos/circle.slang
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ float4 imageMain(uint2 dispatchThreadID, int2 screenSize)
float r = length(p)*0.75;
vec2 uv = vec2(a/tau,r);

float time = getTime() * 0.2;
float time = getTime();

//get the color
float xCol = (uv.x - time/3) * 3.0;
Expand Down
8 changes: 3 additions & 5 deletions demos/ocean.slang
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ float getwaves<int iterations>(float2 position) {
float wavePhaseShift = length(position) * 0.1; // this is to avoid every octave having exactly the same phase everywhere
float iter = 0.0; // this will help generating well distributed wave directions
float frequency = 1.0; // frequency of the wave, this will change every iteration
float timeMultiplier = 0.5; // time multiplier for the wave, this will change every iteration
float timeMultiplier = 5.0; // time multiplier for the wave, this will change every iteration
float weight = 1.0;// weight in final sum for the wave, this will change every iteration
float sumOfValues = 0.0; // will store final sum of values
float sumOfWeights = 0.0; // will store final sum of weights
Expand Down Expand Up @@ -145,18 +145,16 @@ float3 extra_cheap_atmosphere(float3 raydir, float3 sundir) {
// Calculate where the sun should be, it will be moving around the sky
float3 getSunDirection() {
float time = getTime();
return normalize(float3(sin(time * 0.1), 1.0, cos(time * 0.1)));
return normalize(float3(sin(time), 1.0, cos(time)));
}

// Get atmosphere color for given direction
float3 getAtmosphere(float3 dir) {
float time = getTime();
return extra_cheap_atmosphere(dir, getSunDirection()) * 0.5;
}

// Get sun color for given direction
float getSun(float3 dir) {
float time = getTime();
return pow(max(0.0, dot(dir, getSunDirection())), 720.0) * 210.0;
}

Expand Down Expand Up @@ -199,7 +197,7 @@ float4 imageMain(uint2 dispatchThreadID, int2 screenSize)
float3 waterPlaneLow = float3(0.0, -WATER_DEPTH, 0.0);

// define ray origin, moving around
float3 origin = float3(getTime() * 0.2, CAMERA_HEIGHT, 1);
float3 origin = float3(getTime() * 2.0, CAMERA_HEIGHT, 1);

// calculate intersections and reconstruct positions
float highPlaneHit = intersectPlane(origin, ray, waterPlaneHigh, float3(0.0, 1.0, 0.0));
Expand Down
14 changes: 12 additions & 2 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ function formatSpecifier(value, { flags, width, precision, specifierType })
// uint32_t high = 0;
// };
//
function hashToString(hashedStrings, hash)
{
for (var i = 0; i < hashedStrings.length; i++)
{
if (hashedStrings[i].hash == hash)
{
return hashedStrings[i].string;
}
}
}
function parsePrintfBuffer(hashedString, printfValueResource, bufferElementSize)
{

Expand All @@ -279,10 +289,10 @@ function parsePrintfBuffer(hashedString, printfValueResource, bufferElementSize)
switch (type)
{
case 1: // format string
formatString = hashedString.getString(printfBufferArray[offset + 1]); // low field
formatString = hashToString(hashedString, printfBufferArray[offset + 1]); // low field
break;
case 2: // normal string
dataArray.push(hashedString.getString(printfBufferArray[offset + 1])); // low field
dataArray.push(hashToString(hashedString, printfBufferArray[offset + 1])); // low field
break;
case 3: // integer
dataArray.push(printfBufferArray[offset + 1]); // low field
Expand Down

0 comments on commit 26bb0d9

Please sign in to comment.