blob: 5a650ae0c9584e2c10273264b243ed0ff463782e [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// Copyright 2017 The NXT 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
Corentin Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040017#include "utils/NXTHelpers.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
22nxt::Device device;
23
24nxt::Buffer indexBuffer;
25nxt::Buffer vertexBuffer;
26
27nxt::Texture texture;
28nxt::Sampler sampler;
29
30nxt::Queue queue;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070031nxt::SwapChain swapchain;
32nxt::TextureView depthStencilView;
Corentin Wallez66ff4472017-07-14 11:32:57 -040033nxt::RenderPipeline pipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040034nxt::BindGroup bindGroup;
35
36void initBuffers() {
37 static const uint32_t indexData[3] = {
38 0, 1, 2,
39 };
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040040 indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::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 Wallez5ee7afd2017-06-19 13:09:41 -040047 vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048}
49
50void initTextures() {
51 texture = device.CreateTextureBuilder()
52 .SetDimension(nxt::TextureDimension::e2D)
53 .SetExtent(1024, 1024, 1)
54 .SetFormat(nxt::TextureFormat::R8G8B8A8Unorm)
55 .SetMipLevels(1)
56 .SetAllowedUsage(nxt::TextureUsageBit::TransferDst | nxt::TextureUsageBit::Sampled)
57 .GetResult();
58
Corentin Wallezb711b9b2018-05-22 14:50:59 -040059 nxt::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
60 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 Wallez83e779d2017-07-10 21:44:06 -040069 nxt::Buffer stagingBuffer = utils::CreateFrozenBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), nxt::BufferUsageBit::TransferSrc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040070 nxt::CommandBuffer copy = device.CreateCommandBufferBuilder()
71 .TransitionTextureUsage(texture, nxt::TextureUsageBit::TransferDst)
Austin Engc5f8e7a2017-07-13 11:19:14 -040072 .CopyBufferToTexture(stagingBuffer, 0, 0, texture, 0, 0, 0, 1024, 1024, 1, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040073 .GetResult();
74
75 queue.Submit(1, &copy);
76 texture.FreezeUsage(nxt::TextureUsageBit::Sampled);
77}
78
79void init() {
Corentin Wallez583e9a82017-05-29 11:30:29 -070080 device = CreateCppNXTDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081
Corentin Wallezb703def2018-06-14 20:26:27 -040082 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070083 swapchain = GetSwapChain(device);
Corentin Wallez2e31e8f2017-09-21 12:54:53 -040084 swapchain.Configure(GetPreferredSwapChainTextureFormat(),
85 nxt::TextureUsageBit::OutputAttachment, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040086
87 initBuffers();
88 initTextures();
89
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040090 nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091 #version 450
92 layout(location = 0) in vec4 pos;
93 void main() {
94 gl_Position = pos;
95 })"
96 );
97
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040098 nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040099 #version 450
100 layout(set = 0, binding = 0) uniform sampler mySampler;
101 layout(set = 0, binding = 1) uniform texture2D myTexture;
102
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400103 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400104 void main() {
105 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400106 })");
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400107
108 auto inputState = device.CreateInputStateBuilder()
109 .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
110 .SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
111 .GetResult();
112
Kai Ninomiya234becf2018-07-10 12:23:50 -0700113 auto bgl = utils::MakeBindGroupLayout(
114 device, {
115 {0, nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler},
116 {1, nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture},
117 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400118
Kai Ninomiyaf53f98b2018-06-27 16:21:39 -0700119 nxt::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400120
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700121 depthStencilView = CreateDefaultDepthStencilView(device);
122
Corentin Wallez66ff4472017-07-14 11:32:57 -0400123 pipeline = device.CreateRenderPipelineBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400124 .SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
125 .SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400126 .SetLayout(pl)
127 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
128 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
Corentin Wallez405dcd62017-09-19 13:38:05 -0400129 .SetIndexFormat(nxt::IndexFormat::Uint32)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400130 .SetInputState(inputState)
131 .GetResult();
132
133 nxt::TextureView view = texture.CreateTextureViewBuilder().GetResult();
134
135 bindGroup = device.CreateBindGroupBuilder()
136 .SetLayout(bgl)
137 .SetUsage(nxt::BindGroupUsage::Frozen)
138 .SetSamplers(0, 1, &sampler)
139 .SetTextureViews(1, 1, &view)
140 .GetResult();
141}
142
143struct {uint32_t a; float b;} s;
144void frame() {
145 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400146 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400147 if (s.b >= 1.0f) {s.b = 0.0f;}
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700148
149 nxt::Texture backbuffer;
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400150 nxt::RenderPassDescriptor renderPass;
151 GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700152
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400153 static const uint32_t vertexBufferOffsets[1] = {0};
154 nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
Corentin Wallez6f7749c2018-05-02 18:10:13 -0400155 .BeginRenderPass(renderPass)
Corentin Wallez66ff4472017-07-14 11:32:57 -0400156 .SetRenderPipeline(pipeline)
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700157 .SetBindGroup(0, bindGroup)
158 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
Corentin Wallez405dcd62017-09-19 13:38:05 -0400159 .SetIndexBuffer(indexBuffer, 0)
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700160 .DrawElements(3, 1, 0, 0)
161 .EndRenderPass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400162 .GetResult();
163
164 queue.Submit(1, &commands);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700165 backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
166 swapchain.Present(backbuffer);
167 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400168}
169
170int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400171 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172 return 1;
173 }
174 init();
175
176 while (!ShouldQuit()) {
177 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400178 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400179 }
180
181 // TODO release stuff
182}