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 | |
Corentin Wallez | 4d7d169 | 2018-08-13 08:23:27 +0200 | [diff] [blame] | 21 | #include <glm/glm.hpp> |
| 22 | #include <glm/gtc/matrix_transform.hpp> |
| 23 | #include <glm/gtc/type_ptr.hpp> |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 24 | #include <vector> |
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() { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 46 | static const uint32_t indexData[6 * 6] = {0, 1, 2, 0, 2, 3, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 47 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 48 | 4, 5, 6, 4, 6, 7, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 49 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 50 | 8, 9, 10, 8, 10, 11, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 51 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 52 | 12, 13, 14, 12, 14, 15, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 53 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 54 | 16, 17, 18, 16, 18, 19, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 55 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 56 | 20, 21, 22, 20, 22, 23}; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 57 | indexBuffer = |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 58 | utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 59 | |
| 60 | static const float vertexData[6 * 4 * 6] = { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 61 | -1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, |
| 62 | 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 0.0, 0.0, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 63 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 64 | -1.0, -1.0, -1.0, 1.0, 1.0, 0.0, -1.0, 1.0, -1.0, 1.0, 1.0, 0.0, |
| 65 | 1.0, 1.0, -1.0, 1.0, 1.0, 0.0, 1.0, -1.0, -1.0, 1.0, 1.0, 0.0, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 66 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 67 | -1.0, 1.0, -1.0, 1.0, 0.0, 1.0, -1.0, 1.0, 1.0, 1.0, 0.0, 1.0, |
| 68 | 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 69 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 70 | -1.0, -1.0, -1.0, 0.0, 1.0, 0.0, 1.0, -1.0, -1.0, 0.0, 1.0, 0.0, |
| 71 | 1.0, -1.0, 1.0, 0.0, 1.0, 0.0, -1.0, -1.0, 1.0, 0.0, 1.0, 0.0, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 72 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 73 | 1.0, -1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 1.0, -1.0, 0.0, 1.0, 1.0, |
| 74 | 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 75 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 76 | -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, |
| 77 | -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0}; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 78 | vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 79 | wgpu::BufferUsage::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 80 | |
| 81 | static const float planeData[6 * 4] = { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 82 | -2.0, -1.0, -2.0, 0.5, 0.5, 0.5, 2.0, -1.0, -2.0, 0.5, 0.5, 0.5, |
| 83 | 2.0, -1.0, 2.0, 0.5, 0.5, 0.5, -2.0, -1.0, 2.0, 0.5, 0.5, 0.5, |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 84 | }; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 85 | planeBuffer = utils::CreateBufferFromData(device, planeData, sizeof(planeData), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 86 | wgpu::BufferUsage::Vertex); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Austin Eng | 58c76b3 | 2017-06-02 14:31:53 -0400 | [diff] [blame] | 89 | struct CameraData { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 90 | glm::mat4 view; |
| 91 | glm::mat4 proj; |
| 92 | } cameraData; |
| 93 | |
| 94 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 95 | device = CreateCppDawnDevice(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 96 | |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame^] | 97 | queue = device.GetQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 98 | swapchain = GetSwapChain(device); |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 99 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment, |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 100 | 640, 480); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 101 | |
| 102 | initBuffers(); |
| 103 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 104 | wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"( |
| 105 | [[block]] struct Camera { |
| 106 | [[offset(0)]] view : mat4x4<f32>; |
| 107 | [[offset(64)]] proj : mat4x4<f32>; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 108 | }; |
dan sinclair | 0f9c2d7 | 2021-01-19 14:18:51 +0000 | [diff] [blame] | 109 | [[group(0), binding(0)]] var<uniform> camera : Camera; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 110 | |
| 111 | [[block]] struct Model { |
| 112 | [[offset(0)]] matrix : mat4x4<f32>; |
| 113 | }; |
dan sinclair | 0f9c2d7 | 2021-01-19 14:18:51 +0000 | [diff] [blame] | 114 | [[group(0), binding(1)]] var<uniform> model : Model; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 115 | |
| 116 | [[location(0)]] var<in> pos : vec3<f32>; |
| 117 | [[location(1)]] var<in> col : vec3<f32>; |
| 118 | |
| 119 | [[location(2)]] var<out> f_col : vec3<f32>; |
| 120 | [[builtin(position)]] var<out> Position : vec4<f32>; |
| 121 | |
| 122 | [[stage(vertex)]] fn main() -> void { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 123 | f_col = col; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 124 | Position = camera.proj * camera.view * model.matrix * vec4<f32>(pos, 1.0); |
| 125 | return; |
Corentin Wallez | 2a1d8c2 | 2019-07-12 17:52:22 +0000 | [diff] [blame] | 126 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 127 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 128 | wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"( |
| 129 | [[location(0)]] var<out> FragColor : vec4<f32>; |
| 130 | [[location(2)]] var<in> f_col : vec3<f32>; |
| 131 | |
| 132 | [[stage(fragment)]] fn main() -> void { |
| 133 | FragColor = vec4<f32>(f_col, 1.0); |
| 134 | return; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 135 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 136 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 137 | wgpu::ShaderModule fsReflectionModule = utils::CreateShaderModuleFromWGSL(device, R"( |
| 138 | [[location(0)]] var<out> FragColor : vec4<f32>; |
| 139 | [[location(2)]] var<in> f_col : vec3<f32>; |
| 140 | |
| 141 | [[stage(fragment)]] fn main() -> void { |
| 142 | FragColor = vec4<f32>(mix(f_col, vec3<f32>(0.5, 0.5, 0.5), vec3<f32>(0.5, 0.5, 0.5)), 1.0); |
| 143 | return; |
Corentin Wallez | b6fb5f3 | 2017-08-29 13:37:45 -0400 | [diff] [blame] | 144 | })"); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 145 | |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 146 | utils::ComboVertexStateDescriptor vertexState; |
| 147 | vertexState.cVertexBuffers[0].attributeCount = 2; |
| 148 | vertexState.cAttributes[0].format = wgpu::VertexFormat::Float3; |
| 149 | vertexState.cAttributes[1].shaderLocation = 1; |
| 150 | vertexState.cAttributes[1].offset = 3 * sizeof(float); |
| 151 | vertexState.cAttributes[1].format = wgpu::VertexFormat::Float3; |
Yunchao He | 1ba2cb8 | 2019-03-28 05:09:01 +0000 | [diff] [blame] | 152 | |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 153 | vertexState.vertexBufferCount = 1; |
| 154 | vertexState.cVertexBuffers[0].arrayStride = 6 * sizeof(float); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 155 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 156 | auto bgl = utils::MakeBindGroupLayout( |
| 157 | device, { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 158 | {0, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer}, |
| 159 | {1, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer}, |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 160 | }); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 161 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 162 | wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 163 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 164 | wgpu::BufferDescriptor cameraBufDesc; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 165 | cameraBufDesc.size = sizeof(CameraData); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 166 | cameraBufDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 167 | cameraBuffer = device.CreateBuffer(&cameraBufDesc); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 168 | |
| 169 | glm::mat4 transform(1.0); |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 170 | transformBuffer[0] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 171 | wgpu::BufferUsage::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 172 | |
| 173 | transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f)); |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 174 | transformBuffer[1] = utils::CreateBufferFromData(device, &transform, sizeof(glm::mat4), |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 175 | wgpu::BufferUsage::Uniform); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 176 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 177 | bindGroup[0] = utils::MakeBindGroup( |
| 178 | device, bgl, |
| 179 | {{0, cameraBuffer, 0, sizeof(CameraData)}, {1, transformBuffer[0], 0, sizeof(glm::mat4)}}); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 180 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 181 | bindGroup[1] = utils::MakeBindGroup( |
| 182 | device, bgl, |
| 183 | {{0, cameraBuffer, 0, sizeof(CameraData)}, {1, transformBuffer[1], 0, sizeof(glm::mat4)}}); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 184 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 185 | depthStencilView = CreateDefaultDepthStencilView(device); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 186 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 187 | utils::ComboRenderPipelineDescriptor descriptor(device); |
| 188 | descriptor.layout = pl; |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 189 | descriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 190 | descriptor.cFragmentStage.module = fsModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 191 | descriptor.vertexState = &vertexState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 192 | descriptor.depthStencilState = &descriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 193 | descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 194 | descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 195 | descriptor.cDepthStencilState.depthWriteEnabled = true; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 196 | descriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 197 | |
| 198 | pipeline = device.CreateRenderPipeline(&descriptor); |
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 pDescriptor(device); |
| 201 | pDescriptor.layout = pl; |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 202 | pDescriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 203 | pDescriptor.cFragmentStage.module = fsModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 204 | pDescriptor.vertexState = &vertexState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 205 | pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 206 | pDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 207 | pDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 208 | pDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace; |
| 209 | pDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace; |
| 210 | pDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 211 | |
| 212 | planePipeline = device.CreateRenderPipeline(&pDescriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 213 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 214 | utils::ComboRenderPipelineDescriptor rfDescriptor(device); |
| 215 | rfDescriptor.layout = pl; |
Corentin Wallez | c6c7a42 | 2019-09-05 09:35:07 +0000 | [diff] [blame] | 216 | rfDescriptor.vertexStage.module = vsModule; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 217 | rfDescriptor.cFragmentStage.module = fsReflectionModule; |
Kai Ninomiya | ae1f25f | 2019-11-07 22:23:29 +0000 | [diff] [blame] | 218 | rfDescriptor.vertexState = &vertexState; |
Yunchao He | 108bcbd | 2019-02-15 02:20:57 +0000 | [diff] [blame] | 219 | rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 220 | rfDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Corentin Wallez | c81a717 | 2019-09-20 23:22:27 +0000 | [diff] [blame] | 221 | rfDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 222 | rfDescriptor.cDepthStencilState.stencilFront.compare = wgpu::CompareFunction::Equal; |
| 223 | rfDescriptor.cDepthStencilState.stencilBack.compare = wgpu::CompareFunction::Equal; |
| 224 | rfDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace; |
| 225 | rfDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace; |
Yunchao He | ea56333 | 2019-01-04 04:28:37 +0000 | [diff] [blame] | 226 | rfDescriptor.cDepthStencilState.depthWriteEnabled = true; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 227 | rfDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 228 | |
| 229 | reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 230 | |
| 231 | cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f); |
| 232 | } |
| 233 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 234 | struct { |
| 235 | uint32_t a; |
| 236 | float b; |
| 237 | } s; |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 238 | void frame() { |
| 239 | s.a = (s.a + 1) % 256; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 240 | s.b += 0.01f; |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 241 | if (s.b >= 1.0f) { |
| 242 | s.b = 0.0f; |
| 243 | } |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 244 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 245 | cameraData.view = glm::lookAt(glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, |
| 246 | 8.f * std::cos(glm::radians(s.b * 360.f))), |
| 247 | glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 248 | |
Corentin Wallez | 47a3341 | 2020-06-02 09:24:39 +0000 | [diff] [blame] | 249 | queue.WriteBuffer(cameraBuffer, 0, &cameraData, sizeof(CameraData)); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 250 | |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 251 | wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView(); |
| 252 | utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 253 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 254 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 255 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 256 | wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 257 | pass.SetPipeline(pipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 258 | pass.SetBindGroup(0, bindGroup[0]); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 259 | pass.SetVertexBuffer(0, vertexBuffer); |
Corentin Wallez | 5fad85b | 2020-11-25 08:54:14 +0000 | [diff] [blame] | 260 | pass.SetIndexBuffer(indexBuffer, wgpu::IndexFormat::Uint32); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 261 | pass.DrawIndexed(36); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 262 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 263 | pass.SetStencilReference(0x1); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 264 | pass.SetPipeline(planePipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 265 | pass.SetBindGroup(0, bindGroup[0]); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 266 | pass.SetVertexBuffer(0, planeBuffer); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 267 | pass.DrawIndexed(6); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 268 | |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 269 | pass.SetPipeline(reflectionPipeline); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 270 | pass.SetVertexBuffer(0, vertexBuffer); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 271 | pass.SetBindGroup(0, bindGroup[1]); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 272 | pass.DrawIndexed(36); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 273 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 274 | pass.EndPass(); |
| 275 | } |
| 276 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 277 | wgpu::CommandBuffer commands = encoder.Finish(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 278 | queue.Submit(1, &commands); |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 279 | swapchain.Present(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 280 | DoFlush(); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 284 | if (!InitSample(argc, argv)) { |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 285 | return 1; |
| 286 | } |
| 287 | init(); |
| 288 | |
| 289 | while (!ShouldQuit()) { |
| 290 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 291 | utils::USleep(16000); |
Austin Eng | 376f1c6 | 2017-05-30 20:03:44 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // TODO release stuff |
| 295 | } |