blob: 95baf29ab7cf1496dd6e3cc7b97b573fb64712d1 [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 Wallezf07e3bd2017-04-20 14:38:20 -040020#include <vector>
21
Corentin Wallez4828d922018-07-18 13:45:46 +020022dawn::Device device;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040023
Corentin Wallez4828d922018-07-18 13:45:46 +020024dawn::Buffer indexBuffer;
25dawn::Buffer vertexBuffer;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040026
Corentin Wallez4828d922018-07-18 13:45:46 +020027dawn::Texture texture;
28dawn::Sampler sampler;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040029
Corentin Wallez4828d922018-07-18 13:45:46 +020030dawn::Queue queue;
31dawn::SwapChain swapchain;
32dawn::TextureView depthStencilView;
33dawn::RenderPipeline pipeline;
34dawn::BindGroup bindGroup;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040035
36void initBuffers() {
37 static const uint32_t indexData[3] = {
38 0, 1, 2,
39 };
Corentin Wallez4828d922018-07-18 13:45:46 +020040 indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040041
42 static const float vertexData[12] = {
43 0.0f, 0.5f, 0.0f, 1.0f,
44 -0.5f, -0.5f, 0.0f, 1.0f,
45 0.5f, -0.5f, 0.0f, 1.0f,
46 };
Corentin Wallez4828d922018-07-18 13:45:46 +020047 vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048}
49
50void initTextures() {
51 texture = device.CreateTextureBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +020052 .SetDimension(dawn::TextureDimension::e2D)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040053 .SetExtent(1024, 1024, 1)
Corentin Wallez4828d922018-07-18 13:45:46 +020054 .SetFormat(dawn::TextureFormat::R8G8B8A8Unorm)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040055 .SetMipLevels(1)
Corentin Wallez4828d922018-07-18 13:45:46 +020056 .SetAllowedUsage(dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057 .GetResult();
58
Corentin Wallez4828d922018-07-18 13:45:46 +020059 dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
Corentin Wallezb711b9b2018-05-22 14:50:59 -040060 sampler = device.CreateSampler(&samplerDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040061
62 // Initialize the texture with arbitrary data until we can load images
63 std::vector<uint8_t> data(4 * 1024 * 1024, 0);
64 for (size_t i = 0; i < data.size(); ++i) {
Kai Ninomiya78c8b832017-07-21 17:00:22 -070065 data[i] = static_cast<uint8_t>(i % 253);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040066 }
67
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040068
Corentin Wallez4828d922018-07-18 13:45:46 +020069 dawn::Buffer stagingBuffer = utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc);
70 dawn::CommandBuffer copy = device.CreateCommandBufferBuilder()
Austin Engc5f8e7a2017-07-13 11:19:14 -040071 .CopyBufferToTexture(stagingBuffer, 0, 0, texture, 0, 0, 0, 1024, 1024, 1, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040072 .GetResult();
73
74 queue.Submit(1, &copy);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040075}
76
77void init() {
Corentin Wallez583e9a82017-05-29 11:30:29 -070078 device = CreateCppNXTDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040079
Corentin Wallezb703def2018-06-14 20:26:27 -040080 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070081 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040082 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
Corentin Wallez4828d922018-07-18 13:45:46 +020083 dawn::TextureUsageBit::OutputAttachment, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040084
85 initBuffers();
86 initTextures();
87
Corentin Wallez4828d922018-07-18 13:45:46 +020088 dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040089 #version 450
90 layout(location = 0) in vec4 pos;
91 void main() {
92 gl_Position = pos;
93 })"
94 );
95
Corentin Wallez4828d922018-07-18 13:45:46 +020096 dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040097 #version 450
98 layout(set = 0, binding = 0) uniform sampler mySampler;
99 layout(set = 0, binding = 1) uniform texture2D myTexture;
100
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400101 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400102 void main() {
103 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400104 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400105
106 auto inputState = device.CreateInputStateBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200107 .SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
108 .SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400109 .GetResult();
110
Kai Ninomiya234becf2018-07-10 12:23:50 -0700111 auto bgl = utils::MakeBindGroupLayout(
112 device, {
Corentin Wallez4828d922018-07-18 13:45:46 +0200113 {0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler},
114 {1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700115 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400116
Corentin Wallez4828d922018-07-18 13:45:46 +0200117 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400118
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700119 depthStencilView = CreateDefaultDepthStencilView(device);
120
Corentin Wallez66ff4472017-07-14 11:32:57 -0400121 pipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400122 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
Corentin Wallez4828d922018-07-18 13:45:46 +0200123 .SetDepthStencilAttachmentFormat(dawn::TextureFormat::D32FloatS8Uint)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400124 .SetLayout(pl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200125 .SetStage(dawn::ShaderStage::Vertex, vsModule, "main")
126 .SetStage(dawn::ShaderStage::Fragment, fsModule, "main")
127 .SetIndexFormat(dawn::IndexFormat::Uint32)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400128 .SetInputState(inputState)
129 .GetResult();
130
Corentin Wallez4828d922018-07-18 13:45:46 +0200131 dawn::TextureView view = texture.CreateTextureViewBuilder().GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400132
133 bindGroup = device.CreateBindGroupBuilder()
134 .SetLayout(bgl)
Corentin Wallez4828d922018-07-18 13:45:46 +0200135 .SetUsage(dawn::BindGroupUsage::Frozen)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400136 .SetSamplers(0, 1, &sampler)
137 .SetTextureViews(1, 1, &view)
138 .GetResult();
139}
140
141struct {uint32_t a; float b;} s;
142void frame() {
143 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400144 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400145 if (s.b >= 1.0f) {s.b = 0.0f;}
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700146
Corentin Wallez4828d922018-07-18 13:45:46 +0200147 dawn::Texture backbuffer;
148 dawn::RenderPassDescriptor renderPass;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400149 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700150
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400151 static const uint32_t vertexBufferOffsets[1] = {0};
Corentin Wallez4828d922018-07-18 13:45:46 +0200152 dawn::CommandBuffer commands = device.CreateCommandBufferBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400153 .BeginRenderPass(renderPass)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400154 .SetRenderPipeline(pipeline)
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700155 .SetBindGroup(0, bindGroup)
156 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
Corentin Wallez405dcd62017-09-19 13:38:05 -0400157 .SetIndexBuffer(indexBuffer, 0)
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700158 .DrawElements(3, 1, 0, 0)
159 .EndRenderPass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400160 .GetResult();
161
162 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700163 swapchain.Present(backbuffer);
164 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400165}
166
167int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400168 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400169 return 1;
170 }
171 init();
172
173 while (!ShouldQuit()) {
174 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400175 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176 }
177
178 // TODO release stuff
179}