Skip to content

Commit 7551139

Browse files
committed
WebGPURenderer: Refactor init().
1 parent 4090444 commit 7551139

File tree

1 file changed

+57
-63
lines changed

1 file changed

+57
-63
lines changed

examples/jsm/renderers/webgpu/WebGPURenderer.js

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,64 @@ class WebGPURenderer {
135135

136136
}
137137

138-
init() {
138+
async init() {
139139

140-
return initWebGPU( this );
140+
const parameters = this._parameters;
141+
142+
const adapterOptions = {
143+
powerPreference: parameters.powerPreference
144+
};
145+
146+
const adapter = await navigator.gpu.requestAdapter( adapterOptions );
147+
148+
const deviceDescriptor = {
149+
enabledExtensions: parameters.enabledExtensions,
150+
limits: parameters.limits
151+
};
152+
153+
const device = await adapter.requestDevice( deviceDescriptor );
154+
155+
const glslang = await import( 'https://cdn.jsdelivr.net/npm/@webgpu/[email protected]/dist/web-devel/glslang.js' );
156+
const compiler = await glslang.default();
157+
158+
const context = ( parameters.context !== undefined ) ? parameters.context : this.domElement.getContext( 'gpupresent' );
159+
160+
const swapChain = context.configureSwapChain( {
161+
device: device,
162+
format: GPUTextureFormat.BRGA8Unorm
163+
} );
164+
165+
this._adapter = adapter;
166+
this._device = device;
167+
this._context = context;
168+
this._swapChain = swapChain;
169+
170+
this._info = new WebGPUInfo();
171+
this._properties = new WebGPUProperties();
172+
this._attributes = new WebGPUAttributes( device );
173+
this._geometries = new WebGPUGeometries( this._attributes, this._info );
174+
this._textures = new WebGPUTextures( device, this._properties, this._info, compiler );
175+
this._bindings = new WebGPUBindings( device, this._info, this._properties, this._textures );
176+
this._objects = new WebGPUObjects( this._geometries, this._info );
177+
this._renderPipelines = new WebGPURenderPipelines( device, compiler, this._bindings, parameters.sampleCount );
178+
this._renderLists = new WebGPURenderLists();
179+
this._background = new WebGPUBackground( this );
180+
181+
//
182+
183+
this._renderPassDescriptor = {
184+
colorAttachments: [ {
185+
attachment: null
186+
} ],
187+
depthStencilAttachment: {
188+
attachment: null,
189+
depthStoreOp: GPUStoreOp.Store,
190+
stencilStoreOp: GPUStoreOp.Store
191+
}
192+
};
193+
194+
this._setupColorBuffer();
195+
this._setupDepthBuffer();
141196

142197
}
143198

@@ -774,65 +829,4 @@ class WebGPURenderer {
774829

775830
}
776831

777-
async function initWebGPU( scope ) {
778-
779-
const parameters = scope._parameters;
780-
781-
const adapterOptions = {
782-
powerPreference: parameters.powerPreference
783-
};
784-
785-
const adapter = await navigator.gpu.requestAdapter( adapterOptions );
786-
787-
const deviceDescriptor = {
788-
enabledExtensions: parameters.enabledExtensions,
789-
limits: parameters.limits
790-
};
791-
792-
const device = await adapter.requestDevice( deviceDescriptor );
793-
794-
const glslang = await import( 'https://cdn.jsdelivr.net/npm/@webgpu/[email protected]/dist/web-devel/glslang.js' );
795-
const compiler = await glslang.default();
796-
797-
const context = ( parameters.context !== undefined ) ? parameters.context : scope.domElement.getContext( 'gpupresent' );
798-
799-
const swapChain = context.configureSwapChain( {
800-
device: device,
801-
format: GPUTextureFormat.BRGA8Unorm
802-
} );
803-
804-
scope._adapter = adapter;
805-
scope._device = device;
806-
scope._context = context;
807-
scope._swapChain = swapChain;
808-
809-
scope._info = new WebGPUInfo();
810-
scope._properties = new WebGPUProperties();
811-
scope._attributes = new WebGPUAttributes( device );
812-
scope._geometries = new WebGPUGeometries( scope._attributes, scope._info );
813-
scope._textures = new WebGPUTextures( device, scope._properties, scope._info, compiler );
814-
scope._bindings = new WebGPUBindings( device, scope._info, scope._properties, scope._textures );
815-
scope._objects = new WebGPUObjects( scope._geometries, scope._info );
816-
scope._renderPipelines = new WebGPURenderPipelines( device, compiler, scope._bindings, parameters.sampleCount );
817-
scope._renderLists = new WebGPURenderLists();
818-
scope._background = new WebGPUBackground( scope );
819-
820-
//
821-
822-
scope._renderPassDescriptor = {
823-
colorAttachments: [ {
824-
attachment: null
825-
} ],
826-
depthStencilAttachment: {
827-
attachment: null,
828-
depthStoreOp: GPUStoreOp.Store,
829-
stencilStoreOp: GPUStoreOp.Store
830-
}
831-
};
832-
833-
scope._setupColorBuffer();
834-
scope._setupDepthBuffer();
835-
836-
}
837-
838832
export default WebGPURenderer;

0 commit comments

Comments
 (0)