@@ -291,7 +291,21 @@ RenderingDeviceDriver::TextureID RenderingDeviceDriverWebGpu::texture_create(con
291
291
WGPUTexture texture = wgpuDeviceCreateTexture (device, &texture_desc);
292
292
WGPUTextureFormat format = webgpu_texture_format_from_rd (p_view.format );
293
293
294
+ WGPUTextureViewDescriptorExtras texture_view_desc_extras = (WGPUTextureViewDescriptorExtras){
295
+ .chain = (WGPUChainedStruct){
296
+ .next = nullptr ,
297
+ .sType = (WGPUSType)WGPUSType_TextureViewDescriptorExtras,
298
+ },
299
+ .swizzle = (WGPUTextureViewSwizzle){
300
+ .r = webgpu_component_swizzle_from_rd (p_view.swizzle_r ),
301
+ .g = webgpu_component_swizzle_from_rd (p_view.swizzle_g ),
302
+ .b = webgpu_component_swizzle_from_rd (p_view.swizzle_b ),
303
+ .a = webgpu_component_swizzle_from_rd (p_view.swizzle_a ),
304
+ },
305
+ };
306
+
294
307
WGPUTextureViewDescriptor texture_view_desc = (WGPUTextureViewDescriptor){
308
+ .nextInChain = (WGPUChainedStruct *)&texture_view_desc_extras,
295
309
.format = format,
296
310
.dimension = webgpu_texture_view_dimension_from_rd (p_format.texture_type ),
297
311
.mipLevelCount = texture_desc.mipLevelCount ,
@@ -333,7 +347,21 @@ RenderingDeviceDriver::TextureID RenderingDeviceDriverWebGpu::texture_create_sha
333
347
}
334
348
}
335
349
350
+ WGPUTextureViewDescriptorExtras texture_view_desc_extras = (WGPUTextureViewDescriptorExtras){
351
+ .chain = (WGPUChainedStruct){
352
+ .next = nullptr ,
353
+ .sType = (WGPUSType)WGPUSType_TextureViewDescriptorExtras,
354
+ },
355
+ .swizzle = (WGPUTextureViewSwizzle){
356
+ .r = webgpu_component_swizzle_from_rd (p_view.swizzle_r ),
357
+ .g = webgpu_component_swizzle_from_rd (p_view.swizzle_g ),
358
+ .b = webgpu_component_swizzle_from_rd (p_view.swizzle_b ),
359
+ .a = webgpu_component_swizzle_from_rd (p_view.swizzle_a ),
360
+ },
361
+ };
362
+
336
363
WGPUTextureViewDescriptor texture_view_desc = (WGPUTextureViewDescriptor){
364
+ .nextInChain = (WGPUChainedStruct *)&texture_view_desc_extras,
337
365
.format = webgpu_texture_format_from_rd (p_view.format ),
338
366
.mipLevelCount = texture_info->mip_level_count ,
339
367
.arrayLayerCount = texture_info->is_using_depth ? 1 : texture_info->depth_or_array ,
@@ -353,7 +381,21 @@ RenderingDeviceDriver::TextureID RenderingDeviceDriverWebGpu::texture_create_sha
353
381
RenderingDeviceDriver::TextureID RenderingDeviceDriverWebGpu::texture_create_shared_from_slice (TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) {
354
382
TextureInfo *texture_info = (TextureInfo *)p_original_texture.id ;
355
383
384
+ WGPUTextureViewDescriptorExtras texture_view_desc_extras = (WGPUTextureViewDescriptorExtras){
385
+ .chain = (WGPUChainedStruct){
386
+ .next = nullptr ,
387
+ .sType = (WGPUSType)WGPUSType_TextureViewDescriptorExtras,
388
+ },
389
+ .swizzle = (WGPUTextureViewSwizzle){
390
+ .r = webgpu_component_swizzle_from_rd (p_view.swizzle_r ),
391
+ .g = webgpu_component_swizzle_from_rd (p_view.swizzle_g ),
392
+ .b = webgpu_component_swizzle_from_rd (p_view.swizzle_b ),
393
+ .a = webgpu_component_swizzle_from_rd (p_view.swizzle_a ),
394
+ },
395
+ };
396
+
356
397
WGPUTextureViewDescriptor texture_view_desc = (WGPUTextureViewDescriptor){
398
+ .nextInChain = (WGPUChainedStruct *)&texture_view_desc_extras,
357
399
.format = webgpu_texture_format_from_rd (p_view.format ),
358
400
.baseMipLevel = p_mipmap,
359
401
.mipLevelCount = p_mipmaps,
@@ -462,32 +504,37 @@ bool RenderingDeviceDriverWebGpu::sampler_is_format_supported_for_filter(DataFor
462
504
463
505
// NOTE: The attributes in `p_vertex_attribs` must be in order.
464
506
RenderingDeviceDriver::VertexFormatID RenderingDeviceDriverWebGpu::vertex_format_create (VectorView<VertexAttribute> p_vertex_attribs) {
465
- WGPUVertexAttribute *vertex_attributes = memnew_arr (WGPUVertexAttribute, p_vertex_attribs.size ());
466
- uint64_t array_stride = 0 ;
467
- for (int i = 0 ; i < p_vertex_attribs.size (); i++) {
507
+ VertexFormatInfo *vertex_format_info = memnew (VertexFormatInfo);
508
+ vertex_format_info->layouts .resize_zeroed (p_vertex_attribs.size ());
509
+ vertex_format_info->vertex_attributes .resize_zeroed (p_vertex_attribs.size ());
510
+
511
+ for (uint32_t i = 0 ; i < p_vertex_attribs.size (); i++) {
468
512
VertexAttribute attrib = p_vertex_attribs[i];
469
- vertex_attributes[i] = (WGPUVertexAttribute){
470
- .format = webgpu_vertex_format_from_rd (attrib.format ),
471
- .offset = attrib.offset ,
472
- .shaderLocation = attrib.location ,
513
+
514
+ vertex_format_info->vertex_attributes .set (i,
515
+ (WGPUVertexAttribute){
516
+ .format = webgpu_vertex_format_from_rd (attrib.format ),
517
+ .offset = attrib.offset ,
518
+ .shaderLocation = attrib.location ,
519
+ });
520
+
521
+ WGPUVertexStepMode step_mode = attrib.frequency == VertexFrequency::VERTEX_FREQUENCY_VERTEX ? WGPUVertexStepMode_Vertex : WGPUVertexStepMode_Instance;
522
+
523
+ WGPUVertexBufferLayout layout = (WGPUVertexBufferLayout){
524
+ .stepMode = step_mode,
525
+ .arrayStride = attrib.stride ,
526
+ .attributeCount = 1 ,
527
+ .attributes = vertex_format_info->vertex_attributes .ptr () + i,
473
528
};
474
- array_stride += attrib. stride ;
529
+ vertex_format_info-> layouts . set (i, layout) ;
475
530
}
476
531
477
- WGPUVertexStepMode step_mode = p_vertex_attribs[0 ].frequency == VertexFrequency::VERTEX_FREQUENCY_VERTEX ? WGPUVertexStepMode_Vertex : WGPUVertexStepMode_Instance;
478
-
479
- WGPUVertexBufferLayout *layout = memnew (WGPUVertexBufferLayout);
480
- layout->attributes = vertex_attributes;
481
- layout->attributeCount = p_vertex_attribs.size ();
482
- layout->stepMode = step_mode;
483
- layout->arrayStride = array_stride;
484
- return VertexFormatID (layout);
532
+ return VertexFormatID (vertex_format_info);
485
533
}
486
534
487
535
void RenderingDeviceDriverWebGpu::vertex_format_free (VertexFormatID p_vertex_format) {
488
- WGPUVertexBufferLayout *layout = (WGPUVertexBufferLayout *)p_vertex_format.id ;
489
- memdelete_arr (layout->attributes );
490
- memdelete (layout);
536
+ VertexFormatInfo *vertex_format_info = (VertexFormatInfo *)p_vertex_format.id ;
537
+ memdelete (vertex_format_info);
491
538
}
492
539
493
540
/* *****************/
@@ -556,7 +603,7 @@ Error RenderingDeviceDriverWebGpu::command_queue_execute_and_present(CommandQueu
556
603
Vector<WGPUCommandBuffer> commands = Vector<WGPUCommandBuffer>();
557
604
558
605
for (uint32_t i = 0 ; i < p_cmd_buffers.size (); i++) {
559
- CommandBufferInfo* command_buffer_info = (CommandBufferInfo*)p_cmd_buffers[i].id ;
606
+ CommandBufferInfo * command_buffer_info = (CommandBufferInfo *)p_cmd_buffers[i].id ;
560
607
561
608
DEV_ASSERT (command_buffer_info != nullptr );
562
609
DEV_ASSERT (command_buffer_info->encoder != nullptr );
@@ -698,16 +745,10 @@ RenderingDeviceDriver::SwapChainID RenderingDeviceDriverWebGpu::swap_chain_creat
698
745
699
746
RenderPassInfo *render_pass_info = memnew (RenderPassInfo);
700
747
701
- // NOTE: This is not the best way of getting the format of the surface.
702
- WGPUSurfaceCapabilities capabilities;
703
- wgpuSurfaceGetCapabilities (surface->surface , this ->adapter , &capabilities);
704
- WGPUTextureFormat surface_format = capabilities.formats [0 ];
705
- surface->format = surface_format;
706
-
707
748
surface->configure (this ->adapter , this ->device );
708
749
709
750
render_pass_info->attachments = Vector<RenderPassAttachmentInfo>({ (RenderPassAttachmentInfo){
710
- .format = surface_format ,
751
+ .format = surface-> format ,
711
752
.sample_count = 1 ,
712
753
.load_op = WGPULoadOp_Clear,
713
754
.store_op = WGPUStoreOp_Store,
@@ -746,13 +787,11 @@ RenderingDeviceDriver::RenderPassID RenderingDeviceDriverWebGpu::swap_chain_get_
746
787
return swapchain_info->render_pass ;
747
788
}
748
789
749
- // NOTE: In theory, this function's result doesn't matter.
750
- // We take this to create a framebuffer attachment that we never end up using since WebGpu does not support framebuffers.
751
790
RenderingDeviceDriver::DataFormat RenderingDeviceDriverWebGpu::swap_chain_get_format (SwapChainID p_swap_chain) {
752
791
SwapChainInfo *swapchain_info = (SwapChainInfo *)p_swap_chain.id ;
753
- RenderingContextDriverWebGpu::Surface *_ = (RenderingContextDriverWebGpu::Surface *)swapchain_info->surface ;
754
- // TODO: impl Replace this with a proper conversion
755
- return DATA_FORMAT_B8G8R8A8_SRGB ;
792
+ RenderingContextDriverWebGpu::Surface *surface = (RenderingContextDriverWebGpu::Surface *)swapchain_info->surface ;
793
+
794
+ return surface-> rd_format ;
756
795
}
757
796
758
797
void RenderingDeviceDriverWebGpu::swap_chain_free (SwapChainID p_swap_chain) {
@@ -1879,17 +1918,19 @@ void RenderingDeviceDriverWebGpu::command_render_set_viewport(CommandBufferID p_
1879
1918
1880
1919
ERR_FAIL_COND_MSG (p_viewports.size () != 1 , " WebGpu cannot set multiple viewports." );
1881
1920
1882
- command_buffer_info->active_render_pass_info .commands .push_back (((RenderPassEncoderCommand){
1883
- .type = RenderPassEncoderCommand::CommandType::SET_VIEWPORT,
1884
- .set_viewport = (RenderPassEncoderCommand::SetViewport){
1885
- .x = (float )p_viewports[0 ].position .x ,
1886
- .y = (float )p_viewports[0 ].position .y ,
1887
- .width = (float )p_viewports[0 ].size .x ,
1888
- .height = (float )p_viewports[0 ].size .y ,
1889
- .min_depth = 0.0 ,
1890
- .max_depth = 1.0 ,
1891
- },
1892
- }));
1921
+ for (int i = 0 ; i < p_viewports.size (); i++) {
1922
+ command_buffer_info->active_render_pass_info .commands .push_back (((RenderPassEncoderCommand){
1923
+ .type = RenderPassEncoderCommand::CommandType::SET_VIEWPORT,
1924
+ .set_viewport = (RenderPassEncoderCommand::SetViewport){
1925
+ .x = (float )p_viewports[i].position .x ,
1926
+ .y = (float )p_viewports[i].position .y ,
1927
+ .width = (float )p_viewports[i].size .x ,
1928
+ .height = (float )p_viewports[i].size .y ,
1929
+ .min_depth = 0.0 ,
1930
+ .max_depth = 1.0 ,
1931
+ },
1932
+ }));
1933
+ }
1893
1934
}
1894
1935
1895
1936
void RenderingDeviceDriverWebGpu::command_render_set_scissor (CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) {
@@ -1900,15 +1941,17 @@ void RenderingDeviceDriverWebGpu::command_render_set_scissor(CommandBufferID p_c
1900
1941
1901
1942
ERR_FAIL_COND_MSG (p_scissors.size () != 1 , " WebGpu cannot set multiple scissors." );
1902
1943
1903
- command_buffer_info->active_render_pass_info .commands .push_back (((RenderPassEncoderCommand){
1904
- .type = RenderPassEncoderCommand::CommandType::SET_SCISSOR_RECT,
1905
- .set_scissor_rect = (RenderPassEncoderCommand::SetScissorRect){
1906
- .x = (uint32_t )p_scissors[0 ].position .x ,
1907
- .y = (uint32_t )p_scissors[0 ].position .y ,
1908
- .width = (uint32_t )p_scissors[0 ].position .width ,
1909
- .height = (uint32_t )p_scissors[0 ].position .height ,
1910
- },
1911
- }));
1944
+ for (int i = 0 ; i < p_scissors.size (); i++) {
1945
+ command_buffer_info->active_render_pass_info .commands .push_back (((RenderPassEncoderCommand){
1946
+ .type = RenderPassEncoderCommand::CommandType::SET_SCISSOR_RECT,
1947
+ .set_scissor_rect = (RenderPassEncoderCommand::SetScissorRect){
1948
+ .x = (uint32_t )p_scissors[i].position .x ,
1949
+ .y = (uint32_t )p_scissors[i].position .y ,
1950
+ .width = (uint32_t )p_scissors[i].size .width ,
1951
+ .height = (uint32_t )p_scissors[i].size .height ,
1952
+ },
1953
+ }));
1954
+ }
1912
1955
}
1913
1956
1914
1957
void RenderingDeviceDriverWebGpu::command_render_clear_attachments (CommandBufferID _p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) {
@@ -2185,9 +2228,9 @@ RenderingDeviceDriver::PipelineID RenderingDeviceDriverWebGpu::render_pipeline_c
2185
2228
2186
2229
// NOTE: I'm not sure dynamic vertex state is supported.
2187
2230
if (p_vertex_format) {
2188
- WGPUVertexBufferLayout *vertex_buffer_layout = (WGPUVertexBufferLayout *)p_vertex_format.id ;
2189
- vertex_state.buffers = vertex_buffer_layout ;
2190
- vertex_state.bufferCount = 1 ;
2231
+ VertexFormatInfo *format_info = (VertexFormatInfo *)p_vertex_format.id ;
2232
+ vertex_state.buffers = format_info-> layouts . ptr () ;
2233
+ vertex_state.bufferCount = format_info-> layouts . size () ;
2191
2234
}
2192
2235
2193
2236
pipeline_descriptor.vertex = vertex_state;
0 commit comments