Skip to content
Snippets Groups Projects
Commit ebf64404 authored by Alexander Rose's avatar Alexander Rose
Browse files

add loop unrolling glsl support

parent e5dcc8e5
No related branches found
No related tags found
No related merge requests found
...@@ -101,7 +101,8 @@ const ShaderChunks: { [k: string]: string } = { ...@@ -101,7 +101,8 @@ const ShaderChunks: { [k: string]: string } = {
wboit_write wboit_write
}; };
const reInclude = /^(?!\/\/)\s*#include\s+(\S+)/gmi; const reInclude = /^(?!\/\/)\s*#include\s+(\S+)/gm;
const reUnrollLoop = /#pragma unroll_loop_start\s+for\s*\(\s*int\s+i\s*=\s*(\d+)\s*;\s*i\s*<\s*(\d+)\s*;\s*\+\+i\s*\s*\)\s*{([\s\S]+?)}\s+#pragma unroll_loop_end/g;
const reSingleLineComment = /[ \t]*\/\/.*\n/g; const reSingleLineComment = /[ \t]*\/\/.*\n/g;
const reMultiLineComment = /[ \t]*\/\*[\s\S]*?\*\//g; const reMultiLineComment = /[ \t]*\/\*[\s\S]*?\*\//g;
const reMultipleLinebreaks = /\n{2,}/g; const reMultipleLinebreaks = /\n{2,}/g;
...@@ -119,6 +120,29 @@ function addIncludes(text: string) { ...@@ -119,6 +120,29 @@ function addIncludes(text: string) {
.replace(reMultipleLinebreaks, '\n'); .replace(reMultipleLinebreaks, '\n');
} }
function unrollLoops(str: string) {
return str.replace(reUnrollLoop, loopReplacer);
}
function loopReplacer(match: string, start: string, end: string, snippet: string) {
let out = '';
for (let i = parseInt(start); i < parseInt(end); ++i) {
out += snippet
.replace(/\[\s*i\s*\]/g, `[${i}]`)
.replace(/UNROLLED_LOOP_INDEX/g, `${i}`);
}
return out;
}
function replaceCounts(str: string, defines: ShaderDefines) {
if (defines.dClipObjectCount) str = str.replace(/dClipObjectCount/g, `${defines.dClipObjectCount.ref.value}`);
return str;
}
function preprocess(str: string, defines: ShaderDefines) {
return unrollLoops(replaceCounts(str, defines));
}
export function ShaderCode(name: string, vert: string, frag: string, extensions: ShaderExtensions = {}, outTypes: FragOutTypes = {}): ShaderCode { export function ShaderCode(name: string, vert: string, frag: string, extensions: ShaderExtensions = {}, outTypes: FragOutTypes = {}): ShaderCode {
return { id: shaderCodeId(), name, vert: addIncludes(vert), frag: addIncludes(frag), extensions, outTypes }; return { id: shaderCodeId(), name, vert: addIncludes(vert), frag: addIncludes(frag), extensions, outTypes };
} }
...@@ -278,8 +302,8 @@ export function addShaderDefines(gl: GLRenderingContext, extensions: WebGLExtens ...@@ -278,8 +302,8 @@ export function addShaderDefines(gl: GLRenderingContext, extensions: WebGLExtens
return { return {
id: shaderCodeId(), id: shaderCodeId(),
name: shaders.name, name: shaders.name,
vert: `${vertPrefix}${header}${shaders.vert}`, vert: `${vertPrefix}${header}${preprocess(shaders.vert, defines)}`,
frag: `${fragPrefix}${header}${frag}`, frag: `${fragPrefix}${header}${preprocess(frag, defines)}`,
extensions: shaders.extensions, extensions: shaders.extensions,
outTypes: shaders.outTypes outTypes: shaders.outTypes
}; };
......
...@@ -89,8 +89,9 @@ export const common_clip = ` ...@@ -89,8 +89,9 @@ export const common_clip = `
// flag is a bit-flag for clip-objects to ignore (note, object ids start at 1 not 0) // flag is a bit-flag for clip-objects to ignore (note, object ids start at 1 not 0)
bool clipTest(vec4 sphere, int flag) { bool clipTest(vec4 sphere, int flag) {
#pragma unroll_loop_start
for (int i = 0; i < dClipObjectCount; ++i) { for (int i = 0; i < dClipObjectCount; ++i) {
if (flag == 0 || hasBit(flag, i + 1)) { if (flag == 0 || hasBit(flag, UNROLLED_LOOP_INDEX + 1)) {
// TODO take sphere radius into account? // TODO take sphere radius into account?
bool test = getSignedDistance(sphere.xyz, uClipObjectType[i], uClipObjectPosition[i], uClipObjectRotation[i], uClipObjectScale[i]) <= 0.0; bool test = getSignedDistance(sphere.xyz, uClipObjectType[i], uClipObjectPosition[i], uClipObjectRotation[i], uClipObjectScale[i]) <= 0.0;
if ((!uClipObjectInvert[i] && test) || (uClipObjectInvert[i] && !test)) { if ((!uClipObjectInvert[i] && test) || (uClipObjectInvert[i] && !test)) {
...@@ -98,6 +99,7 @@ export const common_clip = ` ...@@ -98,6 +99,7 @@ export const common_clip = `
} }
} }
} }
#pragma unroll_loop_end
return false; return false;
} }
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment