Skip to content

Commit

Permalink
WebGPU: Dummy commandbuffer, change swapchain present if building for…
Browse files Browse the repository at this point in the history
… web
  • Loading branch information
Ravbug committed Oct 17, 2023
1 parent b3f2099 commit efdb444
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
61 changes: 61 additions & 0 deletions src/WGCommandBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#if RGL_WEBGPU_AVAILABLE
#include "WGCommandBuffer.hpp"
namespace RGL{
CommandBufferWG::CommandBufferWG(decltype(owningQueue) owningQueue) : owningQueue(owningQueue) {


}
CommandBufferWG::~CommandBufferWG(){

}

// ICommandBuffer
void CommandBufferWG::Reset() { }
void CommandBufferWG::Begin() { }
void CommandBufferWG::End() { }
void CommandBufferWG::BindRenderPipeline(RGLRenderPipelinePtr) { }
void CommandBufferWG::BeginCompute(RGLComputePipelinePtr) { }
void CommandBufferWG::EndCompute() { }
void CommandBufferWG::DispatchCompute(uint32_t threadsX, uint32_t threadsY, uint32_t threadsZ, uint32_t threadsPerThreadgroupX, uint32_t threadsPerThreadgroupY, uint32_t threadsPerThreadgroupZ) { }

void CommandBufferWG::BeginRendering(RGLRenderPassPtr) { }
void CommandBufferWG::EndRendering() { }

void CommandBufferWG::BindBuffer(RGLBufferPtr buffer, uint32_t binding, uint32_t offsetIntoBuffer) { }
void CommandBufferWG::BindComputeBuffer(RGLBufferPtr buffer, uint32_t binding, uint32_t offsetIntoBuffer) { }
void CommandBufferWG::SetVertexBuffer(RGLBufferPtr buffer, const VertexBufferBinding& bindingInfo) { }

void CommandBufferWG::SetIndexBuffer(RGLBufferPtr buffer) { }

void CommandBufferWG::SetVertexBytes(const untyped_span data, uint32_t offset) { }
void CommandBufferWG::SetFragmentBytes(const untyped_span data, uint32_t offset) { }
void CommandBufferWG::SetComputeBytes(const untyped_span data, uint32_t offset) { }

void CommandBufferWG::SetVertexSampler(RGLSamplerPtr sampler, uint32_t index) { }
void CommandBufferWG::SetFragmentSampler(RGLSamplerPtr sampler, uint32_t index) { }

void CommandBufferWG::SetVertexTexture(const ITexture* texture, uint32_t index) { }
void CommandBufferWG::SetFragmentTexture(const ITexture* texture, uint32_t index) { }

void CommandBufferWG::Draw(uint32_t nVertices, const DrawInstancedConfig&) { }
void CommandBufferWG::DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig&) { }

void CommandBufferWG::SetViewport(const Viewport&) { }
void CommandBufferWG::SetScissor(const Rect&) { }

void CommandBufferWG::CopyTextureToBuffer(RGL::ITexture* sourceTexture, const Rect& sourceRect, size_t offset, RGLBufferPtr desetBuffer) { }
void CommandBufferWG::CopyBufferToBuffer(BufferCopyConfig from, BufferCopyConfig to, uint32_t size) { }

void CommandBufferWG::Commit(const CommitConfig&) { }

void CommandBufferWG::ExecuteIndirectIndexed(const IndirectConfig&) { }
void CommandBufferWG::ExecuteIndirect(const IndirectConfig&) { }

void CommandBufferWG::BeginRenderDebugMarker(const std::string& label) { }
void CommandBufferWG::BeginComputeDebugMarker(const std::string& label) { }

void CommandBufferWG::EndRenderDebugMarker() { }
void CommandBufferWG::EndComputeDebugMarker() { }
}

#endif
62 changes: 62 additions & 0 deletions src/WGCommandBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once
#include <RGL/CommandBuffer.hpp>

namespace RGL{
struct CommandQueueWG;

struct CommandBufferWG : public ICommandBuffer{

const std::shared_ptr<CommandQueueWG> owningQueue;
CommandBufferWG(decltype(owningQueue));

// ICommandBuffer
void Reset() final;
void Begin() final;
void End() final;
void BindRenderPipeline(RGLRenderPipelinePtr) final;
void BeginCompute(RGLComputePipelinePtr) final;
void EndCompute() final;
void DispatchCompute(uint32_t threadsX, uint32_t threadsY, uint32_t threadsZ, uint32_t threadsPerThreadgroupX=1, uint32_t threadsPerThreadgroupY=1, uint32_t threadsPerThreadgroupZ=1) final;

void BeginRendering(RGLRenderPassPtr) final;
void EndRendering() final;

void BindBuffer(RGLBufferPtr buffer, uint32_t binding, uint32_t offsetIntoBuffer = 0) final;
void BindComputeBuffer(RGLBufferPtr buffer, uint32_t binding, uint32_t offsetIntoBuffer = 0) final;
void SetVertexBuffer(RGLBufferPtr buffer, const VertexBufferBinding& bindingInfo = {}) final;

void SetIndexBuffer(RGLBufferPtr buffer) final;

void SetVertexBytes(const untyped_span data, uint32_t offset) final;
void SetFragmentBytes(const untyped_span data, uint32_t offset) final;
void SetComputeBytes(const untyped_span data, uint32_t offset) final;

void SetVertexSampler(RGLSamplerPtr sampler, uint32_t index) final;
void SetFragmentSampler(RGLSamplerPtr sampler, uint32_t index) final;

void SetVertexTexture(const ITexture* texture, uint32_t index) final;
void SetFragmentTexture(const ITexture* texture, uint32_t index) final;

void Draw(uint32_t nVertices, const DrawInstancedConfig& = {}) final;
void DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig& = {}) final;

void SetViewport(const Viewport&) final;
void SetScissor(const Rect&) final;

void CopyTextureToBuffer(RGL::ITexture* sourceTexture, const Rect& sourceRect, size_t offset, RGLBufferPtr desetBuffer) final;
void CopyBufferToBuffer(BufferCopyConfig from, BufferCopyConfig to, uint32_t size) final;

void Commit(const CommitConfig&) final;

void ExecuteIndirectIndexed(const IndirectConfig&) final;
void ExecuteIndirect(const IndirectConfig&) final;

virtual ~CommandBufferWG();

void BeginRenderDebugMarker(const std::string& label) final;
void BeginComputeDebugMarker(const std::string& label) final;

void EndRenderDebugMarker() final;
void EndComputeDebugMarker() final;
};
}
3 changes: 2 additions & 1 deletion src/WGCommandQueue.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if RGL_WEBGPU_AVAILABLE
#include "WGCommandQueue.hpp"
#include "WGCommandBuffer.hpp"
#include "WGDevice.hpp"
#include <iostream>

Expand All @@ -9,7 +10,7 @@ namespace RGL{
}

RGLCommandBufferPtr CommandQueueWG::CreateCommandBuffer(){
return nullptr;
return std::make_shared<CommandBufferWG>(shared_from_this());
}

QueueData CommandQueueWG::GetQueueData(){
Expand Down
3 changes: 2 additions & 1 deletion src/WGCommandQueue.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once
#include <RGL/CommandQueue.hpp>
#include <emscripten/html5_webgpu.h>
#include <memory>

namespace RGL{
struct DeviceWG;

struct CommandQueueWG : public ICommandQueue{
struct CommandQueueWG : public ICommandQueue, public std::enable_shared_from_this<CommandQueueWG>{
WGPUQueue queue = nullptr;
const std::shared_ptr<DeviceWG> owningDevice;
CommandQueueWG(decltype(owningDevice));
Expand Down
9 changes: 8 additions & 1 deletion src/WGSwapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "WGSwapchain.hpp"
#include "WGDevice.hpp"
#include <emscripten/html5_webgpu.h>
#include <emscripten/html5.h>
#include "RGLCommon.hpp"

namespace RGL{
Expand Down Expand Up @@ -46,7 +47,13 @@ namespace RGL{
}

void SwapchainWG::Present(const SwapchainPresentConfig&){
wgpuSwapChainPresent(swapchain);
#if __EMSCRIPTEN__
emscripten_request_animation_frame([](double time, void* userData){
return 0;
},nullptr);
#else
wgpuSwapChainPresent(swapchain);
#endif
}

void SwapchainWG::SetVsyncMode(bool mode){
Expand Down

0 comments on commit efdb444

Please sign in to comment.