Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 15 | #include "SampleUtils.h" |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 16 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 17 | #include "utils/ComboRenderPipelineDescriptor.h" |
Corentin Wallez | f684040 | 2018-07-18 14:00:56 +0200 | [diff] [blame] | 18 | #include "utils/DawnHelpers.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 19 | #include "utils/SystemUtils.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 20 | |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 21 | #include <vector> |
Corentin Wallez | 4d7d169 | 2018-08-13 08:23:27 +0200 | [diff] [blame] | 22 | #include <glm/glm.hpp> |
| 23 | #include <glm/gtc/matrix_transform.hpp> |
| 24 | #include <glm/gtc/type_ptr.hpp> |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 25 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 26 | dawn::Device device; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 27 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 28 | dawn::Buffer indexBuffer; |
| 29 | dawn::Buffer vertexBuffer; |
| 30 | dawn::Buffer planeBuffer; |
| 31 | dawn::Buffer cameraBuffer; |
| 32 | dawn::Buffer transformBuffer[2]; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 33 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 34 | dawn::BindGroup cameraBindGroup; |
| 35 | dawn::BindGroup bindGroup[2]; |
| 36 | dawn::BindGroup cubeTransformBindGroup[2]; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 37 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 38 | dawn::Queue queue; |
| 39 | dawn::SwapChain swapchain; |
| 40 | dawn::TextureView depthStencilView; |
| 41 | dawn::RenderPipeline pipeline; |
| 42 | dawn::RenderPipeline planePipeline; |
| 43 | dawn::RenderPipeline reflectionPipeline; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 44 | |
| 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 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 65 | indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 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 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 98 | vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 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 | }; |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 106 | planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData), dawn::BufferUsageBit::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 109 | struct CameraData { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 110 | glm::mat4 view; |
| 111 | glm::mat4 proj; |
| 112 | } cameraData; |
| 113 | |
| 114 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 115 | device = CreateCppDawnDevice(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 116 | |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 117 | queue = device.CreateQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 118 | swapchain = GetSwapChain(device); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 119 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 120 | dawn::TextureUsageBit::OutputAttachment, 640, 480); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 121 | |
| 122 | initBuffers(); |
| 123 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 124 | dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 142 | dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 143 | #version 450 |
| 144 | layout(location = 2) in vec3 f_col; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 145 | layout(location = 0) out vec4 fragColor; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 146 | void main() { |
| 147 | fragColor = vec4(f_col, 1.0); |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 148 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 149 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 150 | dawn::ShaderModule fsReflectionModule = |
| 151 | utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 152 | #version 450 |
| 153 | layout(location = 2) in vec3 f_col; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 154 | layout(location = 0) out vec4 fragColor; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 155 | void main() { |
| 156 | fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0); |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 157 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 158 | |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame^] | 159 | utils::ComboInputStateDescriptor inputState; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 160 | inputState.numAttributes = 2; |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame^] | 161 | inputState.cAttributes[0].format = dawn::VertexFormat::Float3; |
| 162 | inputState.cAttributes[1].shaderLocation = 1; |
| 163 | inputState.cAttributes[1].offset = 3 * sizeof(float); |
| 164 | inputState.cAttributes[1].format = dawn::VertexFormat::Float3; |
| 165 | |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 166 | inputState.numInputs = 1; |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame^] | 167 | inputState.cInputs[0].stride = 6 * sizeof(float); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 168 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 169 | auto bgl = utils::MakeBindGroupLayout( |
| 170 | device, { |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 171 | {0, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer}, |
| 172 | {1, dawn::ShaderStageBit::Vertex, dawn::BindingType::UniformBuffer}, |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 173 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 174 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 175 | dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 176 | |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 177 | dawn::BufferDescriptor cameraBufDesc; |
| 178 | cameraBufDesc.size = sizeof(CameraData); |
| 179 | cameraBufDesc.usage = dawn::BufferUsageBit::TransferDst | dawn::BufferUsageBit::Uniform; |
| 180 | cameraBuffer = device.CreateBuffer(&cameraBufDesc); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 181 | |
| 182 | glm::mat4 transform(1.0); |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 183 | transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 184 | |
| 185 | transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f)); |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 186 | transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), dawn::BufferUsageBit::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 187 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 188 | bindGroup[0] = utils::MakeBindGroup(device, bgl, { |
Corentin Wallez | 6f0e1f9 | 2018-12-07 12:31:53 +0000 | [diff] [blame] | 189 | {0, cameraBuffer, 0, sizeof(CameraData)}, |
| 190 | {1, transformBuffer[0], 0, sizeof(glm::mat4)} |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 191 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 192 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 193 | bindGroup[1] = utils::MakeBindGroup(device, bgl, { |
Corentin Wallez | 6f0e1f9 | 2018-12-07 12:31:53 +0000 | [diff] [blame] | 194 | {0, cameraBuffer, 0, sizeof(CameraData)}, |
| 195 | {1, transformBuffer[1], 0, sizeof(glm::mat4)} |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 196 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 197 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 198 | depthStencilView = CreateDefaultDepthStencilView(device); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 199 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 200 | utils::ComboRenderPipelineDescriptor descriptor(device); |
| 201 | descriptor.layout = pl; |
| 202 | descriptor.cVertexStage.module = vsModule; |
| 203 | descriptor.cFragmentStage.module = fsModule; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 204 | descriptor.inputState = &inputState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 205 | descriptor.depthStencilState = &descriptor.cDepthStencilState; |
| 206 | descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 207 | descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat(); |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 208 | descriptor.cDepthStencilState.depthWriteEnabled = true; |
| 209 | descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 210 | |
| 211 | pipeline = device.CreateRenderPipeline(&descriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 212 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 213 | utils::ComboRenderPipelineDescriptor pDescriptor(device); |
| 214 | pDescriptor.layout = pl; |
| 215 | pDescriptor.cVertexStage.module = vsModule; |
| 216 | pDescriptor.cFragmentStage.module = fsModule; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 217 | pDescriptor.inputState = &inputState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 218 | pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState; |
| 219 | pDescriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 220 | pDescriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat(); |
Yunchao He | baa3741 | 2019-01-30 21:11:43 +0000 | [diff] [blame] | 221 | pDescriptor.cDepthStencilState.stencilFront.passOp = dawn::StencilOperation::Replace; |
| 222 | pDescriptor.cDepthStencilState.stencilBack.passOp = dawn::StencilOperation::Replace; |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 223 | pDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 224 | |
| 225 | planePipeline = device.CreateRenderPipeline(&pDescriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 226 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 227 | utils::ComboRenderPipelineDescriptor rfDescriptor(device); |
| 228 | rfDescriptor.layout = pl; |
| 229 | rfDescriptor.cVertexStage.module = vsModule; |
| 230 | rfDescriptor.cFragmentStage.module = fsReflectionModule; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 231 | rfDescriptor.inputState = &inputState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 232 | rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState; |
| 233 | rfDescriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint; |
Yunchao He | 938811e | 2019-02-20 13:00:36 +0000 | [diff] [blame] | 234 | rfDescriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat(); |
Yunchao He | 6fe9b98 | 2019-02-14 08:05:37 +0000 | [diff] [blame] | 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; |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 239 | rfDescriptor.cDepthStencilState.depthWriteEnabled = true; |
| 240 | rfDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 241 | |
| 242 | reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 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; |
| 248 | void frame() { |
| 249 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 250 | s.b += 0.01f; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 251 | if (s.b >= 1.0f) {s.b = 0.0f;} |
| 252 | static const uint32_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), |
Corentin Wallez | 804fc74 | 2018-07-03 14:43:46 +0200 | [diff] [blame] | 257 | glm::vec3(0.0f, -1.0f, 0.0f) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 258 | ); |
| 259 | |
Stephen White | e5ae327 | 2018-02-04 11:07:02 -0500 | [diff] [blame] | 260 | cameraBuffer.SetSubData(0, sizeof(CameraData), reinterpret_cast<uint8_t*>(&cameraData)); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 261 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 262 | dawn::Texture backbuffer = swapchain.GetNextTexture(); |
| 263 | utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultTextureView()}, |
| 264 | depthStencilView); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 265 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 266 | dawn::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 267 | { |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 268 | dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 269 | pass.SetPipeline(pipeline); |
Yan, Shaobo | 991ab98 | 2019-03-18 06:01:37 +0000 | [diff] [blame] | 270 | pass.SetBindGroup(0, bindGroup[0], 0, nullptr); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 271 | pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets); |
| 272 | pass.SetIndexBuffer(indexBuffer, 0); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 273 | pass.DrawIndexed(36, 1, 0, 0, 0); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 274 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 275 | pass.SetStencilReference(0x1); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 276 | pass.SetPipeline(planePipeline); |
Yan, Shaobo | 991ab98 | 2019-03-18 06:01:37 +0000 | [diff] [blame] | 277 | pass.SetBindGroup(0, bindGroup[0], 0, nullptr); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 278 | pass.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 279 | pass.DrawIndexed(6, 1, 0, 0, 0); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 280 | |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 281 | pass.SetPipeline(reflectionPipeline); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 282 | pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets); |
Yan, Shaobo | 991ab98 | 2019-03-18 06:01:37 +0000 | [diff] [blame] | 283 | pass.SetBindGroup(0, bindGroup[1], 0, nullptr); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 284 | pass.DrawIndexed(36, 1, 0, 0, 0); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 285 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 286 | pass.EndPass(); |
| 287 | } |
| 288 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 289 | dawn::CommandBuffer commands = encoder.Finish(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 290 | queue.Submit(1, &commands); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 291 | swapchain.Present(backbuffer); |
| 292 | DoFlush(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 296 | if (!InitSample(argc, argv)) { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 297 | return 1; |
| 298 | } |
| 299 | init(); |
| 300 | |
| 301 | while (!ShouldQuit()) { |
| 302 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 303 | utils::USleep(16000); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // TODO release stuff |
| 307 | } |