Skip to content

Commit

Permalink
Update AppDemoWebGPU.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
hudrima1 committed Dec 19, 2023
1 parent e42fd46 commit 5ccb7b1
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions apps/app_demo_webgpu/AppDemoWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ void handleDeviceRequest(WGPURequestDeviceStatus status,
void initWebGPU(WebGPUDemoApp& app)
{
// === Create a WebGPU instance ===
// The instance is the root interface to WebGPU through which we create all other WebGPU resources.
// The instance is the root interface to WebGPU through which we create
// all other WebGPU resources.

app.instance = wgpuCreateInstance(nullptr);
WEBGPU_DEMO_CHECK(app.instance, "[WebGPU] Failed to create instance");
Expand Down Expand Up @@ -591,7 +592,10 @@ void initWebGPU(WebGPUDemoApp& app)
deviceDesc.requiredLimits = &requiredLimits;
deviceDesc.defaultQueue.label = "Demo Queue";

wgpuAdapterRequestDevice(app.adapter, &deviceDesc, handleDeviceRequest, &app.device);
wgpuAdapterRequestDevice(app.adapter,
&deviceDesc,
handleDeviceRequest,
&app.device);

// === Acquire a WebGPU queue ===
// The queue is where the commands for the GPU are submitted to.
Expand Down Expand Up @@ -633,10 +637,14 @@ void initWebGPU(WebGPUDemoApp& app)

// Query the surface capabilities from the adapter.
WGPUSurfaceCapabilities surfaceCapabilities;
wgpuSurfaceGetCapabilities(app.surface, app.adapter, &surfaceCapabilities);
wgpuSurfaceGetCapabilities(app.surface,
app.adapter,
&surfaceCapabilities);

// Get the window size from the GLFW window.
glfwGetFramebufferSize(app.window, &app.surfaceWidth, &app.surfaceHeight);
glfwGetFramebufferSize(app.window,
&app.surfaceWidth,
&app.surfaceHeight);

app.surfaceConfig = {};
app.surfaceConfig.device = app.device;
Expand Down Expand Up @@ -706,7 +714,11 @@ void initWebGPU(WebGPUDemoApp& app)
WEBGPU_DEMO_LOG("[WebGPU] Vertex buffer created");

// Upload the data to the GPU.
wgpuQueueWriteBuffer(app.queue, app.vertexBuffer, 0, vertexData.data(), app.vertexDataSize);
wgpuQueueWriteBuffer(app.queue,
app.vertexBuffer,
0,
vertexData.data(),
app.vertexDataSize);

// === Create the index buffer ===

Expand Down Expand Up @@ -734,7 +746,11 @@ void initWebGPU(WebGPUDemoApp& app)
WEBGPU_DEMO_CHECK(app.indexBuffer, "[WebGPU] Failed to create index buffer");
WEBGPU_DEMO_LOG("[WebGPU] Index buffer created");

wgpuQueueWriteBuffer(app.queue, app.indexBuffer, 0, indexData.data(), app.indexDataSize);
wgpuQueueWriteBuffer(app.queue,
app.indexBuffer,
0,
indexData.data(),
app.indexDataSize);

// === Create the uniform buffer ===

Expand All @@ -748,7 +764,11 @@ void initWebGPU(WebGPUDemoApp& app)
WEBGPU_DEMO_LOG("[WebGPU] Uniform buffer created");

ShaderUniformData uniformData = {};
wgpuQueueWriteBuffer(app.queue, app.uniformBuffer, 0, &uniformData, sizeof(ShaderUniformData));
wgpuQueueWriteBuffer(app.queue,
app.uniformBuffer,
0,
&uniformData,
sizeof(ShaderUniformData));

// === Create the depth texture ===

Expand Down Expand Up @@ -832,7 +852,8 @@ void initWebGPU(WebGPUDemoApp& app)
for (unsigned mipLevel = 0; mipLevel < textureDesc.mipLevelCount; mipLevel++)
{
cv::Mat mipLevelImage;
cv::Size cvSize(static_cast<int>(mipLevelSize.width), static_cast<int>(mipLevelSize.height));
cv::Size cvSize(static_cast<int>(mipLevelSize.width),
static_cast<int>(mipLevelSize.height));
cv::resize(image, mipLevelImage, cvSize);

std::size_t mipLevelBytes = 4ull * mipLevelSize.width * mipLevelSize.height;
Expand All @@ -842,7 +863,12 @@ void initWebGPU(WebGPUDemoApp& app)
pixelDataLayout.rowsPerImage = mipLevelSize.height;

// Upload the data to the GPU.
wgpuQueueWriteTexture(app.queue, &destination, mipLevelImage.data, mipLevelBytes, &pixelDataLayout, &mipLevelSize);
wgpuQueueWriteTexture(app.queue,
&destination,
mipLevelImage.data,
mipLevelBytes,
&pixelDataLayout,
&mipLevelSize);

// Scale the image down for the next mip level.
mipLevelSize.width /= 2;
Expand Down Expand Up @@ -981,7 +1007,8 @@ void initWebGPU(WebGPUDemoApp& app)
bindGroupLayoutDesc.entryCount = bindGroupLayoutEntries.size();
bindGroupLayoutDesc.entries = bindGroupLayoutEntries.data();

app.bindGroupLayout = wgpuDeviceCreateBindGroupLayout(app.device, &bindGroupLayoutDesc);
app.bindGroupLayout = wgpuDeviceCreateBindGroupLayout(app.device,
&bindGroupLayoutDesc);
WEBGPU_DEMO_CHECK(app.bindGroupLayout, "[WebGPU] Failed to create bind group layout");
WEBGPU_DEMO_LOG("[WebGPU] Bind group layout created");

Expand Down Expand Up @@ -1046,7 +1073,9 @@ void initWebGPU(WebGPUDemoApp& app)
uvAttribute.offset = 6 * sizeof(float);
uvAttribute.shaderLocation = 2;

std::vector<WGPUVertexAttribute> vertexAttributes = {positionAttribute, normalAttribute, uvAttribute};
std::vector<WGPUVertexAttribute> vertexAttributes = {positionAttribute,
normalAttribute,
uvAttribute};

// Description of the vertex buffer layout for the vertex shader stage
WGPUVertexBufferLayout vertexBufferLayout = {};
Expand Down Expand Up @@ -1185,6 +1214,7 @@ int main(int argc, const char* argv[])
lastCursorY = cursorY;

onPaint(app);

glfwPollEvents();
}

Expand Down

0 comments on commit 5ccb7b1

Please sign in to comment.