2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
#include "SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2019-10-25 11:36:47 +00:00
|
|
|
#include "utils/WGPUHelpers.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUDevice device;
|
|
|
|
WGPUQueue queue;
|
|
|
|
WGPUSwapChain swapchain;
|
|
|
|
WGPURenderPipeline pipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUTextureFormat swapChainFormat;
|
2018-01-19 17:51:18 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
void init() {
|
2018-07-18 12:06:10 +00:00
|
|
|
device = CreateCppDawnDevice().Release();
|
2019-10-25 11:36:47 +00:00
|
|
|
queue = wgpuDeviceCreateQueue(device);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUSwapChainDescriptor descriptor;
|
2019-02-15 11:15:58 +00:00
|
|
|
descriptor.nextInChain = nullptr;
|
2019-10-03 14:56:49 +00:00
|
|
|
descriptor.label = nullptr;
|
2019-02-15 11:15:58 +00:00
|
|
|
descriptor.implementation = GetSwapChainImplementation();
|
2019-10-25 11:36:47 +00:00
|
|
|
swapchain = wgpuDeviceCreateSwapChain(device, &descriptor);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2019-10-25 11:36:47 +00:00
|
|
|
swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
|
|
|
|
wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_OutputAttachment, 640, 480);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
const char* vs =
|
|
|
|
"#version 450\n"
|
|
|
|
"const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUShaderModule vsModule =
|
|
|
|
utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs)
|
2019-08-27 08:42:29 +00:00
|
|
|
.Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
const char* fs =
|
|
|
|
"#version 450\n"
|
2017-08-29 17:37:45 +00:00
|
|
|
"layout(location = 0) out vec4 fragColor;"
|
2017-04-20 18:38:20 +00:00
|
|
|
"void main() {\n"
|
|
|
|
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUShaderModule fsModule =
|
2019-08-27 08:42:29 +00:00
|
|
|
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPURenderPipelineDescriptor descriptor;
|
2019-10-03 14:56:49 +00:00
|
|
|
descriptor.label = nullptr;
|
2018-12-10 19:47:22 +00:00
|
|
|
descriptor.nextInChain = nullptr;
|
|
|
|
|
2019-09-05 09:35:07 +00:00
|
|
|
descriptor.vertexStage.nextInChain = nullptr;
|
|
|
|
descriptor.vertexStage.module = vsModule;
|
|
|
|
descriptor.vertexStage.entryPoint = "main";
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUProgrammableStageDescriptor fragmentStage;
|
2018-12-10 19:47:22 +00:00
|
|
|
fragmentStage.nextInChain = nullptr;
|
|
|
|
fragmentStage.module = fsModule;
|
|
|
|
fragmentStage.entryPoint = "main";
|
|
|
|
descriptor.fragmentStage = &fragmentStage;
|
|
|
|
|
|
|
|
descriptor.sampleCount = 1;
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUBlendDescriptor blendDescriptor;
|
|
|
|
blendDescriptor.operation = WGPUBlendOperation_Add;
|
|
|
|
blendDescriptor.srcFactor = WGPUBlendFactor_One;
|
|
|
|
blendDescriptor.dstFactor = WGPUBlendFactor_One;
|
|
|
|
WGPUColorStateDescriptor colorStateDescriptor;
|
2019-02-15 02:20:57 +00:00
|
|
|
colorStateDescriptor.nextInChain = nullptr;
|
|
|
|
colorStateDescriptor.format = swapChainFormat;
|
|
|
|
colorStateDescriptor.alphaBlend = blendDescriptor;
|
|
|
|
colorStateDescriptor.colorBlend = blendDescriptor;
|
2019-10-25 11:36:47 +00:00
|
|
|
colorStateDescriptor.writeMask = WGPUColorWriteMask_All;
|
2019-02-20 13:00:36 +00:00
|
|
|
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.colorStateCount = 1;
|
2019-09-20 23:22:27 +00:00
|
|
|
descriptor.colorStates = &colorStateDescriptor;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUPipelineLayoutDescriptor pl;
|
2018-12-10 19:47:22 +00:00
|
|
|
pl.nextInChain = nullptr;
|
2019-10-03 14:56:49 +00:00
|
|
|
pl.label = nullptr;
|
2019-02-21 00:45:19 +00:00
|
|
|
pl.bindGroupLayoutCount = 0;
|
2018-12-10 19:47:22 +00:00
|
|
|
pl.bindGroupLayouts = nullptr;
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUVertexInputDescriptor vertexInput;
|
2019-05-22 22:46:32 +00:00
|
|
|
vertexInput.nextInChain = nullptr;
|
2019-10-25 11:36:47 +00:00
|
|
|
vertexInput.indexFormat = WGPUIndexFormat_Uint32;
|
2019-06-06 17:54:30 +00:00
|
|
|
vertexInput.bufferCount = 0;
|
2019-05-22 22:46:32 +00:00
|
|
|
vertexInput.buffers = nullptr;
|
|
|
|
descriptor.vertexInput = &vertexInput;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPURasterizationStateDescriptor rasterizationState;
|
2019-04-11 18:46:54 +00:00
|
|
|
rasterizationState.nextInChain = nullptr;
|
2019-10-25 11:36:47 +00:00
|
|
|
rasterizationState.frontFace = WGPUFrontFace_CCW;
|
|
|
|
rasterizationState.cullMode = WGPUCullMode_None;
|
2019-04-11 18:46:54 +00:00
|
|
|
rasterizationState.depthBias = 0;
|
|
|
|
rasterizationState.depthBiasSlopeScale = 0.0;
|
|
|
|
rasterizationState.depthBiasClamp = 0.0;
|
|
|
|
descriptor.rasterizationState = &rasterizationState;
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList;
|
2019-07-15 20:47:56 +00:00
|
|
|
descriptor.sampleMask = 0xFFFFFFFF;
|
|
|
|
descriptor.alphaToCoverageEnabled = false;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2019-02-15 02:20:57 +00:00
|
|
|
descriptor.depthStencilState = nullptr;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpuShaderModuleRelease(vsModule);
|
|
|
|
wgpuShaderModuleRelease(fsModule);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUTexture backbuffer = wgpuSwapChainGetNextTexture(swapchain);
|
|
|
|
WGPUTextureView backbufferView = wgpuTextureCreateView(backbuffer, nullptr);
|
|
|
|
WGPURenderPassDescriptor renderpassInfo;
|
2019-10-08 07:34:43 +00:00
|
|
|
renderpassInfo.nextInChain = nullptr;
|
2019-10-03 14:56:49 +00:00
|
|
|
renderpassInfo.label = nullptr;
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPURenderPassColorAttachmentDescriptor colorAttachment;
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2018-12-19 08:21:13 +00:00
|
|
|
colorAttachment.attachment = backbufferView;
|
|
|
|
colorAttachment.resolveTarget = nullptr;
|
|
|
|
colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
|
2019-10-25 11:36:47 +00:00
|
|
|
colorAttachment.loadOp = WGPULoadOp_Clear;
|
|
|
|
colorAttachment.storeOp = WGPUStoreOp_Store;
|
2019-02-27 09:21:56 +00:00
|
|
|
renderpassInfo.colorAttachmentCount = 1;
|
2019-09-20 22:59:47 +00:00
|
|
|
renderpassInfo.colorAttachments = &colorAttachment;
|
2019-02-27 09:21:56 +00:00
|
|
|
renderpassInfo.depthStencilAttachment = nullptr;
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUCommandBuffer commands;
|
2017-04-20 18:38:20 +00:00
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
|
2018-09-21 00:24:37 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
|
|
|
|
wgpuRenderPassEncoderSetPipeline(pass, pipeline);
|
|
|
|
wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
|
|
|
|
wgpuRenderPassEncoderEndPass(pass);
|
|
|
|
wgpuRenderPassEncoderRelease(pass);
|
2018-09-21 00:24:37 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
commands = wgpuCommandEncoderFinish(encoder, nullptr);
|
|
|
|
wgpuCommandEncoderRelease(encoder);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpuQueueSubmit(queue, 1, &commands);
|
|
|
|
wgpuCommandBufferRelease(commands);
|
|
|
|
wgpuSwapChainPresent(swapchain, backbuffer);
|
|
|
|
wgpuTextureViewRelease(backbufferView);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
DoFlush();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|