Skip to content

Commit

Permalink
webgpu update (#16184)
Browse files Browse the repository at this point in the history
  • Loading branch information
hana-alice authored Sep 8, 2023
1 parent dee8822 commit 91ce03d
Show file tree
Hide file tree
Showing 29 changed files with 599 additions and 526 deletions.
5 changes: 3 additions & 2 deletions native/cocos/renderer/gfx-agent/DescriptorSetAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,17 @@ void DescriptorSetAgent::forceUpdate() {
});
}

void DescriptorSetAgent::bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index) {
void DescriptorSetAgent::bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index, AccessFlags flags) {
DescriptorSet::bindBuffer(binding, buffer, index);

ENQUEUE_MESSAGE_4(
ENQUEUE_MESSAGE_5(
DeviceAgent::getInstance()->getMessageQueue(),
DescriptorSetBindBuffer,
actor, getActor(),
binding, binding,
buffer, static_cast<BufferAgent *>(buffer)->getActor(),
index, index,
flags, flags,
{
actor->bindBuffer(binding, buffer, index);
});
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/DescriptorSetAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CC_DLL DescriptorSetAgent final : public Agent<DescriptorSet> {
void update() override;
void forceUpdate() override;

void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index) override;
void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index, AccessFlags flags) override;
void bindTexture(uint32_t binding, Texture *texture, uint32_t index, AccessFlags flags) override;
void bindSampler(uint32_t binding, Sampler *sampler, uint32_t index) override;

Expand Down
19 changes: 12 additions & 7 deletions native/cocos/renderer/gfx-base/GFXDescriptorSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ void DescriptorSet::destroy() {
}

void DescriptorSet::bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index) {
const uint32_t descriptorIndex = _layout->getDescriptorIndices()[binding] + index;
const uint32_t newId = getObjectID(buffer);
if (_buffers[descriptorIndex].id != newId) {
_buffers[descriptorIndex].ptr = buffer;
_buffers[descriptorIndex].id = newId;
_isDirty = true;
}
bindBuffer(binding, buffer, index, AccessFlagBit::NONE);
}

void DescriptorSet::bindTexture(uint32_t binding, Texture *texture, uint32_t index) {
Expand All @@ -85,6 +79,17 @@ void DescriptorSet::bindTexture(uint32_t binding, Texture *texture, uint32_t ind
}
}

void DescriptorSet::bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index, AccessFlags flags) {
const uint32_t descriptorIndex = _layout->getDescriptorIndices()[binding] + index;
const uint32_t newId = getObjectID(buffer);
if (_buffers[descriptorIndex].id != newId) {
_buffers[descriptorIndex].ptr = buffer;
_buffers[descriptorIndex].id = newId;
_buffers[descriptorIndex].flags = flags;
_isDirty = true;
}
}

void DescriptorSet::bindSampler(uint32_t binding, Sampler *sampler, uint32_t index) {
const uint32_t descriptorIndex = _layout->getDescriptorIndices()[binding] + index;
const uint32_t newId = getObjectID(sampler);
Expand Down
3 changes: 2 additions & 1 deletion native/cocos/renderer/gfx-base/GFXDescriptorSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ class CC_DLL DescriptorSet : public GFXObject, public RefCounted {
virtual void update() = 0;
virtual void forceUpdate() = 0;

virtual void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index);
virtual void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index, AccessFlags flags);
virtual void bindSampler(uint32_t binding, Sampler *sampler, uint32_t index);
virtual void bindTexture(uint32_t binding, Texture *texture, uint32_t index, AccessFlags flags);

void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index);
void bindTexture(uint32_t binding, Texture *texture, uint32_t index);

// Functions invoked by JSB adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void DescriptorSetValidator::updateReferenceStamp() {
_referenceStamp = DeviceValidator::getInstance()->currentFrame();
}

void DescriptorSetValidator::bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index) {
void DescriptorSetValidator::bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index, AccessFlags flags) {
CC_ASSERT(isInited());
auto *vBuffer = static_cast<BufferValidator *>(buffer);
CC_ASSERT(buffer && vBuffer->isInited());
Expand All @@ -139,9 +139,9 @@ void DescriptorSetValidator::bindBuffer(uint32_t binding, Buffer *buffer, uint32

/////////// execute ///////////

DescriptorSet::bindBuffer(binding, buffer, index);
DescriptorSet::bindBuffer(binding, buffer, index, flags);

_actor->bindBuffer(binding, vBuffer->getActor(), index);
_actor->bindBuffer(binding, vBuffer->getActor(), index, flags);
}

void DescriptorSetValidator::bindTexture(uint32_t binding, Texture *texture, uint32_t index, AccessFlags flags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CC_DLL DescriptorSetValidator final : public Agent<DescriptorSet> {
void update() override;
void forceUpdate() override;

void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index) override;
void bindBuffer(uint32_t binding, Buffer *buffer, uint32_t index, AccessFlags flags) override;
void bindTexture(uint32_t binding, Texture *texture, uint32_t index, AccessFlags flags) override;
void bindSampler(uint32_t binding, Sampler *sampler, uint32_t index) override;

Expand Down
19 changes: 14 additions & 5 deletions native/cocos/renderer/gfx-wgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8)
set(APP_NAME "webgpu" CACHE STRING "Project Name")
project(${APP_NAME}_wasm)

message(${CMAKE_BUILD_TYPE})
if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
endif()
Expand All @@ -11,11 +12,13 @@ set(ENGINE_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)

include(${ENGINE_ROOT_DIR}/cmake/predefine.cmake)

# include (${ENGINE_ROOT_DIR}/external/CMakeLists.txt)
include (${ENGINE_ROOT_DIR}/external/CMakeLists.txt)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wextra -Wno-nonportable-include-path -fno-exceptions -v")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g3 -D_DEBUG=1 -Wno-unused -O0 -std=c++17")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -w -DNDEBUG=1 -O3 -std=c++17")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wextra -Wno-nonportable-include-path -fno-exceptions -v -D_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES=D_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g3 -D_DEBUG=1 -Wno-unused -O0 -std=c++17 -D_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -w -DNDEBUG=1 -O3 -std=c++17 -D_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES")

message(${CMAKE_BUILD_TYPE})

AUX_SOURCE_DIRECTORY(
./states
Expand Down Expand Up @@ -100,7 +103,7 @@ AUX_SOURCE_DIRECTORY(
add_executable(${APP_NAME}_wasm ${WASM_EXPORTS} ${NATIVE_SRC})

target_include_directories(
${APP_NAME}_wasm PRIVATE "${CMAKE_CURRENT_LIST_DIR}/inc"
${APP_NAME}_wasm PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include"
${CC_EXTERNAL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
${ENGINE_ROOT_DIR}
Expand All @@ -122,4 +125,10 @@ set(EMS_LINK_FLAGS
"-flto --bind --no-entry -O3 -s USE_ES6_IMPORT_META=0 -s EXPORT_ES6=1 -s MODULARIZE=1 -s EXPORT_NAME='wasmDevice' -s ENVIRONMENT=web -s WASM=1 -s USE_WEBGPU=1 -s NO_EXIT_RUNTIME=1 -s LLD_REPORT_UNDEFINED -s ALLOW_MEMORY_GROWTH=1"
)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
string(APPEND EMS_LINK_FLAGS " -s ASSERTIONS=2")
endif()

set_target_properties(${APP_NAME}_wasm PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ENGINE_ROOT_DIR}/external/emscripten/webgpu)

set_target_properties(${APP_NAME}_wasm PROPERTIES CXX_STANDARD 17 LINK_FLAGS ${EMS_LINK_FLAGS})
8 changes: 5 additions & 3 deletions native/cocos/renderer/gfx-wgpu/WGPUBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
#include "WGPUDevice.h"
#include "WGPUObject.h"
#include "WGPUUtils.h"
#include <boost/align/align_up.hpp>

namespace cc {
namespace gfx {

namespace {
CCWGPUBuffer *dftUniformBuffer = nullptr;
CCWGPUBuffer *dftStorageBuffer = nullptr;
static uint32_t BUFFER_ALIGNMENT = 16;
} // namespace

using namespace emscripten;
Expand All @@ -54,7 +56,7 @@ void CCWGPUBuffer::doInit(const BufferInfo &info) {
_gpuBufferObject->indirectObjs.resize(drawInfoCount);
}

_size = ceil(info.size / 4.0) * 4;
_size = boost::alignment::align_up(_size, BUFFER_ALIGNMENT);

WGPUBufferDescriptor descriptor = {
.nextInChain = nullptr,
Expand Down Expand Up @@ -113,7 +115,7 @@ void CCWGPUBuffer::doResize(uint32_t size, uint32_t count) {
_gpuBufferObject->indirectObjs.resize(drawInfoCount);
}

_size = ceil(size / 4.0) * 4;
_size = boost::alignment::align_up(size, BUFFER_ALIGNMENT);

WGPUBufferDescriptor descriptor = {
.nextInChain = nullptr,
Expand Down Expand Up @@ -172,7 +174,7 @@ void CCWGPUBuffer::update(const void *buffer, uint32_t size) {
update(drawInfo, drawInfoCount);
} else {
size_t offset = _isBufferView ? _offset : 0;
uint32_t alignedSize = ceil(size / 4.0) * 4;
uint32_t alignedSize = boost::alignment::align_up(size, BUFFER_ALIGNMENT);
size_t buffSize = alignedSize;
wgpuQueueWriteBuffer(CCWGPUDevice::getInstance()->gpuDeviceObject()->wgpuQueue, _gpuBufferObject->wgpuBuffer, offset, buffer, buffSize);
}
Expand Down
Loading

0 comments on commit 91ce03d

Please sign in to comment.