blob: b521b9d38fb8a5ae2c8f7aaf0837549691f7de36 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Corentin Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Corentin Wallezf6840402018-07-18 14:00:56 +020017#include "utils/DawnHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallezb1669e32018-07-18 15:12:52 +020020dawnDevice device;
21dawnQueue queue;
22dawnSwapChain swapchain;
23dawnRenderPipeline pipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
Corentin Wallezb1669e32018-07-18 15:12:52 +020025dawnTextureFormat swapChainFormat;
Corentin Walleze98678f2018-01-19 12:51:18 -050026
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +020028 device = CreateCppDawnDevice().Release();
Corentin Wallezb1669e32018-07-18 15:12:52 +020029 queue = dawnDeviceCreateQueue(device);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070031 {
Corentin Wallezb1669e32018-07-18 15:12:52 +020032 dawnSwapChainBuilder builder = dawnDeviceCreateSwapChainBuilder(device);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070033 uint64_t swapchainImpl = GetSwapChainImplementation();
Corentin Wallezb1669e32018-07-18 15:12:52 +020034 dawnSwapChainBuilderSetImplementation(builder, swapchainImpl);
35 swapchain = dawnSwapChainBuilderGetResult(builder);
36 dawnSwapChainBuilderRelease(builder);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070037 }
Corentin Wallezb1669e32018-07-18 15:12:52 +020038 swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat());
39 dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
Corentin Walleze98678f2018-01-19 12:51:18 -050040 480);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070041
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040042 const char* vs =
43 "#version 450\n"
44 "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
45 "void main() {\n"
46 " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
47 "}\n";
Corentin Wallezb1669e32018-07-18 15:12:52 +020048 dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::ShaderStage::Vertex, vs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040049
50 const char* fs =
51 "#version 450\n"
Corentin Wallezb6fb5f32017-08-29 13:37:45 -040052 "layout(location = 0) out vec4 fragColor;"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040053 "void main() {\n"
54 " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
55 "}\n";
Corentin Wallezb1669e32018-07-18 15:12:52 +020056 dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
58 {
Yan, Shaoboa4924272018-12-10 19:47:22 +000059 dawnRenderPipelineDescriptor descriptor;
60 descriptor.nextInChain = nullptr;
61
62 dawnPipelineStageDescriptor vertexStage;
63 vertexStage.nextInChain = nullptr;
64 vertexStage.module = vsModule;
65 vertexStage.entryPoint = "main";
66 descriptor.vertexStage = &vertexStage;
67
68 dawnPipelineStageDescriptor fragmentStage;
69 fragmentStage.nextInChain = nullptr;
70 fragmentStage.module = fsModule;
71 fragmentStage.entryPoint = "main";
72 descriptor.fragmentStage = &fragmentStage;
73
74 dawnAttachmentsStateDescriptor attachmentsState;
75 attachmentsState.nextInChain = nullptr;
76 attachmentsState.numColorAttachments = 1;
77 dawnAttachmentDescriptor colorAttachment = {nullptr, swapChainFormat};
78 attachmentsState.colorAttachments = &colorAttachment;
79 attachmentsState.hasDepthStencilAttachment = false;
80 // Even with hasDepthStencilAttachment = false, depthStencilAttachment must point to valid
81 // data because we don't have optional substructures yet.
82 attachmentsState.depthStencilAttachment = &colorAttachment;
83 descriptor.attachmentsState = &attachmentsState;
84
85 descriptor.sampleCount = 1;
86
87 descriptor.numBlendStates = 1;
88 dawnBlendStateBuilder blendStateBuilder = dawnDeviceCreateBlendStateBuilder(device);
89 dawnBlendState blendState = dawnBlendStateBuilderGetResult(blendStateBuilder);
90 descriptor.blendStates = &blendState;
91 dawnBlendStateBuilderRelease(blendStateBuilder);
92
93 dawnPipelineLayoutDescriptor pl;
94 pl.nextInChain = nullptr;
95 pl.numBindGroupLayouts = 0;
96 pl.bindGroupLayouts = nullptr;
97 descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl);
98
99 dawnInputStateBuilder inputStateBuilder = dawnDeviceCreateInputStateBuilder(device);
100 descriptor.inputState = dawnInputStateBuilderGetResult(inputStateBuilder);
101 dawnInputStateBuilderRelease(inputStateBuilder);
102
103 descriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32;
104 descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
105
106 dawnDepthStencilStateBuilder depthStencilBuilder = dawnDeviceCreateDepthStencilStateBuilder(device);
107 descriptor.depthStencilState = dawnDepthStencilStateBuilderGetResult(depthStencilBuilder);
108 dawnDepthStencilStateBuilderRelease(depthStencilBuilder);
109
110 pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor);
111
112 dawnBlendStateRelease(descriptor.blendStates[0]);
113 dawnDepthStencilStateRelease(descriptor.depthStencilState);
114 dawnInputStateRelease(descriptor.inputState);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400115 }
116
Corentin Wallezb1669e32018-07-18 15:12:52 +0200117 dawnShaderModuleRelease(vsModule);
118 dawnShaderModuleRelease(fsModule);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400119}
120
121void frame() {
Corentin Wallezb1669e32018-07-18 15:12:52 +0200122 dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
123 dawnTextureView backbufferView;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700124 {
Jiawei Shao3d670502018-09-18 00:31:57 +0000125 backbufferView = dawnTextureCreateDefaultTextureView(backbuffer);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700126 }
Corentin Wallezb1669e32018-07-18 15:12:52 +0200127 dawnRenderPassDescriptor renderpassInfo;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700128 {
Corentin Wallezb1669e32018-07-18 15:12:52 +0200129 dawnRenderPassDescriptorBuilder builder = dawnDeviceCreateRenderPassDescriptorBuilder(device);
Jiawei Shao5e811ae2018-12-19 08:21:13 +0000130 dawnRenderPassColorAttachmentDescriptor colorAttachment;
131 colorAttachment.attachment = backbufferView;
132 colorAttachment.resolveTarget = nullptr;
133 colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
134 colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR;
135 colorAttachment.storeOp = DAWN_STORE_OP_STORE;
136 dawnRenderPassDescriptorBuilderSetColorAttachments(builder, 1, &colorAttachment);
Corentin Wallezb1669e32018-07-18 15:12:52 +0200137 renderpassInfo = dawnRenderPassDescriptorBuilderGetResult(builder);
138 dawnRenderPassDescriptorBuilderRelease(builder);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700139 }
Corentin Wallezb1669e32018-07-18 15:12:52 +0200140 dawnCommandBuffer commands;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400141 {
Corentin Wallezb1669e32018-07-18 15:12:52 +0200142 dawnCommandBufferBuilder builder = dawnDeviceCreateCommandBufferBuilder(device);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000143
144 dawnRenderPassEncoder pass = dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
145 dawnRenderPassEncoderSetRenderPipeline(pass, pipeline);
Jiawei Shaoc789b842018-12-10 05:20:19 +0000146 dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000147 dawnRenderPassEncoderEndPass(pass);
148 dawnRenderPassEncoderRelease(pass);
149
Corentin Wallezb1669e32018-07-18 15:12:52 +0200150 commands = dawnCommandBufferBuilderGetResult(builder);
151 dawnCommandBufferBuilderRelease(builder);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400152 }
153
Corentin Wallezb1669e32018-07-18 15:12:52 +0200154 dawnQueueSubmit(queue, 1, &commands);
155 dawnCommandBufferRelease(commands);
156 dawnSwapChainPresent(swapchain, backbuffer);
157 dawnRenderPassDescriptorRelease(renderpassInfo);
158 dawnTextureViewRelease(backbufferView);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400159
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700160 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400161}
162
163int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400164 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400165 return 1;
166 }
167 init();
168
169 while (!ShouldQuit()) {
170 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400171 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172 }
173
174 // TODO release stuff
175}