• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
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 
15 #include "SampleUtils.h"
16 
17 #include "utils/ComboRenderPipelineDescriptor.h"
18 #include "utils/DawnHelpers.h"
19 #include "utils/SystemUtils.h"
20 
21 #include <vector>
22 #include <glm/glm.hpp>
23 #include <glm/gtc/matrix_transform.hpp>
24 #include <glm/gtc/type_ptr.hpp>
25 
26 dawn::Device device;
27 
28 dawn::Buffer indexBuffer;
29 dawn::Buffer vertexBuffer;
30 dawn::Buffer planeBuffer;
31 dawn::Buffer cameraBuffer;
32 dawn::Buffer transformBuffer[2];
33 
34 dawn::BindGroup cameraBindGroup;
35 dawn::BindGroup bindGroup[2];
36 dawn::BindGroup cubeTransformBindGroup[2];
37 
38 dawn::Queue queue;
39 dawn::SwapChain swapchain;
40 dawn::TextureView depthStencilView;
41 dawn::RenderPipeline pipeline;
42 dawn::RenderPipeline planePipeline;
43 dawn::RenderPipeline reflectionPipeline;
44 
initBuffers()45 void initBuffers() {
46     static const uint32_t indexData[6*6] = {
47         0, 1, 2,
48         0, 2, 3,
49 
50         4, 5, 6,
51         4, 6, 7,
52 
53         8, 9, 10,
54         8, 10, 11,
55 
56         12, 13, 14,
57         12, 14, 15,
58 
59         16, 17, 18,
60         16, 18, 19,
61 
62         20, 21, 22,
63         20, 22, 23
64     };
65     indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
66 
67     static const float vertexData[6 * 4 * 6] = {
68         -1.0, -1.0,  1.0,    1.0, 0.0, 0.0,
69         1.0, -1.0,  1.0,    1.0, 0.0, 0.0,
70         1.0,  1.0,  1.0,    1.0, 0.0, 0.0,
71         -1.0,  1.0,  1.0,    1.0, 0.0, 0.0,
72 
73         -1.0, -1.0, -1.0,    1.0, 1.0, 0.0,
74         -1.0,  1.0, -1.0,    1.0, 1.0, 0.0,
75         1.0,  1.0, -1.0,    1.0, 1.0, 0.0,
76         1.0, -1.0, -1.0,    1.0, 1.0, 0.0,
77 
78         -1.0,  1.0, -1.0,    1.0, 0.0, 1.0,
79         -1.0,  1.0,  1.0,    1.0, 0.0, 1.0,
80         1.0,  1.0,  1.0,    1.0, 0.0, 1.0,
81         1.0,  1.0, -1.0,    1.0, 0.0, 1.0,
82 
83         -1.0, -1.0, -1.0,    0.0, 1.0, 0.0,
84         1.0, -1.0, -1.0,    0.0, 1.0, 0.0,
85         1.0, -1.0,  1.0,    0.0, 1.0, 0.0,
86         -1.0, -1.0,  1.0,    0.0, 1.0, 0.0,
87 
88         1.0, -1.0, -1.0,    0.0, 1.0, 1.0,
89         1.0,  1.0, -1.0,    0.0, 1.0, 1.0,
90         1.0,  1.0,  1.0,    0.0, 1.0, 1.0,
91         1.0, -1.0,  1.0,    0.0, 1.0, 1.0,
92 
93         -1.0, -1.0, -1.0,    1.0, 1.0, 1.0,
94         -1.0, -1.0,  1.0,    1.0, 1.0, 1.0,
95         -1.0,  1.0,  1.0,    1.0, 1.0, 1.0,
96         -1.0,  1.0, -1.0,    1.0, 1.0, 1.0
97     };
98     vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
99 
100     static const float planeData[6 * 4] = {
101         -2.0, -1.0, -2.0,    0.5, 0.5, 0.5,
102         2.0, -1.0, -2.0,    0.5, 0.5, 0.5,
103         2.0, -1.0,  2.0,    0.5, 0.5, 0.5,
104         -2.0, -1.0,  2.0,    0.5, 0.5, 0.5,
105     };
106     planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData), dawn::BufferUsageBit::Vertex);
107 }
108 
109 struct CameraData {
110     glm::mat4 view;
111     glm::mat4 proj;
112 } cameraData;
113 
init()114 void init() {
115     device = CreateCppDawnDevice();
116 
117     queue = device.CreateQueue();
118     swapchain = GetSwapChain(device);
119     swapchain.Configure(GetPreferredSwapChainTextureFormat(),
120                         dawn::TextureUsageBit::OutputAttachment, 640, 480);
121 
122     initBuffers();
123 
124     dawn::ShaderModule vsModule = utils::CreateShaderModule(device, utils::ShaderStage::Vertex, R"(
125         #version 450
126         layout(set = 0, binding = 0) uniform cameraData {
127             mat4 view;
128             mat4 proj;
129         } camera;
130         layout(set = 0, binding = 1) uniform modelData {
131             mat4 modelMatrix;
132         };
133         layout(location = 0) in vec3 pos;
134         layout(location = 1) in vec3 col;
135         layout(location = 2) out vec3 f_col;
136         void main() {
137             f_col = col;
138             gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
139         })");
140 
141     dawn::ShaderModule fsModule =
142         utils::CreateShaderModule(device, utils::ShaderStage::Fragment, R"(
143         #version 450
144         layout(location = 2) in vec3 f_col;
145         layout(location = 0) out vec4 fragColor;
146         void main() {
147             fragColor = vec4(f_col, 1.0);
148         })");
149 
150     dawn::ShaderModule fsReflectionModule =
151         utils::CreateShaderModule(device, utils::ShaderStage::Fragment, R"(
152         #version 450
153         layout(location = 2) in vec3 f_col;
154         layout(location = 0) out vec4 fragColor;
155         void main() {
156             fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
157         })");
158 
159     utils::ComboVertexInputDescriptor vertexInput;
160     vertexInput.cBuffers[0].attributeCount = 2;
161     vertexInput.cAttributes[0].format = dawn::VertexFormat::Float3;
162     vertexInput.cAttributes[1].shaderLocation = 1;
163     vertexInput.cAttributes[1].offset = 3 * sizeof(float);
164     vertexInput.cAttributes[1].format = dawn::VertexFormat::Float3;
165 
166     vertexInput.bufferCount = 1;
167     vertexInput.cBuffers[0].stride = 6 * sizeof(float);
168 
169     auto bgl = utils::MakeBindGroupLayout(
170         device, {
171                     {0, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
172                     {1, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer},
173                 });
174 
175     dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
176 
177     dawn::BufferDescriptor cameraBufDesc;
178     cameraBufDesc.size = sizeof(CameraData);
179     cameraBufDesc.usage = dawn::BufferUsageBit::CopyDst | dawn::BufferUsageBit::Uniform;
180     cameraBuffer = device.CreateBuffer(&cameraBufDesc);
181 
182     glm::mat4 transform(1.0);
183     transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform);
184 
185     transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
186     transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform);
187 
188     bindGroup[0] = utils::MakeBindGroup(device, bgl, {
189         {0, cameraBuffer, 0, sizeof(CameraData)},
190         {1, transformBuffer[0], 0, sizeof(glm::mat4)}
191     });
192 
193     bindGroup[1] = utils::MakeBindGroup(device, bgl, {
194         {0, cameraBuffer, 0, sizeof(CameraData)},
195         {1, transformBuffer[1], 0, sizeof(glm::mat4)}
196     });
197 
198     depthStencilView = CreateDefaultDepthStencilView(device);
199 
200     utils::ComboRenderPipelineDescriptor descriptor(device);
201     descriptor.layout = pl;
202     descriptor.cVertexStage.module = vsModule;
203     descriptor.cFragmentStage.module = fsModule;
204     descriptor.vertexInput = &vertexInput;
205     descriptor.depthStencilState = &descriptor.cDepthStencilState;
206     descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
207     descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
208     descriptor.cDepthStencilState.depthWriteEnabled = true;
209     descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
210 
211     pipeline = device.CreateRenderPipeline(&descriptor);
212 
213     utils::ComboRenderPipelineDescriptor pDescriptor(device);
214     pDescriptor.layout = pl;
215     pDescriptor.cVertexStage.module = vsModule;
216     pDescriptor.cFragmentStage.module = fsModule;
217     pDescriptor.vertexInput = &vertexInput;
218     pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState;
219     pDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
220     pDescriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
221     pDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
222     pDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
223     pDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
224 
225     planePipeline = device.CreateRenderPipeline(&pDescriptor);
226 
227     utils::ComboRenderPipelineDescriptor rfDescriptor(device);
228     rfDescriptor.layout = pl;
229     rfDescriptor.cVertexStage.module = vsModule;
230     rfDescriptor.cFragmentStage.module = fsReflectionModule;
231     rfDescriptor.vertexInput = &vertexInput;
232     rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState;
233     rfDescriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
234     rfDescriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
235     rfDescriptor.cDepthStencilState.stencilFront.compare = dawn::CompareFunction::Equal;
236     rfDescriptor.cDepthStencilState.stencilBack.compare = dawn::CompareFunction::Equal;
237     rfDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace;
238     rfDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace;
239     rfDescriptor.cDepthStencilState.depthWriteEnabled = true;
240     rfDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
241 
242     reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor);
243 
244     cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
245 }
246 
247 struct {uint32_t a; float b;} s;
frame()248 void frame() {
249     s.a = (s.a + 1) % 256;
250     s.b += 0.01f;
251     if (s.b >= 1.0f) {s.b = 0.0f;}
252     static const uint64_t vertexBufferOffsets[1] = {0};
253 
254     cameraData.view = glm::lookAt(
255         glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
256         glm::vec3(0.0f, 0.0f, 0.0f),
257         glm::vec3(0.0f, -1.0f, 0.0f)
258     );
259 
260     cameraBuffer.SetSubData(0, sizeof(CameraData), &cameraData);
261 
262     dawn::Texture backbuffer = swapchain.GetNextTexture();
263     utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultView()},
264                                                 depthStencilView);
265 
266     dawn::CommandEncoder encoder = device.CreateCommandEncoder();
267     {
268         dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
269         pass.SetPipeline(pipeline);
270         pass.SetBindGroup(0, bindGroup[0], 0, nullptr);
271         pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
272         pass.SetIndexBuffer(indexBuffer, 0);
273         pass.DrawIndexed(36, 1, 0, 0, 0);
274 
275         pass.SetStencilReference(0x1);
276         pass.SetPipeline(planePipeline);
277         pass.SetBindGroup(0, bindGroup[0], 0, nullptr);
278         pass.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets);
279         pass.DrawIndexed(6, 1, 0, 0, 0);
280 
281         pass.SetPipeline(reflectionPipeline);
282         pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
283         pass.SetBindGroup(0, bindGroup[1], 0, nullptr);
284         pass.DrawIndexed(36, 1, 0, 0, 0);
285 
286         pass.EndPass();
287     }
288 
289     dawn::CommandBuffer commands = encoder.Finish();
290     queue.Submit(1, &commands);
291     swapchain.Present(backbuffer);
292     DoFlush();
293 }
294 
main(int argc,const char * argv[])295 int main(int argc, const char* argv[]) {
296     if (!InitSample(argc, argv)) {
297         return 1;
298     }
299     init();
300 
301     while (!ShouldQuit()) {
302         frame();
303         utils::USleep(16000);
304     }
305 
306     // TODO release stuff
307 }
308