Skip to content

Commit 724b41a

Browse files
committed
Move setAttributes implementation to renderer
1 parent 3ea32ed commit 724b41a

File tree

4 files changed

+107
-102
lines changed

4 files changed

+107
-102
lines changed

src/webgl/p5.RendererGL.js

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,54 @@ class RendererGL extends Renderer3D {
382382
return;
383383
}
384384

385+
_setAttributes(key, value) {
386+
if (typeof this._pInst._glAttributes === "undefined") {
387+
console.log(
388+
"You are trying to use setAttributes on a p5.Graphics object " +
389+
"that does not use a WEBGL renderer."
390+
);
391+
return;
392+
}
393+
let unchanged = true;
394+
if (typeof value !== "undefined") {
395+
//first time modifying the attributes
396+
if (this._pInst._glAttributes === null) {
397+
this._pInst._glAttributes = {};
398+
}
399+
if (this._pInst._glAttributes[key] !== value) {
400+
//changing value of previously altered attribute
401+
this._pInst._glAttributes[key] = value;
402+
unchanged = false;
403+
}
404+
//setting all attributes with some change
405+
} else if (key instanceof Object) {
406+
if (this._pInst._glAttributes !== key) {
407+
this._pInst._glAttributes = key;
408+
unchanged = false;
409+
}
410+
}
411+
//@todo_FES
412+
if (!this.isP3D || unchanged) {
413+
return;
414+
}
415+
416+
if (!this._pInst._setupDone) {
417+
if (this.geometryBufferCache.numCached() > 0) {
418+
p5._friendlyError(
419+
"Sorry, Could not set the attributes, you need to call setAttributes() " +
420+
"before calling the other drawing methods in setup()"
421+
);
422+
return;
423+
}
424+
}
425+
426+
this._resetContext(null, null, RendererGL);
427+
428+
if (this.states.curCamera) {
429+
this.states.curCamera._renderer = this._renderer;
430+
}
431+
}
432+
385433
_initContext() {
386434
if (this._pInst._glAttributes?.version !== 1) {
387435
// Unless WebGL1 is explicitly asked for, try to create a WebGL2 context
@@ -2085,51 +2133,7 @@ function rendererGL(p5, fn) {
20852133
* @param {Object} obj object with key-value pairs
20862134
*/
20872135
fn.setAttributes = function (key, value) {
2088-
if (typeof this._glAttributes === "undefined") {
2089-
console.log(
2090-
"You are trying to use setAttributes on a p5.Graphics object " +
2091-
"that does not use a WEBGL renderer."
2092-
);
2093-
return;
2094-
}
2095-
let unchanged = true;
2096-
if (typeof value !== "undefined") {
2097-
//first time modifying the attributes
2098-
if (this._glAttributes === null) {
2099-
this._glAttributes = {};
2100-
}
2101-
if (this._glAttributes[key] !== value) {
2102-
//changing value of previously altered attribute
2103-
this._glAttributes[key] = value;
2104-
unchanged = false;
2105-
}
2106-
//setting all attributes with some change
2107-
} else if (key instanceof Object) {
2108-
if (this._glAttributes !== key) {
2109-
this._glAttributes = key;
2110-
unchanged = false;
2111-
}
2112-
}
2113-
//@todo_FES
2114-
if (!this._renderer.isP3D || unchanged) {
2115-
return;
2116-
}
2117-
2118-
if (!this._setupDone) {
2119-
if (this._renderer.geometryBufferCache.numCached() > 0) {
2120-
p5._friendlyError(
2121-
"Sorry, Could not set the attributes, you need to call setAttributes() " +
2122-
"before calling the other drawing methods in setup()"
2123-
);
2124-
return;
2125-
}
2126-
}
2127-
2128-
this._renderer._resetContext(null, null, RendererGL);
2129-
2130-
if (this._renderer.states.curCamera) {
2131-
this._renderer.states.curCamera._renderer = this._renderer;
2132-
}
2136+
return this._renderer._setAttributes(key, value);
21332137
};
21342138

21352139
/**

src/webgpu/p5.RendererWebGPU.js

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ class RendererWebGPU extends Renderer3D {
5050

5151
async _initContext() {
5252
this.adapter = await navigator.gpu?.requestAdapter(this._webgpuAttributes);
53-
console.log('Adapter:');
54-
console.log(this.adapter);
53+
// console.log('Adapter:');
54+
// console.log(this.adapter);
5555
if (this.adapter) {
5656
console.log([...this.adapter.features]);
5757
}
5858
this.device = await this.adapter?.requestDevice({
5959
// Todo: check support
6060
requiredFeatures: ['depth32float-stencil8']
6161
});
62-
console.log('Device:');
63-
console.log(this.device);
62+
// console.log('Device:');
63+
// console.log(this.device);
6464
if (!this.device) {
6565
throw new Error('Your browser does not support WebGPU.');
6666
}
@@ -79,6 +79,55 @@ class RendererWebGPU extends Renderer3D {
7979
this._update();
8080
}
8181

82+
async _setAttributes(key, value) {
83+
if (typeof this._pInst._webgpuAttributes === "undefined") {
84+
console.log(
85+
"You are trying to use setAttributes on a p5.Graphics object " +
86+
"that does not use a WebGPU renderer."
87+
);
88+
return;
89+
}
90+
let unchanged = true;
91+
92+
if (typeof value !== "undefined") {
93+
//first time modifying the attributes
94+
if (this._pInst._webgpuAttributes === null) {
95+
this._pInst._webgpuAttributes = {};
96+
}
97+
if (this._pInst._webgpuAttributes[key] !== value) {
98+
//changing value of previously altered attribute
99+
this._webgpuAttributes[key] = value;
100+
unchanged = false;
101+
}
102+
//setting all attributes with some change
103+
} else if (key instanceof Object) {
104+
if (this._pInst._webgpuAttributes !== key) {
105+
this._pInst._webgpuAttributes = key;
106+
unchanged = false;
107+
}
108+
}
109+
//@todo_FES
110+
if (!this.isP3D || unchanged) {
111+
return;
112+
}
113+
114+
if (!this._pInst._setupDone) {
115+
if (this.geometryBufferCache.numCached() > 0) {
116+
p5._friendlyError(
117+
"Sorry, Could not set the attributes, you need to call setAttributes() " +
118+
"before calling the other drawing methods in setup()"
119+
);
120+
return;
121+
}
122+
}
123+
124+
await this._resetContext(null, null, RendererWebGPU);
125+
126+
if (this.states.curCamera) {
127+
this.states.curCamera._renderer = this._renderer;
128+
}
129+
}
130+
82131
_updateSize() {
83132
if (this.depthTexture && this.depthTexture.destroy) {
84133
this.depthTexture.destroy();
@@ -1882,53 +1931,9 @@ function rendererWebGPU(p5, fn) {
18821931
return this._renderer.ensureTexture(source);
18831932
}
18841933

1934+
// TODO: move this and the duplicate in the WebGL renderer to another file
18851935
fn.setAttributes = async function (key, value) {
1886-
if (typeof this._webgpuAttributes === "undefined") {
1887-
console.log(
1888-
"You are trying to use setAttributes on a p5.Graphics object " +
1889-
"that does not use a WebGPU renderer."
1890-
);
1891-
return;
1892-
}
1893-
let unchanged = true;
1894-
1895-
if (typeof value !== "undefined") {
1896-
//first time modifying the attributes
1897-
if (this._webgpuAttributes === null) {
1898-
this._webgpuAttributes = {};
1899-
}
1900-
if (this._webgpuAttributes[key] !== value) {
1901-
//changing value of previously altered attribute
1902-
this._webgpuAttributes[key] = value;
1903-
unchanged = false;
1904-
}
1905-
//setting all attributes with some change
1906-
} else if (key instanceof Object) {
1907-
if (this._webgpuAttributes !== key) {
1908-
this._webgpuAttributes = key;
1909-
unchanged = false;
1910-
}
1911-
}
1912-
//@todo_FES
1913-
if (!this._renderer.isP3D || unchanged) {
1914-
return;
1915-
}
1916-
1917-
if (!this._setupDone) {
1918-
if (this._renderer.geometryBufferCache.numCached() > 0) {
1919-
p5._friendlyError(
1920-
"Sorry, Could not set the attributes, you need to call setAttributes() " +
1921-
"before calling the other drawing methods in setup()"
1922-
);
1923-
return;
1924-
}
1925-
}
1926-
1927-
await this._renderer._resetContext(null, null, RendererWebGPU);
1928-
1929-
if (this._renderer.states.curCamera) {
1930-
this._renderer.states.curCamera._renderer = this._renderer;
1931-
}
1936+
return this._renderer._setAttributes(key, value);
19321937
}
19331938
}
19341939

test/unit/webgpu/p5.Framebuffer.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ suite('WebGPU p5.Framebuffer', function() {
1616
});
1717

1818
beforeEach(async function() {
19-
const renderer = await myp5.createCanvas(10, 10, 'webgpu');
20-
await myp5.setAttributes({
21-
forceFallbackAdapter: true
22-
});
19+
await myp5.createCanvas(10, 10, 'webgpu');
2320
})
2421

2522
afterAll(function() {

vitest.workspace.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineWorkspace } from 'vitest/config';
22
import vitePluginString from 'vite-plugin-string';
3-
console.log(`CI: ${process.env.CI}`)
43

54
const plugins = [
65
vitePluginString({
@@ -35,7 +34,7 @@ export default defineWorkspace([
3534
'./test/unit/visual/cases/webgpu.js',
3635
'./test/unit/webgpu/*.js',
3736
],
38-
testTimeout: 10000,
37+
testTimeout: 1000,
3938
globals: true,
4039
browser: {
4140
enabled: true,
@@ -84,7 +83,7 @@ export default defineWorkspace([
8483
'./test/unit/visual/visualTest.js',
8584
// './test/unit/visual/cases/webgpu.js',
8685
],
87-
testTimeout: 10000,
86+
testTimeout: 1000,
8887
globals: true,
8988
browser: {
9089
enabled: true,

0 commit comments

Comments
 (0)