WebGPU various small fixes (#2140)

This commit is contained in:
Hugo Amnov
2020-05-11 02:48:25 +02:00
committed by GitHub
parent 1359503a62
commit e1c211b537
2 changed files with 33 additions and 23 deletions

View File

@@ -8,15 +8,14 @@
//#define DAWN_ENABLE_BACKEND_D3D12 //#define DAWN_ENABLE_BACKEND_D3D12
#define DAWN_ENABLE_BACKEND_VULKAN #define DAWN_ENABLE_BACKEND_VULKAN
#define BGFX_CONFIG_DEBUG_ANNOTATION 0
#if BGFX_CONFIG_RENDERER_WEBGPU #if BGFX_CONFIG_RENDERER_WEBGPU
# include "renderer_webgpu.h" # include "renderer_webgpu.h"
# include "renderer_vk.h"
# include "renderer.h" # include "renderer.h"
# include "debug_renderdoc.h" # include "debug_renderdoc.h"
# ifdef DAWN_ENABLE_BACKEND_VULKAN
# include "renderer_vk.h"
# endif // DAWN_ENABLE_BACKEND_VULKAN
# if !BX_PLATFORM_EMSCRIPTEN # if !BX_PLATFORM_EMSCRIPTEN
# ifdef DAWN_ENABLE_BACKEND_D3D12 # ifdef DAWN_ENABLE_BACKEND_D3D12
# include <dawn_native/D3D12Backend.h> # include <dawn_native/D3D12Backend.h>
@@ -1140,6 +1139,7 @@ namespace bgfx { namespace webgpu
m_renderEncoder = rce; m_renderEncoder = rce;
rce.SetViewport(0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f); rce.SetViewport(0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f);
rce.SetScissorRect(0.0f, 0.0f, (float)width, (float)height);
rce.SetPipeline(pso->m_rps); rce.SetPipeline(pso->m_rps);
@@ -1520,7 +1520,7 @@ namespace bgfx { namespace webgpu
} }
} }
void clearQuad(ClearQuad& _clearQuad, const Rect& /*_rect*/, const Clear& _clear, const float _palette[][4]) void clearQuad(ClearQuad& _clearQuad, const Rect& _rect, const Clear& _clear, const float _palette[][4])
{ {
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
@@ -1628,6 +1628,9 @@ namespace bgfx { namespace webgpu
bindProgram(rce, program, bindState, numOffset, offsets); bindProgram(rce, program, bindState, numOffset, offsets);
rce.SetViewport(_rect.m_x, _rect.m_y, _rect.m_width, _rect.m_height, 0.0f, 1.0f);
rce.SetScissorRect(_rect.m_x, _rect.m_y, _rect.m_width, _rect.m_height);
rce.SetVertexBuffer(0, vb.m_ptr); rce.SetVertexBuffer(0, vb.m_ptr);
rce.Draw(4, 1, 0, 0); rce.Draw(4, 1, 0, 0);
} }
@@ -1971,6 +1974,7 @@ namespace bgfx { namespace webgpu
layout.bindGroupLayouts = &program.m_bindGroupLayout; layout.bindGroupLayouts = &program.m_bindGroupLayout;
layout.bindGroupLayoutCount = 1; layout.bindGroupLayoutCount = 1;
BX_TRACE("Creating WebGPU render pipeline layout for program %s", program.m_vsh->name());
pd.desc.layout = m_device.CreatePipelineLayout(&layout); pd.desc.layout = m_device.CreatePipelineLayout(&layout);
// TODO (hugoam) this should be cached too ? // TODO (hugoam) this should be cached too ?
@@ -2110,7 +2114,6 @@ namespace bgfx { namespace webgpu
input.vertexBuffers[stream].attributes = &input.attributes[firstAttrib]; input.vertexBuffers[stream].attributes = &input.attributes[firstAttrib];
} }
input.desc.indexFormat = _index32 ? wgpu::IndexFormat::Uint32 : wgpu::IndexFormat::Uint16; input.desc.indexFormat = _index32 ? wgpu::IndexFormat::Uint32 : wgpu::IndexFormat::Uint16;
pd.desc.vertexState = &input.desc; pd.desc.vertexState = &input.desc;
@@ -2162,12 +2165,14 @@ namespace bgfx { namespace webgpu
layout.bindGroupLayouts = &program.m_bindGroupLayout; layout.bindGroupLayouts = &program.m_bindGroupLayout;
layout.bindGroupLayoutCount = 1; layout.bindGroupLayoutCount = 1;
BX_TRACE("Creating WebGPU render pipeline layout for program %s", program.m_vsh->name());
pso->m_layout = m_device.CreatePipelineLayout(&layout); pso->m_layout = m_device.CreatePipelineLayout(&layout);
wgpu::ComputePipelineDescriptor desc; wgpu::ComputePipelineDescriptor desc;
desc.layout = pso->m_layout; desc.layout = pso->m_layout;
desc.computeStage = { NULL, program.m_vsh->m_module, "main" }; desc.computeStage = { NULL, program.m_vsh->m_module, "main" };
BX_TRACE("Creating WebGPU render pipeline state for program %s", program.m_vsh->name());
pso->m_cps = m_device.CreateComputePipeline(&desc); pso->m_cps = m_device.CreateComputePipeline(&desc);
} }
@@ -2192,7 +2197,7 @@ namespace bgfx { namespace webgpu
desc.magFilter = s_textureFilterMinMag[(_flags&BGFX_SAMPLER_MAG_MASK)>>BGFX_SAMPLER_MAG_SHIFT]; desc.magFilter = s_textureFilterMinMag[(_flags&BGFX_SAMPLER_MAG_MASK)>>BGFX_SAMPLER_MAG_SHIFT];
desc.mipmapFilter = s_textureFilterMip[(_flags&BGFX_SAMPLER_MIP_MASK)>>BGFX_SAMPLER_MIP_SHIFT]; desc.mipmapFilter = s_textureFilterMip[(_flags&BGFX_SAMPLER_MIP_MASK)>>BGFX_SAMPLER_MIP_SHIFT];
desc.lodMinClamp = 0; desc.lodMinClamp = 0;
desc.lodMaxClamp = FLT_MAX; desc.lodMaxClamp = bx::kFloatMax;
const uint32_t cmpFunc = (_flags&BGFX_SAMPLER_COMPARE_MASK)>>BGFX_SAMPLER_COMPARE_SHIFT; const uint32_t cmpFunc = (_flags&BGFX_SAMPLER_COMPARE_MASK)>>BGFX_SAMPLER_COMPARE_SHIFT;
desc.compare = 0 == cmpFunc desc.compare = 0 == cmpFunc
@@ -2680,10 +2685,13 @@ namespace bgfx { namespace webgpu
} }
} }
wgpu::ShaderModuleSPIRVDescriptor spirv;
spirv.code = m_code;
spirv.codeSize = shaderSize / 4;
wgpu::ShaderModuleDescriptor desc; wgpu::ShaderModuleDescriptor desc;
desc.label = getName(_handle); desc.label = getName(_handle);
desc.code = m_code; desc.nextInChain = &spirv;
desc.codeSize = shaderSize/4;
m_module = s_renderWgpu->m_device.CreateShaderModule(&desc); m_module = s_renderWgpu->m_device.CreateShaderModule(&desc);
@@ -2886,7 +2894,7 @@ namespace bgfx { namespace webgpu
wgpu::CreateBufferMappedResult mapped = s_renderWgpu->m_device.CreateBufferMapped(&desc); wgpu::CreateBufferMappedResult mapped = s_renderWgpu->m_device.CreateBufferMapped(&desc);
wgpu::Buffer staging = mapped.buffer; wgpu::Buffer staging = mapped.buffer;
bx::memCopy(mapped.data, m_dynamic, _size); bx::memCopy(mapped.data, m_dynamic, end - start);
mapped.buffer.Unmap(); mapped.buffer.Unmap();
// TODO pad to 4 bytes // TODO pad to 4 bytes
@@ -3461,6 +3469,7 @@ namespace bgfx { namespace webgpu
uint32_t ScratchBufferWgpu::write(void* data, uint64_t _size, uint64_t _offset) uint32_t ScratchBufferWgpu::write(void* data, uint64_t _size, uint64_t _offset)
{ {
BX_CHECK(nullptr != m_staging, "Cannot write uniforms outside of begin()/submit() calls"); BX_CHECK(nullptr != m_staging, "Cannot write uniforms outside of begin()/submit() calls");
BX_CHECK(m_size > m_offset + _offset, "Out-of-bounds scratch buffer write");
uint32_t offset = m_offset; uint32_t offset = m_offset;
bx::memCopy((void*)((uint8_t*)m_staging->m_data + offset), data, _size); bx::memCopy((void*)((uint8_t*)m_staging->m_data + offset), data, _size);
m_offset += _offset; m_offset += _offset;
@@ -3470,6 +3479,7 @@ namespace bgfx { namespace webgpu
uint32_t ScratchBufferWgpu::write(void* data, uint64_t _size) uint32_t ScratchBufferWgpu::write(void* data, uint64_t _size)
{ {
BX_CHECK(nullptr != m_staging, "Cannot write uniforms outside of begin()/submit() calls"); BX_CHECK(nullptr != m_staging, "Cannot write uniforms outside of begin()/submit() calls");
BX_CHECK(m_size > m_offset + _size, "Out-of-bounds scratch buffer write");
uint32_t offset = m_offset; uint32_t offset = m_offset;
bx::memCopy((void*)((uint8_t*)m_staging->m_data + offset), data, _size); bx::memCopy((void*)((uint8_t*)m_staging->m_data + offset), data, _size);
m_offset += _size; m_offset += _size;
@@ -3480,8 +3490,11 @@ namespace bgfx { namespace webgpu
{ {
m_staging->unmap(); m_staging->unmap();
wgpu::CommandEncoder& bce = s_renderWgpu->getStagingEncoder(); if (m_offset != 0)
bce.CopyBufferToBuffer(m_staging->m_buffer, 0, m_buffer, 0, m_offset); {
wgpu::CommandEncoder& bce = s_renderWgpu->getStagingEncoder();
bce.CopyBufferToBuffer(m_staging->m_buffer, 0, m_buffer, 0, m_offset);
}
} }
void ScratchBufferWgpu::release() void ScratchBufferWgpu::release()
@@ -3562,7 +3575,7 @@ namespace bgfx { namespace webgpu
desc.presentMode = wgpu::PresentMode::Immediate; desc.presentMode = wgpu::PresentMode::Immediate;
desc.format = wgpu::TextureFormat::RGBA8Unorm; desc.format = wgpu::TextureFormat::RGBA8Unorm;
desc.implementation = reinterpret_cast<uint64_t>(&m_impl); desc.implementation = reinterpret_cast<uint64_t>(&m_impl);
m_swapChain = _device.CreateSwapChain(NULL, &desc); m_swapChain = _device.CreateSwapChain(nullptr, &desc);
#else #else
wgpu::SurfaceDescriptorFromHTMLCanvasId canvasDesc{}; wgpu::SurfaceDescriptorFromHTMLCanvasId canvasDesc{};
canvasDesc.id = "canvas"; canvasDesc.id = "canvas";
@@ -3571,7 +3584,7 @@ namespace bgfx { namespace webgpu
surfDesc.nextInChain = &canvasDesc; surfDesc.nextInChain = &canvasDesc;
wgpu::Surface surface = wgpu::Instance().CreateSurface(&surfDesc); wgpu::Surface surface = wgpu::Instance().CreateSurface(&surfDesc);
desc.presentMode = wgpu::PresentMode::Immediate; desc.presentMode = wgpu::PresentMode::Fifo;
desc.format = wgpu::TextureFormat::BGRA8Unorm; desc.format = wgpu::TextureFormat::BGRA8Unorm;
m_swapChain = _device.CreateSwapChain(surface, &desc); m_swapChain = _device.CreateSwapChain(surface, &desc);
#endif #endif
@@ -4183,11 +4196,8 @@ namespace bgfx { namespace webgpu
//rce.setTriangleFillMode(wireframe ? MTLTriangleFillModeLines : MTLTriangleFillModeFill); //rce.setTriangleFillMode(wireframe ? MTLTriangleFillModeLines : MTLTriangleFillModeFill);
// TODO (webgpu) check other renderers
const Rect& rect = viewState.m_rect; const Rect& rect = viewState.m_rect;
rce.SetViewport(rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0.0f, 1.0f); rce.SetViewport(rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0.0f, 1.0f);
// can't disable: set to view rect
rce.SetScissorRect(rect.m_x, rect.m_y, rect.m_width, rect.m_height); rce.SetScissorRect(rect.m_x, rect.m_y, rect.m_width, rect.m_height);

View File

@@ -970,6 +970,11 @@ namespace bgfx { namespace spirv
spirv_cross::CompilerReflection refl(spirv); spirv_cross::CompilerReflection refl(spirv);
spirv_cross::ShaderResources resourcesrefl = refl.get_shader_resources(); spirv_cross::ShaderResources resourcesrefl = refl.get_shader_resources();
if (g_verbose)
{
glslang::SpirvToolsDisassemble(std::cout, spirv);
}
// Loop through the separate_images, and extract the uniform names: // Loop through the separate_images, and extract the uniform names:
for (auto &resource : resourcesrefl.separate_images) for (auto &resource : resourcesrefl.separate_images)
{ {
@@ -1060,7 +1065,7 @@ namespace bgfx { namespace spirv
uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding); uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
uniform.name = name; uniform.name = name;
uniform.type = UniformType::End; uniform.type = type;
uniform.regIndex = binding_index; uniform.regIndex = binding_index;
uniform.regCount = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; uniform.regCount = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
break; break;
@@ -1072,11 +1077,6 @@ namespace bgfx { namespace spirv
if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) ) if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) )
{ {
if (g_verbose)
{
glslang::SpirvToolsDisassemble(std::cout, spirv);
}
spirv_cross::CompilerMSL msl(std::move(spirv)); spirv_cross::CompilerMSL msl(std::move(spirv));
spirv_cross::ShaderResources resources = msl.get_shader_resources(); spirv_cross::ShaderResources resources = msl.get_shader_resources();