Make examples and utils use webgpu.h

BUG=dawn:22

Change-Id: I602d6a3422b493d199f3fded61ff1666bc2d9d7d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/12702
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Corentin Wallez
2019-10-25 11:36:47 +00:00
committed by Commit Bot service account
parent 9f90c8d3ca
commit 04863c42be
80 changed files with 451 additions and 454 deletions

View File

@@ -15,19 +15,19 @@
#include "SampleUtils.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include "utils/WGPUHelpers.h"
#include <cstdlib>
#include <cstdio>
#include <vector>
dawn::Device device;
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::RenderPipeline pipeline;
dawn::BindGroup bindGroup;
dawn::Buffer ubo;
wgpu::Device device;
wgpu::Queue queue;
wgpu::SwapChain swapchain;
wgpu::RenderPipeline pipeline;
wgpu::BindGroup bindGroup;
wgpu::Buffer ubo;
float RandomFloat(float min, float max) {
float zeroOne = rand() / float(RAND_MAX);
@@ -52,10 +52,10 @@ void init() {
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
640, 480);
dawn::ShaderModule vsModule =
wgpu::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
@@ -103,7 +103,7 @@ void init() {
gl_Position = vec4(xpos, ypos, 0.0, 1.0);
})");
dawn::ShaderModule fsModule =
wgpu::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
@@ -112,8 +112,8 @@ void init() {
fragColor = v_color;
})");
dawn::BindGroupLayout bgl = utils::MakeBindGroupLayout(
device, {{0, dawn::ShaderStage::Vertex, dawn::BindingType::UniformBuffer, true}});
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(
device, {{0, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer, true}});
utils::ComboRenderPipelineDescriptor descriptor(device);
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
@@ -133,9 +133,9 @@ void init() {
data.scalarOffset = RandomFloat(0.0f, 10.0f);
}
dawn::BufferDescriptor bufferDesc;
wgpu::BufferDescriptor bufferDesc;
bufferDesc.size = kNumTriangles * sizeof(ShaderData);
bufferDesc.usage = dawn::BufferUsage::CopyDst | dawn::BufferUsage::Uniform;
bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
ubo = device.CreateBuffer(&bufferDesc);
bindGroup =
@@ -143,7 +143,7 @@ void init() {
}
void frame() {
dawn::Texture backbuffer = swapchain.GetNextTexture();
wgpu::Texture backbuffer = swapchain.GetNextTexture();
static int f = 0;
f++;
@@ -153,9 +153,9 @@ void frame() {
ubo.SetSubData(0, kNumTriangles * sizeof(ShaderData), shaderData.data());
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()});
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(pipeline);
for (size_t i = 0; i < kNumTriangles; i++) {
@@ -167,7 +167,7 @@ void frame() {
pass.EndPass();
}
dawn::CommandBuffer commands = encoder.Finish();
wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
swapchain.Present(backbuffer);
DoFlush();

View File

@@ -14,30 +14,29 @@
#include "SampleUtils.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include "utils/WGPUHelpers.h"
DawnDevice device;
DawnQueue queue;
DawnSwapChain swapchain;
DawnRenderPipeline pipeline;
WGPUDevice device;
WGPUQueue queue;
WGPUSwapChain swapchain;
WGPURenderPipeline pipeline;
DawnTextureFormat swapChainFormat;
WGPUTextureFormat swapChainFormat;
void init() {
device = CreateCppDawnDevice().Release();
queue = dawnDeviceCreateQueue(device);
queue = wgpuDeviceCreateQueue(device);
{
DawnSwapChainDescriptor descriptor;
WGPUSwapChainDescriptor descriptor;
descriptor.nextInChain = nullptr;
descriptor.label = nullptr;
descriptor.implementation = GetSwapChainImplementation();
swapchain = dawnDeviceCreateSwapChain(device, &descriptor);
swapchain = wgpuDeviceCreateSwapChain(device, &descriptor);
}
swapChainFormat = static_cast<DawnTextureFormat>(GetPreferredSwapChainTextureFormat());
dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_OUTPUT_ATTACHMENT, 640,
480);
swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_OutputAttachment, 640, 480);
const char* vs =
"#version 450\n"
@@ -45,8 +44,8 @@ void init() {
"void main() {\n"
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
"}\n";
DawnShaderModule vsModule =
utils::CreateShaderModule(dawn::Device(device), utils::SingleShaderStage::Vertex, vs)
WGPUShaderModule vsModule =
utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs)
.Release();
const char* fs =
@@ -55,11 +54,11 @@ void init() {
"void main() {\n"
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
DawnShaderModule fsModule =
WGPUShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release();
{
DawnRenderPipelineDescriptor descriptor;
WGPURenderPipelineDescriptor descriptor;
descriptor.label = nullptr;
descriptor.nextInChain = nullptr;
@@ -67,7 +66,7 @@ void init() {
descriptor.vertexStage.module = vsModule;
descriptor.vertexStage.entryPoint = "main";
DawnProgrammableStageDescriptor fragmentStage;
WGPUProgrammableStageDescriptor fragmentStage;
fragmentStage.nextInChain = nullptr;
fragmentStage.module = fsModule;
fragmentStage.entryPoint = "main";
@@ -75,91 +74,91 @@ void init() {
descriptor.sampleCount = 1;
DawnBlendDescriptor blendDescriptor;
blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD;
blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE;
blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE;
DawnColorStateDescriptor colorStateDescriptor;
WGPUBlendDescriptor blendDescriptor;
blendDescriptor.operation = WGPUBlendOperation_Add;
blendDescriptor.srcFactor = WGPUBlendFactor_One;
blendDescriptor.dstFactor = WGPUBlendFactor_One;
WGPUColorStateDescriptor colorStateDescriptor;
colorStateDescriptor.nextInChain = nullptr;
colorStateDescriptor.format = swapChainFormat;
colorStateDescriptor.alphaBlend = blendDescriptor;
colorStateDescriptor.colorBlend = blendDescriptor;
colorStateDescriptor.writeMask = DAWN_COLOR_WRITE_MASK_ALL;
colorStateDescriptor.writeMask = WGPUColorWriteMask_All;
descriptor.colorStateCount = 1;
descriptor.colorStates = &colorStateDescriptor;
DawnPipelineLayoutDescriptor pl;
WGPUPipelineLayoutDescriptor pl;
pl.nextInChain = nullptr;
pl.label = nullptr;
pl.bindGroupLayoutCount = 0;
pl.bindGroupLayouts = nullptr;
descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl);
descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
DawnVertexInputDescriptor vertexInput;
WGPUVertexInputDescriptor vertexInput;
vertexInput.nextInChain = nullptr;
vertexInput.indexFormat = DAWN_INDEX_FORMAT_UINT32;
vertexInput.indexFormat = WGPUIndexFormat_Uint32;
vertexInput.bufferCount = 0;
vertexInput.buffers = nullptr;
descriptor.vertexInput = &vertexInput;
DawnRasterizationStateDescriptor rasterizationState;
WGPURasterizationStateDescriptor rasterizationState;
rasterizationState.nextInChain = nullptr;
rasterizationState.frontFace = DAWN_FRONT_FACE_CCW;
rasterizationState.cullMode = DAWN_CULL_MODE_NONE;
rasterizationState.frontFace = WGPUFrontFace_CCW;
rasterizationState.cullMode = WGPUCullMode_None;
rasterizationState.depthBias = 0;
rasterizationState.depthBiasSlopeScale = 0.0;
rasterizationState.depthBiasClamp = 0.0;
descriptor.rasterizationState = &rasterizationState;
descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList;
descriptor.sampleMask = 0xFFFFFFFF;
descriptor.alphaToCoverageEnabled = false;
descriptor.depthStencilState = nullptr;
pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor);
pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
}
dawnShaderModuleRelease(vsModule);
dawnShaderModuleRelease(fsModule);
wgpuShaderModuleRelease(vsModule);
wgpuShaderModuleRelease(fsModule);
}
void frame() {
DawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
DawnTextureView backbufferView = dawnTextureCreateView(backbuffer, nullptr);
DawnRenderPassDescriptor renderpassInfo;
WGPUTexture backbuffer = wgpuSwapChainGetNextTexture(swapchain);
WGPUTextureView backbufferView = wgpuTextureCreateView(backbuffer, nullptr);
WGPURenderPassDescriptor renderpassInfo;
renderpassInfo.nextInChain = nullptr;
renderpassInfo.label = nullptr;
DawnRenderPassColorAttachmentDescriptor colorAttachment;
WGPURenderPassColorAttachmentDescriptor colorAttachment;
{
colorAttachment.attachment = backbufferView;
colorAttachment.resolveTarget = nullptr;
colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR;
colorAttachment.storeOp = DAWN_STORE_OP_STORE;
colorAttachment.loadOp = WGPULoadOp_Clear;
colorAttachment.storeOp = WGPUStoreOp_Store;
renderpassInfo.colorAttachmentCount = 1;
renderpassInfo.colorAttachments = &colorAttachment;
renderpassInfo.depthStencilAttachment = nullptr;
}
DawnCommandBuffer commands;
WGPUCommandBuffer commands;
{
DawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device, nullptr);
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
DawnRenderPassEncoder pass = dawnCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
dawnRenderPassEncoderSetPipeline(pass, pipeline);
dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
dawnRenderPassEncoderEndPass(pass);
dawnRenderPassEncoderRelease(pass);
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
wgpuRenderPassEncoderSetPipeline(pass, pipeline);
wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
wgpuRenderPassEncoderEndPass(pass);
wgpuRenderPassEncoderRelease(pass);
commands = dawnCommandEncoderFinish(encoder, nullptr);
dawnCommandEncoderRelease(encoder);
commands = wgpuCommandEncoderFinish(encoder, nullptr);
wgpuCommandEncoderRelease(encoder);
}
dawnQueueSubmit(queue, 1, &commands);
dawnCommandBufferRelease(commands);
dawnSwapChainPresent(swapchain, backbuffer);
dawnTextureViewRelease(backbufferView);
wgpuQueueSubmit(queue, 1, &commands);
wgpuCommandBufferRelease(commands);
wgpuSwapChainPresent(swapchain, backbuffer);
wgpuTextureViewRelease(backbufferView);
DoFlush();
}

View File

@@ -15,8 +15,8 @@
#include "SampleUtils.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include "utils/WGPUHelpers.h"
#include <array>
#include <cstring>
@@ -24,19 +24,19 @@
#include <glm/glm.hpp>
dawn::Device device;
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::TextureView depthStencilView;
wgpu::Device device;
wgpu::Queue queue;
wgpu::SwapChain swapchain;
wgpu::TextureView depthStencilView;
dawn::Buffer modelBuffer;
std::array<dawn::Buffer, 2> particleBuffers;
wgpu::Buffer modelBuffer;
std::array<wgpu::Buffer, 2> particleBuffers;
dawn::RenderPipeline renderPipeline;
wgpu::RenderPipeline renderPipeline;
dawn::Buffer updateParams;
dawn::ComputePipeline updatePipeline;
std::array<dawn::BindGroup, 2> updateBGs;
wgpu::Buffer updateParams;
wgpu::ComputePipeline updatePipeline;
std::array<wgpu::BindGroup, 2> updateBGs;
size_t pingpong = 0;
@@ -65,11 +65,11 @@ void initBuffers() {
{0.00, 0.02},
};
modelBuffer =
utils::CreateBufferFromData(device, model, sizeof(model), dawn::BufferUsage::Vertex);
utils::CreateBufferFromData(device, model, sizeof(model), wgpu::BufferUsage::Vertex);
SimParams params = { 0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles };
updateParams =
utils::CreateBufferFromData(device, &params, sizeof(params), dawn::BufferUsage::Uniform);
utils::CreateBufferFromData(device, &params, sizeof(params), wgpu::BufferUsage::Uniform);
std::vector<Particle> initialParticles(kNumParticles);
{
@@ -83,10 +83,10 @@ void initBuffers() {
}
for (size_t i = 0; i < 2; i++) {
dawn::BufferDescriptor descriptor;
wgpu::BufferDescriptor descriptor;
descriptor.size = sizeof(Particle) * kNumParticles;
descriptor.usage =
dawn::BufferUsage::CopyDst | dawn::BufferUsage::Vertex | dawn::BufferUsage::Storage;
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Storage;
particleBuffers[i] = device.CreateBuffer(&descriptor);
particleBuffers[i].SetSubData(0,
@@ -96,7 +96,7 @@ void initBuffers() {
}
void initRender() {
dawn::ShaderModule vsModule =
wgpu::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec2 a_particlePos;
@@ -110,7 +110,7 @@ void initRender() {
}
)");
dawn::ShaderModule fsModule =
wgpu::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
@@ -127,27 +127,27 @@ void initRender() {
descriptor.cVertexInput.bufferCount = 2;
descriptor.cVertexInput.cBuffers[0].stride = sizeof(Particle);
descriptor.cVertexInput.cBuffers[0].stepMode = dawn::InputStepMode::Instance;
descriptor.cVertexInput.cBuffers[0].stepMode = wgpu::InputStepMode::Instance;
descriptor.cVertexInput.cBuffers[0].attributeCount = 2;
descriptor.cVertexInput.cAttributes[0].offset = offsetof(Particle, pos);
descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float2;
descriptor.cVertexInput.cAttributes[0].format = wgpu::VertexFormat::Float2;
descriptor.cVertexInput.cAttributes[1].shaderLocation = 1;
descriptor.cVertexInput.cAttributes[1].offset = offsetof(Particle, vel);
descriptor.cVertexInput.cAttributes[1].format = dawn::VertexFormat::Float2;
descriptor.cVertexInput.cAttributes[1].format = wgpu::VertexFormat::Float2;
descriptor.cVertexInput.cBuffers[1].stride = sizeof(glm::vec2);
descriptor.cVertexInput.cBuffers[1].attributeCount = 1;
descriptor.cVertexInput.cBuffers[1].attributes = &descriptor.cVertexInput.cAttributes[2];
descriptor.cVertexInput.cAttributes[2].shaderLocation = 2;
descriptor.cVertexInput.cAttributes[2].format = dawn::VertexFormat::Float2;
descriptor.cVertexInput.cAttributes[2].format = wgpu::VertexFormat::Float2;
descriptor.depthStencilState = &descriptor.cDepthStencilState;
descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
renderPipeline = device.CreateRenderPipeline(&descriptor);
}
void initSim() {
dawn::ShaderModule module =
wgpu::ShaderModule module =
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"(
#version 450
@@ -239,14 +239,14 @@ void initSim() {
auto bgl = utils::MakeBindGroupLayout(
device, {
{0, dawn::ShaderStage::Compute, dawn::BindingType::UniformBuffer},
{1, dawn::ShaderStage::Compute, dawn::BindingType::StorageBuffer},
{2, dawn::ShaderStage::Compute, dawn::BindingType::StorageBuffer},
{0, wgpu::ShaderStage::Compute, wgpu::BindingType::UniformBuffer},
{1, wgpu::ShaderStage::Compute, wgpu::BindingType::StorageBuffer},
{2, wgpu::ShaderStage::Compute, wgpu::BindingType::StorageBuffer},
});
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
dawn::ComputePipelineDescriptor csDesc;
wgpu::ComputePipelineDescriptor csDesc;
csDesc.layout = pl;
csDesc.computeStage.module = module;
csDesc.computeStage.entryPoint = "main";
@@ -261,12 +261,12 @@ void initSim() {
}
}
dawn::CommandBuffer createCommandBuffer(const dawn::Texture backbuffer, size_t i) {
wgpu::CommandBuffer createCommandBuffer(const wgpu::Texture backbuffer, size_t i) {
auto& bufferDst = particleBuffers[(i + 1) % 2];
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
pass.SetPipeline(updatePipeline);
pass.SetBindGroup(0, updateBGs[i]);
pass.Dispatch(kNumParticles, 1, 1);
@@ -275,7 +275,7 @@ dawn::CommandBuffer createCommandBuffer(const dawn::Texture backbuffer, size_t i
{
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(renderPipeline);
pass.SetVertexBuffer(0, bufferDst);
pass.SetVertexBuffer(1, modelBuffer);
@@ -291,7 +291,7 @@ void init() {
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
640, 480);
initBuffers();
@@ -300,9 +300,9 @@ void init() {
}
void frame() {
dawn::Texture backbuffer = swapchain.GetNextTexture();
wgpu::Texture backbuffer = swapchain.GetNextTexture();
dawn::CommandBuffer commandBuffer = createCommandBuffer(backbuffer, pingpong);
wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbuffer, pingpong);
queue.Submit(1, &commandBuffer);
swapchain.Present(backbuffer);
DoFlush();

View File

@@ -15,31 +15,31 @@
#include "SampleUtils.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include "utils/WGPUHelpers.h"
#include <vector>
dawn::Device device;
wgpu::Device device;
dawn::Buffer indexBuffer;
dawn::Buffer vertexBuffer;
wgpu::Buffer indexBuffer;
wgpu::Buffer vertexBuffer;
dawn::Texture texture;
dawn::Sampler sampler;
wgpu::Texture texture;
wgpu::Sampler sampler;
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::TextureView depthStencilView;
dawn::RenderPipeline pipeline;
dawn::BindGroup bindGroup;
wgpu::Queue queue;
wgpu::SwapChain swapchain;
wgpu::TextureView depthStencilView;
wgpu::RenderPipeline pipeline;
wgpu::BindGroup bindGroup;
void initBuffers() {
static const uint32_t indexData[3] = {
0, 1, 2,
};
indexBuffer =
utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsage::Index);
utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index);
static const float vertexData[12] = {
0.0f, 0.5f, 0.0f, 1.0f,
@@ -47,23 +47,23 @@ void initBuffers() {
0.5f, -0.5f, 0.0f, 1.0f,
};
vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData),
dawn::BufferUsage::Vertex);
wgpu::BufferUsage::Vertex);
}
void initTextures() {
dawn::TextureDescriptor descriptor;
descriptor.dimension = dawn::TextureDimension::e2D;
wgpu::TextureDescriptor descriptor;
descriptor.dimension = wgpu::TextureDimension::e2D;
descriptor.size.width = 1024;
descriptor.size.height = 1024;
descriptor.size.depth = 1;
descriptor.arrayLayerCount = 1;
descriptor.sampleCount = 1;
descriptor.format = dawn::TextureFormat::RGBA8Unorm;
descriptor.format = wgpu::TextureFormat::RGBA8Unorm;
descriptor.mipLevelCount = 1;
descriptor.usage = dawn::TextureUsage::CopyDst | dawn::TextureUsage::Sampled;
descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::Sampled;
texture = device.CreateTexture(&descriptor);
dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
wgpu::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
sampler = device.CreateSampler(&samplerDesc);
// Initialize the texture with arbitrary data until we can load images
@@ -72,16 +72,16 @@ void initTextures() {
data[i] = static_cast<uint8_t>(i % 253);
}
dawn::Buffer stagingBuffer = utils::CreateBufferFromData(
device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsage::CopySrc);
dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
dawn::Extent3D copySize = {1024, 1024, 1};
wgpu::Buffer stagingBuffer = utils::CreateBufferFromData(
device, data.data(), static_cast<uint32_t>(data.size()), wgpu::BufferUsage::CopySrc);
wgpu::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
wgpu::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
wgpu::Extent3D copySize = {1024, 1024, 1};
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize);
dawn::CommandBuffer copy = encoder.Finish();
wgpu::CommandBuffer copy = encoder.Finish();
queue.Submit(1, &copy);
}
@@ -90,13 +90,13 @@ void init() {
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
640, 480);
initBuffers();
initTextures();
dawn::ShaderModule vsModule =
wgpu::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
@@ -104,7 +104,7 @@ void init() {
gl_Position = pos;
})");
dawn::ShaderModule fsModule =
wgpu::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(set = 0, binding = 0) uniform sampler mySampler;
@@ -117,11 +117,11 @@ void init() {
auto bgl = utils::MakeBindGroupLayout(
device, {
{0, dawn::ShaderStage::Fragment, dawn::BindingType::Sampler},
{1, dawn::ShaderStage::Fragment, dawn::BindingType::SampledTexture},
{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::Sampler},
{1, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture},
});
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
depthStencilView = CreateDefaultDepthStencilView(device);
@@ -132,14 +132,14 @@ void init() {
descriptor.cVertexInput.bufferCount = 1;
descriptor.cVertexInput.cBuffers[0].stride = 4 * sizeof(float);
descriptor.cVertexInput.cBuffers[0].attributeCount = 1;
descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float4;
descriptor.cVertexInput.cAttributes[0].format = wgpu::VertexFormat::Float4;
descriptor.depthStencilState = &descriptor.cDepthStencilState;
descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
pipeline = device.CreateRenderPipeline(&descriptor);
dawn::TextureView view = texture.CreateView();
wgpu::TextureView view = texture.CreateView();
bindGroup = utils::MakeBindGroup(device, bgl, {
{0, sampler},
@@ -153,12 +153,12 @@ void frame() {
s.b += 0.02f;
if (s.b >= 1.0f) {s.b = 0.0f;}
dawn::Texture backbuffer = swapchain.GetNextTexture();
wgpu::Texture backbuffer = swapchain.GetNextTexture();
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(pipeline);
pass.SetBindGroup(0, bindGroup);
pass.SetVertexBuffer(0, vertexBuffer);
@@ -167,7 +167,7 @@ void frame() {
pass.EndPass();
}
dawn::CommandBuffer commands = encoder.Finish();
wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
swapchain.Present(backbuffer);
DoFlush();

View File

@@ -15,32 +15,32 @@
#include "SampleUtils.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include "utils/WGPUHelpers.h"
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
dawn::Device device;
wgpu::Device device;
dawn::Buffer indexBuffer;
dawn::Buffer vertexBuffer;
dawn::Buffer planeBuffer;
dawn::Buffer cameraBuffer;
dawn::Buffer transformBuffer[2];
wgpu::Buffer indexBuffer;
wgpu::Buffer vertexBuffer;
wgpu::Buffer planeBuffer;
wgpu::Buffer cameraBuffer;
wgpu::Buffer transformBuffer[2];
dawn::BindGroup cameraBindGroup;
dawn::BindGroup bindGroup[2];
dawn::BindGroup cubeTransformBindGroup[2];
wgpu::BindGroup cameraBindGroup;
wgpu::BindGroup bindGroup[2];
wgpu::BindGroup cubeTransformBindGroup[2];
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::TextureView depthStencilView;
dawn::RenderPipeline pipeline;
dawn::RenderPipeline planePipeline;
dawn::RenderPipeline reflectionPipeline;
wgpu::Queue queue;
wgpu::SwapChain swapchain;
wgpu::TextureView depthStencilView;
wgpu::RenderPipeline pipeline;
wgpu::RenderPipeline planePipeline;
wgpu::RenderPipeline reflectionPipeline;
void initBuffers() {
static const uint32_t indexData[6*6] = {
@@ -63,7 +63,7 @@ void initBuffers() {
20, 22, 23
};
indexBuffer =
utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsage::Index);
utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index);
static const float vertexData[6 * 4 * 6] = {
-1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
@@ -97,7 +97,7 @@ void initBuffers() {
-1.0, 1.0, -1.0, 1.0, 1.0, 1.0
};
vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData),
dawn::BufferUsage::Vertex);
wgpu::BufferUsage::Vertex);
static const float planeData[6 * 4] = {
-2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
@@ -106,7 +106,7 @@ void initBuffers() {
-2.0, -1.0, 2.0, 0.5, 0.5, 0.5,
};
planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData),
dawn::BufferUsage::Vertex);
wgpu::BufferUsage::Vertex);
}
struct CameraData {
@@ -119,12 +119,12 @@ void init() {
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
640, 480);
initBuffers();
dawn::ShaderModule vsModule =
wgpu::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
layout(set = 0, binding = 0) uniform cameraData {
@@ -142,7 +142,7 @@ void init() {
gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
})");
dawn::ShaderModule fsModule =
wgpu::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 2) in vec3 f_col;
@@ -151,7 +151,7 @@ void init() {
fragColor = vec4(f_col, 1.0);
})");
dawn::ShaderModule fsReflectionModule =
wgpu::ShaderModule fsReflectionModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 2) in vec3 f_col;
@@ -162,34 +162,34 @@ void init() {
utils::ComboVertexInputDescriptor vertexInput;
vertexInput.cBuffers[0].attributeCount = 2;
vertexInput.cAttributes[0].format = dawn::VertexFormat::Float3;
vertexInput.cAttributes[0].format = wgpu::VertexFormat::Float3;
vertexInput.cAttributes[1].shaderLocation = 1;
vertexInput.cAttributes[1].offset = 3 * sizeof(float);
vertexInput.cAttributes[1].format = dawn::VertexFormat::Float3;
vertexInput.cAttributes[1].format = wgpu::VertexFormat::Float3;
vertexInput.bufferCount = 1;
vertexInput.cBuffers[0].stride = 6 * sizeof(float);
auto bgl = utils::MakeBindGroupLayout(
device, {
{0, dawn::ShaderStage::Vertex, dawn::BindingType::UniformBuffer},
{1, dawn::ShaderStage::Vertex, dawn::BindingType::UniformBuffer},
{0, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer},
{1, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer},
});
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
dawn::BufferDescriptor cameraBufDesc;
wgpu::BufferDescriptor cameraBufDesc;
cameraBufDesc.size = sizeof(CameraData);
cameraBufDesc.usage = dawn::BufferUsage::CopyDst | dawn::BufferUsage::Uniform;
cameraBufDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
cameraBuffer = device.CreateBuffer(&cameraBufDesc);
glm::mat4 transform(1.0);
transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4),
dawn::BufferUsage::Uniform);
wgpu::BufferUsage::Uniform);
transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4),
dawn::BufferUsage::Uniform);
wgpu::BufferUsage::Uniform);
bindGroup[0] = utils::MakeBindGroup(device, bgl, {
{0, cameraBuffer, 0, sizeof(CameraData)},
@@ -209,10 +209,10 @@ void init() {
descriptor.cFragmentStage.module = fsModule;
descriptor.vertexInput = &vertexInput;
descriptor.depthStencilState = &descriptor.cDepthStencilState;
descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
descriptor.cDepthStencilState.depthWriteEnabled = true;
descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
descriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less;
pipeline = device.CreateRenderPipeline(&descriptor);
@@ -222,11 +222,11 @@ void init() {
pDescriptor.cFragmentStage.module = fsModule;
pDescriptor.vertexInput = &vertexInput;
pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState;
pDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
pDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
pDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
pDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
pDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
pDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
pDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace;
pDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace;
pDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less;
planePipeline = device.CreateRenderPipeline(&pDescriptor);
@@ -236,14 +236,14 @@ void init() {
rfDescriptor.cFragmentStage.module = fsReflectionModule;
rfDescriptor.vertexInput = &vertexInput;
rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState;
rfDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
rfDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
rfDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
rfDescriptor.cDepthStencilState.stencilFront.compare = dawn::CompareFunction::Equal;
rfDescriptor.cDepthStencilState.stencilBack.compare = dawn::CompareFunction::Equal;
rfDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
rfDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
rfDescriptor.cDepthStencilState.stencilFront.compare = wgpu::CompareFunction::Equal;
rfDescriptor.cDepthStencilState.stencilBack.compare = wgpu::CompareFunction::Equal;
rfDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace;
rfDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace;
rfDescriptor.cDepthStencilState.depthWriteEnabled = true;
rfDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
rfDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less;
reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor);
@@ -264,12 +264,12 @@ void frame() {
cameraBuffer.SetSubData(0, sizeof(CameraData), &cameraData);
dawn::Texture backbuffer = swapchain.GetNextTexture();
wgpu::Texture backbuffer = swapchain.GetNextTexture();
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(pipeline);
pass.SetBindGroup(0, bindGroup[0]);
pass.SetVertexBuffer(0, vertexBuffer);
@@ -290,7 +290,7 @@ void frame() {
pass.EndPass();
}
dawn::CommandBuffer commands = encoder.Finish();
wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
swapchain.Present(backbuffer);
DoFlush();

View File

@@ -19,10 +19,8 @@
#include "utils/BackendBinding.h"
#include "utils/TerribleCommandBuffer.h"
#include <dawn/dawn.h>
#include <dawn/dawn_proc.h>
#include <dawn/dawn_wsi.h>
#include <dawn/dawncpp.h>
#include <dawn_native/DawnNative.h>
#include <dawn_wire/WireClient.h>
#include <dawn_wire/WireServer.h>
@@ -32,18 +30,18 @@
#include <cstring>
#include <iostream>
void PrintDeviceError(DawnErrorType errorType, const char* message, void*) {
void PrintDeviceError(WGPUErrorType errorType, const char* message, void*) {
switch (errorType) {
case DAWN_ERROR_TYPE_VALIDATION:
case WGPUErrorType_Validation:
std::cout << "Validation ";
break;
case DAWN_ERROR_TYPE_OUT_OF_MEMORY:
case WGPUErrorType_OutOfMemory:
std::cout << "Out of memory ";
break;
case DAWN_ERROR_TYPE_UNKNOWN:
case WGPUErrorType_Unknown:
std::cout << "Unknown ";
break;
case DAWN_ERROR_TYPE_DEVICE_LOST:
case WGPUErrorType_DeviceLost:
std::cout << "Device lost ";
break;
default:
@@ -88,17 +86,17 @@ static dawn_wire::WireClient* wireClient = nullptr;
static utils::TerribleCommandBuffer* c2sBuf = nullptr;
static utils::TerribleCommandBuffer* s2cBuf = nullptr;
dawn::Device CreateCppDawnDevice() {
wgpu::Device CreateCppDawnDevice() {
glfwSetErrorCallback(PrintGLFWError);
if (!glfwInit()) {
return dawn::Device();
return wgpu::Device();
}
// Create the test window and discover adapters using it (esp. for OpenGL)
utils::SetupGLFWWindowHintsForBackend(backendType);
window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr);
if (!window) {
return dawn::Device();
return wgpu::Device();
}
instance = std::make_unique<dawn_native::Instance>();
@@ -116,16 +114,16 @@ dawn::Device CreateCppDawnDevice() {
backendAdapter = *adapterIt;
}
DawnDevice backendDevice = backendAdapter.CreateDevice();
WGPUDevice backendDevice = backendAdapter.CreateDevice();
DawnProcTable backendProcs = dawn_native::GetProcs();
binding = utils::CreateBinding(backendType, window, backendDevice);
if (binding == nullptr) {
return dawn::Device();
return wgpu::Device();
}
// Choose whether to use the backend procs and devices directly, or set up the wire.
DawnDevice cDevice = nullptr;
WGPUDevice cDevice = nullptr;
DawnProcTable procs;
switch (cmdBufType) {
@@ -151,7 +149,7 @@ dawn::Device CreateCppDawnDevice() {
clientDesc.serializer = c2sBuf;
wireClient = new dawn_wire::WireClient(clientDesc);
DawnDevice clientDevice = wireClient->GetDevice();
WGPUDevice clientDevice = wireClient->GetDevice();
DawnProcTable clientProcs = wireClient->GetProcs();
s2cBuf->SetHandler(wireClient);
@@ -163,35 +161,35 @@ dawn::Device CreateCppDawnDevice() {
dawnProcSetProcs(&procs);
procs.deviceSetUncapturedErrorCallback(cDevice, PrintDeviceError, nullptr);
return dawn::Device::Acquire(cDevice);
return wgpu::Device::Acquire(cDevice);
}
uint64_t GetSwapChainImplementation() {
return binding->GetSwapChainImplementation();
}
dawn::TextureFormat GetPreferredSwapChainTextureFormat() {
wgpu::TextureFormat GetPreferredSwapChainTextureFormat() {
DoFlush();
return static_cast<dawn::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
return static_cast<wgpu::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
}
dawn::SwapChain GetSwapChain(const dawn::Device &device) {
dawn::SwapChainDescriptor swapChainDesc;
wgpu::SwapChain GetSwapChain(const wgpu::Device& device) {
wgpu::SwapChainDescriptor swapChainDesc;
swapChainDesc.implementation = GetSwapChainImplementation();
return device.CreateSwapChain(&swapChainDesc);
}
dawn::TextureView CreateDefaultDepthStencilView(const dawn::Device& device) {
dawn::TextureDescriptor descriptor;
descriptor.dimension = dawn::TextureDimension::e2D;
wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device) {
wgpu::TextureDescriptor descriptor;
descriptor.dimension = wgpu::TextureDimension::e2D;
descriptor.size.width = 640;
descriptor.size.height = 480;
descriptor.size.depth = 1;
descriptor.arrayLayerCount = 1;
descriptor.sampleCount = 1;
descriptor.format = dawn::TextureFormat::Depth24PlusStencil8;
descriptor.format = wgpu::TextureFormat::Depth24PlusStencil8;
descriptor.mipLevelCount = 1;
descriptor.usage = dawn::TextureUsage::OutputAttachment;
descriptor.usage = wgpu::TextureUsage::OutputAttachment;
auto depthStencilTexture = device.CreateTexture(&descriptor);
return depthStencilTexture.CreateView();
}

View File

@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <dawn/dawncpp.h>
#include <dawn/dawn_wsi.h>
#include <dawn/webgpu_cpp.h>
bool InitSample(int argc, const char** argv);
void DoFlush();
@@ -22,8 +22,8 @@ bool ShouldQuit();
struct GLFWwindow;
struct GLFWwindow* GetGLFWWindow();
dawn::Device CreateCppDawnDevice();
wgpu::Device CreateCppDawnDevice();
uint64_t GetSwapChainImplementation();
dawn::TextureFormat GetPreferredSwapChainTextureFormat();
dawn::SwapChain GetSwapChain(const dawn::Device& device);
dawn::TextureView CreateDefaultDepthStencilView(const dawn::Device& device);
wgpu::TextureFormat GetPreferredSwapChainTextureFormat();
wgpu::SwapChain GetSwapChain(const wgpu::Device& device);
wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device);