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
|
|
|
|
2018-07-18 12:00:56 +00:00
|
|
|
#include "utils/DawnHelpers.h"
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnDevice device;
|
|
|
|
dawnQueue queue;
|
|
|
|
dawnSwapChain swapchain;
|
|
|
|
dawnRenderPipeline pipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnTextureFormat 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();
|
2018-07-18 13:12:52 +00:00
|
|
|
queue = dawnDeviceCreateQueue(device);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnSwapChainBuilder builder = dawnDeviceCreateSwapChainBuilder(device);
|
2017-07-28 01:30:57 +00:00
|
|
|
uint64_t swapchainImpl = GetSwapChainImplementation();
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnSwapChainBuilderSetImplementation(builder, swapchainImpl);
|
|
|
|
swapchain = dawnSwapChainBuilderGetResult(builder);
|
|
|
|
dawnSwapChainBuilderRelease(builder);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2018-07-18 13:12:52 +00:00
|
|
|
swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat());
|
|
|
|
dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
|
2018-01-19 17:51:18 +00:00
|
|
|
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";
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::ShaderStage::Vertex, vs).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";
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
{
|
2018-12-10 19:47:22 +00:00
|
|
|
dawnRenderPipelineDescriptor descriptor;
|
|
|
|
descriptor.nextInChain = nullptr;
|
|
|
|
|
|
|
|
dawnPipelineStageDescriptor vertexStage;
|
|
|
|
vertexStage.nextInChain = nullptr;
|
|
|
|
vertexStage.module = vsModule;
|
|
|
|
vertexStage.entryPoint = "main";
|
|
|
|
descriptor.vertexStage = &vertexStage;
|
|
|
|
|
|
|
|
dawnPipelineStageDescriptor fragmentStage;
|
|
|
|
fragmentStage.nextInChain = nullptr;
|
|
|
|
fragmentStage.module = fsModule;
|
|
|
|
fragmentStage.entryPoint = "main";
|
|
|
|
descriptor.fragmentStage = &fragmentStage;
|
|
|
|
|
|
|
|
dawnAttachmentsStateDescriptor attachmentsState;
|
|
|
|
attachmentsState.nextInChain = nullptr;
|
|
|
|
attachmentsState.numColorAttachments = 1;
|
|
|
|
dawnAttachmentDescriptor colorAttachment = {nullptr, swapChainFormat};
|
2019-01-21 08:29:01 +00:00
|
|
|
dawnAttachmentDescriptor* colorAttachmentPtr[] = {&colorAttachment};
|
|
|
|
attachmentsState.colorAttachments = colorAttachmentPtr;
|
2018-12-10 19:47:22 +00:00
|
|
|
attachmentsState.hasDepthStencilAttachment = false;
|
|
|
|
// Even with hasDepthStencilAttachment = false, depthStencilAttachment must point to valid
|
|
|
|
// data because we don't have optional substructures yet.
|
|
|
|
attachmentsState.depthStencilAttachment = &colorAttachment;
|
|
|
|
descriptor.attachmentsState = &attachmentsState;
|
|
|
|
|
|
|
|
descriptor.sampleCount = 1;
|
|
|
|
|
|
|
|
descriptor.numBlendStates = 1;
|
2018-12-27 06:32:31 +00:00
|
|
|
|
|
|
|
dawnBlendDescriptor blendDescriptor;
|
|
|
|
blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD;
|
|
|
|
blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE;
|
|
|
|
blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE;
|
|
|
|
dawnBlendStateDescriptor blendStateDescriptor;
|
|
|
|
blendStateDescriptor.nextInChain = nullptr;
|
|
|
|
blendStateDescriptor.alphaBlend = blendDescriptor;
|
|
|
|
blendStateDescriptor.colorBlend = blendDescriptor;
|
|
|
|
blendStateDescriptor.colorWriteMask = DAWN_COLOR_WRITE_MASK_ALL;
|
|
|
|
descriptor.blendStates = &blendStateDescriptor;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
dawnPipelineLayoutDescriptor pl;
|
|
|
|
pl.nextInChain = nullptr;
|
|
|
|
pl.numBindGroupLayouts = 0;
|
|
|
|
pl.bindGroupLayouts = nullptr;
|
|
|
|
descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl);
|
|
|
|
|
|
|
|
dawnInputStateBuilder inputStateBuilder = dawnDeviceCreateInputStateBuilder(device);
|
|
|
|
descriptor.inputState = dawnInputStateBuilderGetResult(inputStateBuilder);
|
|
|
|
dawnInputStateBuilderRelease(inputStateBuilder);
|
|
|
|
|
|
|
|
descriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32;
|
|
|
|
descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
|
|
|
|
2019-01-04 04:28:37 +00:00
|
|
|
dawnStencilStateFaceDescriptor stencilFace;
|
|
|
|
stencilFace.compare = DAWN_COMPARE_FUNCTION_ALWAYS;
|
2019-01-30 21:11:43 +00:00
|
|
|
stencilFace.failOp = DAWN_STENCIL_OPERATION_KEEP;
|
2019-01-04 04:28:37 +00:00
|
|
|
stencilFace.depthFailOp = DAWN_STENCIL_OPERATION_KEEP;
|
|
|
|
stencilFace.passOp = DAWN_STENCIL_OPERATION_KEEP;
|
|
|
|
|
|
|
|
dawnDepthStencilStateDescriptor depthStencilState;
|
|
|
|
depthStencilState.nextInChain = nullptr;
|
|
|
|
depthStencilState.depthWriteEnabled = false;
|
|
|
|
depthStencilState.depthCompare = DAWN_COMPARE_FUNCTION_ALWAYS;
|
2019-01-30 21:11:43 +00:00
|
|
|
depthStencilState.stencilBack = stencilFace;
|
|
|
|
depthStencilState.stencilFront = stencilFace;
|
2019-01-04 04:28:37 +00:00
|
|
|
depthStencilState.stencilReadMask = 0xff;
|
|
|
|
depthStencilState.stencilWriteMask = 0xff;
|
|
|
|
descriptor.depthStencilState = &depthStencilState;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor);
|
|
|
|
|
|
|
|
dawnInputStateRelease(descriptor.inputState);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnShaderModuleRelease(vsModule);
|
|
|
|
dawnShaderModuleRelease(fsModule);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
|
|
|
|
dawnTextureView backbufferView;
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2018-09-18 00:31:57 +00:00
|
|
|
backbufferView = dawnTextureCreateDefaultTextureView(backbuffer);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnRenderPassDescriptor renderpassInfo;
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnRenderPassDescriptorBuilder builder = dawnDeviceCreateRenderPassDescriptorBuilder(device);
|
2018-12-19 08:21:13 +00:00
|
|
|
dawnRenderPassColorAttachmentDescriptor 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;
|
|
|
|
dawnRenderPassDescriptorBuilderSetColorAttachments(builder, 1, &colorAttachment);
|
2018-07-18 13:12:52 +00:00
|
|
|
renderpassInfo = dawnRenderPassDescriptorBuilderGetResult(builder);
|
|
|
|
dawnRenderPassDescriptorBuilderRelease(builder);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnCommandBuffer commands;
|
2017-04-20 18:38:20 +00:00
|
|
|
{
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnCommandBufferBuilder builder = dawnDeviceCreateCommandBufferBuilder(device);
|
2018-09-21 00:24:37 +00:00
|
|
|
|
|
|
|
dawnRenderPassEncoder pass = dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
|
2018-12-21 10:40:26 +00:00
|
|
|
dawnRenderPassEncoderSetPipeline(pass, pipeline);
|
2018-12-10 05:20:19 +00:00
|
|
|
dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
|
2018-09-21 00:24:37 +00:00
|
|
|
dawnRenderPassEncoderEndPass(pass);
|
|
|
|
dawnRenderPassEncoderRelease(pass);
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
commands = dawnCommandBufferBuilderGetResult(builder);
|
|
|
|
dawnCommandBufferBuilderRelease(builder);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnQueueSubmit(queue, 1, &commands);
|
|
|
|
dawnCommandBufferRelease(commands);
|
|
|
|
dawnSwapChainPresent(swapchain, backbuffer);
|
|
|
|
dawnRenderPassDescriptorRelease(renderpassInfo);
|
|
|
|
dawnTextureViewRelease(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
|
|
|
|
}
|