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 | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 19 | #include "utils/WGPUHelpers.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 | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 26 | wgpu::Device device; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 27 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 28 | wgpu::Buffer indexBuffer; |
| 29 | wgpu::Buffer vertexBuffer; |
| 30 | wgpu::Buffer planeBuffer; |
| 31 | wgpu::Buffer cameraBuffer; |
| 32 | wgpu::Buffer transformBuffer[2]; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 33 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 34 | wgpu::BindGroup cameraBindGroup; |
| 35 | wgpu::BindGroup bindGroup[2]; |
| 36 | wgpu::BindGroup cubeTransformBindGroup[2]; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 37 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 38 | wgpu::Queue queue; |
| 39 | wgpu::SwapChain swapchain; |
| 40 | wgpu::TextureView depthStencilView; |
| 41 | wgpu::RenderPipeline pipeline; |
| 42 | wgpu::RenderPipeline planePipeline; |
| 43 | wgpu::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 | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 65 | indexBuffer = |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 66 | utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 67 | |
| 68 | static const float vertexData[6 * 4 * 6] = { |
| 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 | -1.0, 1.0, 1.0, 1.0, 0.0, 0.0, |
| 73 | |
| 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 | 1.0, -1.0, -1.0, 1.0, 1.0, 0.0, |
| 78 | |
| 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 | 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, |
| 83 | |
| 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 | -1.0, -1.0, 1.0, 0.0, 1.0, 0.0, |
| 88 | |
| 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 | 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, |
| 93 | |
| 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 | -1.0, 1.0, -1.0, 1.0, 1.0, 1.0 |
| 98 | }; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 99 | vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 100 | wgpu::BufferUsage::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 101 | |
| 102 | static const float planeData[6 * 4] = { |
| 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 | 2.0, -1.0, 2.0, 0.5, 0.5, 0.5, |
| 106 | -2.0, -1.0, 2.0, 0.5, 0.5, 0.5, |
| 107 | }; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 108 | planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 109 | wgpu::BufferUsage::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 112 | struct CameraData { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 113 | glm::mat4 view; |
| 114 | glm::mat4 proj; |
| 115 | } cameraData; |
| 116 | |
| 117 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 118 | device = CreateCppDawnDevice(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 119 | |
Corentin Wallez | b703def | 2018-06-14 20:26:27 -0400 | [diff] [blame] | 120 | queue = device.CreateQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 121 | swapchain = GetSwapChain(device); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 122 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment, |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 123 | 640, 480); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 124 | |
| 125 | initBuffers(); |
| 126 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 127 | wgpu::ShaderModule vsModule = |
Corentin Wallez | b9b088f | 2019-08-27 08:42:29 +0000 | [diff] [blame] | 128 | utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 129 | #version 450 |
| 130 | layout(set = 0, binding = 0) uniform cameraData { |
| 131 | mat4 view; |
| 132 | mat4 proj; |
| 133 | } camera; |
| 134 | layout(set = 0, binding = 1) uniform modelData { |
| 135 | mat4 modelMatrix; |
| 136 | }; |
| 137 | layout(location = 0) in vec3 pos; |
| 138 | layout(location = 1) in vec3 col; |
| 139 | layout(location = 2) out vec3 f_col; |
| 140 | void main() { |
| 141 | f_col = col; |
| 142 | gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0); |
Corentin Wallez | 2a1d8c2 | 2019-07-12 17:52:22 +0000 | [diff] [blame] | 143 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 144 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 145 | wgpu::ShaderModule fsModule = |
Corentin Wallez | b9b088f | 2019-08-27 08:42:29 +0000 | [diff] [blame] | 146 | utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 147 | #version 450 |
| 148 | layout(location = 2) in vec3 f_col; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 149 | layout(location = 0) out vec4 fragColor; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 150 | void main() { |
| 151 | fragColor = vec4(f_col, 1.0); |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 152 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 153 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 154 | wgpu::ShaderModule fsReflectionModule = |
Corentin Wallez | b9b088f | 2019-08-27 08:42:29 +0000 | [diff] [blame] | 155 | utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 156 | #version 450 |
| 157 | layout(location = 2) in vec3 f_col; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 158 | layout(location = 0) out vec4 fragColor; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 159 | void main() { |
| 160 | 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] | 161 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 162 | |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 163 | utils::ComboVertexStateDescriptor vertexState; |
| 164 | vertexState.cVertexBuffers[0].attributeCount = 2; |
| 165 | vertexState.cAttributes[0].format = wgpu::VertexFormat::Float3; |
| 166 | vertexState.cAttributes[1].shaderLocation = 1; |
| 167 | vertexState.cAttributes[1].offset = 3 * sizeof(float); |
| 168 | vertexState.cAttributes[1].format = wgpu::VertexFormat::Float3; |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame] | 169 | |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 170 | vertexState.vertexBufferCount = 1; |
| 171 | vertexState.cVertexBuffers[0].arrayStride = 6 * sizeof(float); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 172 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 173 | auto bgl = utils::MakeBindGroupLayout( |
| 174 | device, { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 175 | {0, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer}, |
| 176 | {1, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer}, |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 177 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 178 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 179 | wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 180 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 181 | wgpu::BufferDescriptor cameraBufDesc; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 182 | cameraBufDesc.size = sizeof(CameraData); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 183 | cameraBufDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 184 | cameraBuffer = device.CreateBuffer(&cameraBufDesc); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 185 | |
| 186 | glm::mat4 transform(1.0); |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 187 | transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 188 | wgpu::BufferUsage::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 189 | |
| 190 | transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f)); |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 191 | transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 192 | wgpu::BufferUsage::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 193 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 194 | bindGroup[0] = utils::MakeBindGroup(device, bgl, { |
Corentin Wallez | 6f0e1f9 | 2018-12-07 12:31:53 +0000 | [diff] [blame] | 195 | {0, cameraBuffer, 0, sizeof(CameraData)}, |
| 196 | {1, transformBuffer[0], 0, sizeof(glm::mat4)} |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 197 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 198 | |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 199 | bindGroup[1] = utils::MakeBindGroup(device, bgl, { |
Corentin Wallez | 6f0e1f9 | 2018-12-07 12:31:53 +0000 | [diff] [blame] | 200 | {0, cameraBuffer, 0, sizeof(CameraData)}, |
| 201 | {1, transformBuffer[1], 0, sizeof(glm::mat4)} |
Corentin Wallez | 6f9d21e | 2018-12-05 07:18:30 +0000 | [diff] [blame] | 202 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 203 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 204 | depthStencilView = CreateDefaultDepthStencilView(device); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 205 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 206 | utils::ComboRenderPipelineDescriptor descriptor(device); |
| 207 | descriptor.layout = pl; |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 208 | descriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 209 | descriptor.cFragmentStage.module = fsModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 210 | descriptor.vertexState = &vertexState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 211 | descriptor.depthStencilState = &descriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 212 | descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 213 | descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 214 | descriptor.cDepthStencilState.depthWriteEnabled = true; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 215 | descriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 216 | |
| 217 | pipeline = device.CreateRenderPipeline(&descriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 218 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 219 | utils::ComboRenderPipelineDescriptor pDescriptor(device); |
| 220 | pDescriptor.layout = pl; |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 221 | pDescriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 222 | pDescriptor.cFragmentStage.module = fsModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 223 | pDescriptor.vertexState = &vertexState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 224 | pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 225 | pDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 226 | pDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 227 | pDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace; |
| 228 | pDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace; |
| 229 | pDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 230 | |
| 231 | planePipeline = device.CreateRenderPipeline(&pDescriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 232 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 233 | utils::ComboRenderPipelineDescriptor rfDescriptor(device); |
| 234 | rfDescriptor.layout = pl; |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 235 | rfDescriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 236 | rfDescriptor.cFragmentStage.module = fsReflectionModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 237 | rfDescriptor.vertexState = &vertexState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 238 | rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 239 | rfDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 240 | rfDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 241 | rfDescriptor.cDepthStencilState.stencilFront.compare = wgpu::CompareFunction::Equal; |
| 242 | rfDescriptor.cDepthStencilState.stencilBack.compare = wgpu::CompareFunction::Equal; |
| 243 | rfDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace; |
| 244 | rfDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace; |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 245 | rfDescriptor.cDepthStencilState.depthWriteEnabled = true; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 246 | rfDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 247 | |
| 248 | reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 249 | |
| 250 | cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f); |
| 251 | } |
| 252 | |
| 253 | struct {uint32_t a; float b;} s; |
| 254 | void frame() { |
| 255 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 256 | s.b += 0.01f; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 257 | if (s.b >= 1.0f) {s.b = 0.0f;} |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 258 | |
| 259 | cameraData.view = glm::lookAt( |
| 260 | glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))), |
| 261 | glm::vec3(0.0f, 0.0f, 0.0f), |
Corentin Wallez | d46cabd | 2019-09-20 23:53:27 +0000 | [diff] [blame] | 262 | glm::vec3(0.0f, 1.0f, 0.0f) |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 263 | ); |
| 264 | |
Corentin Wallez | 67ab1ea | 2019-06-06 16:19:15 +0000 | [diff] [blame] | 265 | cameraBuffer.SetSubData(0, sizeof(CameraData), &cameraData); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 266 | |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame^] | 267 | wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView(); |
| 268 | utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 269 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 270 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 271 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 272 | wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 273 | pass.SetPipeline(pipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 274 | pass.SetBindGroup(0, bindGroup[0]); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 275 | pass.SetVertexBuffer(0, vertexBuffer); |
Corentin Wallez | d285525 | 2019-10-10 17:21:48 +0000 | [diff] [blame] | 276 | pass.SetIndexBuffer(indexBuffer); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 277 | pass.DrawIndexed(36, 1, 0, 0, 0); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 278 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 279 | pass.SetStencilReference(0x1); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 280 | pass.SetPipeline(planePipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 281 | pass.SetBindGroup(0, bindGroup[0]); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 282 | pass.SetVertexBuffer(0, planeBuffer); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 283 | pass.DrawIndexed(6, 1, 0, 0, 0); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 284 | |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 285 | pass.SetPipeline(reflectionPipeline); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 286 | pass.SetVertexBuffer(0, vertexBuffer); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 287 | pass.SetBindGroup(0, bindGroup[1]); |
Jiawei Shao | ff9562f | 2018-12-13 01:05:26 +0000 | [diff] [blame] | 288 | pass.DrawIndexed(36, 1, 0, 0, 0); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 289 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 290 | pass.EndPass(); |
| 291 | } |
| 292 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 293 | wgpu::CommandBuffer commands = encoder.Finish(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 294 | queue.Submit(1, &commands); |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame^] | 295 | swapchain.Present(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 296 | DoFlush(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 300 | if (!InitSample(argc, argv)) { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 301 | return 1; |
| 302 | } |
| 303 | init(); |
| 304 | |
| 305 | while (!ShouldQuit()) { |
| 306 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 307 | utils::USleep(16000); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // TODO release stuff |
| 311 | } |