Skip to content

Commit

Permalink
cleanup and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Oct 12, 2023
1 parent 2f2f01a commit 353280c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 30 deletions.
3 changes: 1 addition & 2 deletions WGLMakie/src/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ export class MakieCamera {
// update all existing preprojection matrices
Object.keys(this.preprojections).forEach((key) => {
const [space, markerspace] = key.split(","); // jeez js, really just converting array keys to "elem,elem"?
this.preprojections[key].value =
new THREE.Uniform(this.calculate_preprojection_matrix(space, markerspace));
this.preprojections[key].value = this.calculate_preprojection_matrix(space, markerspace);
});
}

Expand Down
2 changes: 1 addition & 1 deletion WGLMakie/src/Serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function recreate_instanced_geometry(mesh) {
const buffer_geometry = new THREE.InstancedBufferGeometry();
const vertexarrays = {};
const instance_attributes = {};
const faces = [...mesh.geometry.index];
const faces = [...mesh.geometry.index.array];
Object.keys(mesh.geometry.attributes).forEach((name) => {
const buffer = mesh.geometry.attributes[name];
// really dont know why copying an array is considered rocket science in JS
Expand Down
1 change: 1 addition & 0 deletions WGLMakie/src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ function serialize_three(program::Program)
uniforms = serialize_uniforms(program.uniforms)
attribute_updater = Observable(["", [], 0])
register_geometry_updates(attribute_updater, program)
# TODO, make this configurable in ShaderAbstractions
update_shader(x) = replace(x, "#version 300 es" => "")
return Dict(:vertexarrays => serialize_named_buffer(program.vertexarray),
:faces => indices, :uniforms => uniforms,
Expand Down
40 changes: 33 additions & 7 deletions WGLMakie/src/wglmakie.bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -20997,6 +20997,38 @@ function wglerror(gl, error) {
return "Unknown error";
}
}
function handleSource(string, errorLine) {
const lines = string.split("\n");
const lines2 = [];
const from = Math.max(errorLine - 6, 0);
const to = Math.min(errorLine + 6, lines.length);
for(let i = from; i < to; i++){
const line = i + 1;
lines2.push(`${line === errorLine ? ">" : " "} ${line}: ${lines[i]}`);
}
return lines2.join("\n");
}
function getShaderErrors(gl, shader, type) {
const status = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
const errors = gl.getShaderInfoLog(shader).trim();
if (status && errors === "") return "";
const errorMatches = /ERROR: 0:(\d+)/.exec(errors);
if (errorMatches) {
const errorLine = parseInt(errorMatches[1]);
return type.toUpperCase() + "\n\n" + errors + "\n\n" + handleSource(gl.getShaderSource(shader), errorLine);
} else {
return errors;
}
}
function on_shader_error(gl, program, glVertexShader, glFragmentShader) {
const programLog = gl.getProgramInfoLog(program).trim();
const vertexErrors = getShaderErrors(gl, glVertexShader, "vertex");
const fragmentErrors = getShaderErrors(gl, glFragmentShader, "fragment");
const vertexLog = gl.getShaderInfoLog(glVertexShader).trim();
const fragmentLog = gl.getShaderInfoLog(glFragmentShader).trim();
const err = "THREE.WebGLProgram: Shader Error " + wglerror(gl, gl.getError()) + " - " + "VALIDATE_STATUS " + gl.getProgramParameter(program, gl.VALIDATE_STATUS) + "\n\n" + "Program Info Log:\n" + programLog + "\n" + vertexErrors + "\n" + fragmentErrors + "\n" + "Fragment log:\n" + fragmentLog + "Vertex log:\n" + vertexLog;
JSServe.Connection.send_warning(err);
}
function threejs_module(canvas, comm, width, height, resize_to_body) {
let context = canvas.getContext("webgl2", {
preserveDrawingBuffer: true
Expand All @@ -21016,13 +21048,7 @@ function threejs_module(canvas, comm, width, height, resize_to_body) {
context: context,
powerPreference: "high-performance"
});
renderer.debug.onShaderError = (gl, program, vs, fs)=>{
const infolog = gl.getProgramInfoLog(program);
const vss = "Vertex Shader:\n" + gl.getShaderSource(vs);
const fss = "Fragment Shader:\n" + gl.getShaderSource(fs);
const err = infolog + "\n" + vss + "\n" + fss;
JSServe.Connection.send_error("Error in shader: " + err, null);
};
renderer.debug.onShaderError = on_shader_error;
renderer.setClearColor("#ffffff");
renderer.setPixelRatio(pixelRatio1);
renderer.setSize(width / pixelRatio1, height / pixelRatio1);
Expand Down
101 changes: 81 additions & 20 deletions WGLMakie/src/wglmakie.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,101 @@ function throttle_function(func, delay) {
// to occur at some later later time, so that it
// does not get lost; we'll schedule it so that it
// fires just a bit after our choke ends.
future_id = setTimeout(() => inner_throttle(...args), now - prev + 1);
future_id = setTimeout(
() => inner_throttle(...args),
now - prev + 1
);
}
};
}
return inner_throttle;
}

export function wglerror(gl, error) {
switch (error) {
case gl.NO_ERROR:
return ("No error");
return "No error";
case gl.INVALID_ENUM:
return ("Invalid enum");
return "Invalid enum";
case gl.INVALID_VALUE:
return ("Invalid value");
return "Invalid value";
case gl.INVALID_OPERATION:
return ("Invalid operation");
return "Invalid operation";
case gl.OUT_OF_MEMORY:
return ("Out of memory");
return "Out of memory";
case gl.CONTEXT_LOST_WEBGL:
return ("Context lost");
return "Context lost";
default:
return ("Unknown error");
return "Unknown error";
}
}
// taken from THREEJS:
//https://github.com/mrdoob/three.js/blob/5303ef2d46b02e7c503ca63cedca0b93cd9c853e/src/renderers/webgl/WebGLProgram.js#L67C1-L89C2
function handleSource(string, errorLine) {
const lines = string.split("\n");
const lines2 = [];

const from = Math.max(errorLine - 6, 0);
const to = Math.min(errorLine + 6, lines.length);

for (let i = from; i < to; i++) {
const line = i + 1;
lines2.push(`${line === errorLine ? ">" : " "} ${line}: ${lines[i]}`);
}

return lines2.join("\n");
}

function getShaderErrors(gl, shader, type) {
const status = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
const errors = gl.getShaderInfoLog(shader).trim();

if (status && errors === "") return "";

const errorMatches = /ERROR: 0:(\d+)/.exec(errors);
if (errorMatches) {
// --enable-privileged-webgl-extension
// console.log( '**' + type + '**', gl.getExtension( 'WEBGL_debug_shaders' ).getTranslatedShaderSource( shader ) );

const errorLine = parseInt(errorMatches[1]);
return (
type.toUpperCase() +
"\n\n" +
errors +
"\n\n" +
handleSource(gl.getShaderSource(shader), errorLine)
);
} else {
return errors;
}
}
function on_shader_error(gl, program, glVertexShader, glFragmentShader) {
const programLog = gl.getProgramInfoLog(program).trim();
const vertexErrors = getShaderErrors(gl, glVertexShader, "vertex");
const fragmentErrors = getShaderErrors(gl, glFragmentShader, "fragment");
const vertexLog = gl.getShaderInfoLog(glVertexShader).trim();
const fragmentLog = gl.getShaderInfoLog(glFragmentShader).trim();

const err =
"THREE.WebGLProgram: Shader Error " +
wglerror(gl, gl.getError()) +
" - " +
"VALIDATE_STATUS " +
gl.getProgramParameter(program, gl.VALIDATE_STATUS) +
"\n\n" +
"Program Info Log:\n" +
programLog +
"\n" +
vertexErrors +
"\n" +
fragmentErrors +
"\n" +
"Fragment log:\n" +
fragmentLog +
"Vertex log:\n" +
vertexLog;

JSServe.Connection.send_warning(err);
}

function threejs_module(canvas, comm, width, height, resize_to_body) {
let context = canvas.getContext("webgl2", {
Expand All @@ -155,15 +226,7 @@ function threejs_module(canvas, comm, width, height, resize_to_body) {
powerPreference: "high-performance",
});

renderer.debug.onShaderError = (gl, program, vs, fs) => {
// Get the error info from the WebGL context
const infolog = gl.getProgramInfoLog(program);
// If you want to see the full shader source code:
const vss = "Vertex Shader:\n" + gl.getShaderSource(vs);
const fss = "Fragment Shader:\n" + gl.getShaderSource(fs);
const err = infolog + "\n" + vss + "\n" + fss;
JSServe.Connection.send_error("Error in shader: " + err, null);
};
renderer.debug.onShaderError = on_shader_error;

renderer.setClearColor("#ffffff");

Expand Down Expand Up @@ -548,8 +611,6 @@ export function register_popup(popup, scene, plots_to_pick, callback) {
});
}



window.WGL = {
deserialize_scene,
threejs_module,
Expand Down

0 comments on commit 353280c

Please sign in to comment.