Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jespertheend committed Aug 22, 2024
1 parent 4ccd817 commit a1dc6fc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
2 changes: 2 additions & 0 deletions test/unit/src/rendering/renderers/shared/sceneUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function createVertexState() {
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
unsigned: false,
shaderLocation: 0,
},
],
},
Expand All @@ -31,6 +32,7 @@ export function createVertexState() {
componentCount: 4,
format: Mesh.AttributeFormat.FLOAT32,
unsigned: false,
shaderLocation: 1,
},
],
},
Expand Down
27 changes: 22 additions & 5 deletions test/unit/src/rendering/renderers/webGl/WebGlRenderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ async function basicRendererSetup() {
const scene = new Entity();
scene.add(cam);

const { commandLog, canvas } = assertHasSingleContext();
const context = assertHasSingleContext();

return { renderer, domTarget, camComponent, scene, commandLog, canvas };
return { renderer, domTarget, camComponent, scene, ...context };
}

/**
* @param {object} options
* @param {import("../../../../../../src/mod.js").MaterialMapMappedValues} [options.mappedValues]
* @param {string} [options.vertexShader]
*/
function createMaterial({
mappedValues = {},
vertexShader = "",
} = {}) {
const material = new Material();
const materialMapType = new WebGlMaterialMapType();
const materialConfig = new WebGlMaterialConfig();
materialConfig.vertexShader = new ShaderSource("");
materialConfig.vertexShader = new ShaderSource(vertexShader);
materialConfig.fragmentShader = new ShaderSource("");
materialMapType.materialConfig = materialConfig;
const materialMap = new MaterialMap({
Expand Down Expand Up @@ -168,7 +170,7 @@ Deno.test({
name: "Mesh with single buffer and two attributes",
async fn() {
await runWithWebGlMocksAsync(async () => {
const { scene, domTarget, camComponent, commandLog } = await basicRendererSetup();
const { scene, domTarget, camComponent, commandLog, setAttributeLocations } = await basicRendererSetup();

const vertexState = new VertexState({
buffers: [
Expand All @@ -181,19 +183,33 @@ Deno.test({
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
unsigned: false,
shaderLocation: 0,
},
{
attributeType: Mesh.AttributeType.UV1,
componentCount: 2,
format: Mesh.AttributeFormat.FLOAT32,
unsigned: false,
shaderLocation: 1,
},
],
},
],
});

const { material } = createMaterial();
const { material } = createMaterial({
vertexShader: `
// @location(0)
attribute vec3 pos;
// @location(1)
attribute vec2 uv;
`
});

setAttributeLocations({
pos: 0,
uv: 1,
})

const { mesh } = createCubeEntity({ scene, vertexState, material });

Expand Down Expand Up @@ -478,6 +494,7 @@ Deno.test({
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
unsigned: false,
shaderLocation: 0,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export class WebGlObject {}
export function createWebGlRenderingContext() {
const commandLog = new WebGlCommandLog();

/** @type {Map<string, number>} */
let attributeLocations = new Map();

const proxy = new Proxy({}, {
get(target, prop, receiver) {
if (typeof prop != "string") {
Expand All @@ -13,6 +16,13 @@ export function createWebGlRenderingContext() {
if (prop.toUpperCase() == prop) {
return "GL_" + prop;
}
if (prop == "getAttribLocation") {
/** @type {WebGLRenderingContext["getAttribLocation"]} */
const fn = (program, name) => {
return attributeLocations.get(name) ?? -1;
}
return fn;
}

/**
* @param {...unknown[]} args
Expand All @@ -29,5 +39,11 @@ export function createWebGlRenderingContext() {
return {
context: /** @type {WebGLRenderingContext} */ (proxy),
commandLog,
/**
* @param {Object.<string, number>} locations
*/
setAttributeLocations(locations) {
attributeLocations = new Map(Object.entries(locations));
}
};
}
8 changes: 4 additions & 4 deletions test/unit/src/rendering/renderers/webGl/shared/webGlMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createWebGlRenderingContext } from "./WebGlRenderingContext.js";
const oldDocument = globalThis.document;
let installed = false;

/** @type {{canvas: HTMLCanvasElement, context: WebGLRenderingContext, commandLog: import("./WebGlCommandLog.js").WebGlCommandLog}[]} */
/** @type {({canvas: HTMLCanvasElement} & ReturnType<typeof createWebGlRenderingContext>)[]} */
let createdContexts = [];

export function installWebGlMocks() {
Expand Down Expand Up @@ -34,9 +34,9 @@ export function installWebGlMocks() {
contextRequested = true;
if (contextId == "webgl") {
if (!webGlContextSupported) return null;
const { context, commandLog } = createWebGlRenderingContext();
createdContexts.push({ canvas, context, commandLog });
return context;
const context = createWebGlRenderingContext();
createdContexts.push({ canvas, ...context });
return context.context;
} else if (contextId == "2d") {
if (!context2dSupported) return null;
return create2dRenderingContext(canvas);
Expand Down

0 comments on commit a1dc6fc

Please sign in to comment.