blob: c5335b5dcb88f7858750ae78f6c05526d94aaf1f [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;
Corentin Wallez66ff4472017-07-14 11:32:57 -040031nxt::RenderPipeline pipeline;
Kai Ninomiya68df8b02017-05-16 14:04:22 -070032nxt::RenderPass renderpass;
33nxt::Framebuffer framebuffer;
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
59 sampler = device.CreateSamplerBuilder()
60 .SetFilterMode(nxt::FilterMode::Linear, nxt::FilterMode::Linear, nxt::FilterMode::Linear)
61 .GetResult();
62
63 // Initialize the texture with arbitrary data until we can load images
64 std::vector<uint8_t> data(4 * 1024 * 1024, 0);
65 for (size_t i = 0; i < data.size(); ++i) {
66 data[i] = i % 253;
67 }
68
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069
Corentin Wallez83e779d2017-07-10 21:44:06 -040070 nxt::Buffer stagingBuffer = utils::CreateFrozenBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), nxt::BufferUsageBit::TransferSrc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040071 nxt::CommandBuffer copy = device.CreateCommandBufferBuilder()
72 .TransitionTextureUsage(texture, nxt::TextureUsageBit::TransferDst)
Austin Engc5f8e7a2017-07-13 11:19:14 -040073 .CopyBufferToTexture(stagingBuffer, 0, 0, texture, 0, 0, 0, 1024, 1024, 1, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040074 .GetResult();
75
76 queue.Submit(1, &copy);
77 texture.FreezeUsage(nxt::TextureUsageBit::Sampled);
78}
79
80void init() {
Corentin Wallez583e9a82017-05-29 11:30:29 -070081 device = CreateCppNXTDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040082
83 queue = device.CreateQueueBuilder().GetResult();
84
85 initBuffers();
86 initTextures();
87
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040088 nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::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 Wallez5ee7afd2017-06-19 13:09:41 -040096 nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::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
101 out vec4 fragColor;
102 void main() {
103 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
104 })"
105 );
106
107 auto inputState = device.CreateInputStateBuilder()
108 .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
109 .SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
110 .GetResult();
111
112 nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
113 .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler, 0, 1)
114 .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture, 1, 1)
115 .GetResult();
116
117 nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
118 .SetBindGroupLayout(0, bgl)
119 .GetResult();
120
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400121 utils::CreateDefaultRenderPass(device, &renderpass, &framebuffer);
Corentin Wallez66ff4472017-07-14 11:32:57 -0400122 pipeline = device.CreateRenderPipelineBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700123 .SetSubpass(renderpass, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400124 .SetLayout(pl)
125 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
126 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
127 .SetInputState(inputState)
128 .GetResult();
129
130 nxt::TextureView view = texture.CreateTextureViewBuilder().GetResult();
131
132 bindGroup = device.CreateBindGroupBuilder()
133 .SetLayout(bgl)
134 .SetUsage(nxt::BindGroupUsage::Frozen)
135 .SetSamplers(0, 1, &sampler)
136 .SetTextureViews(1, 1, &view)
137 .GetResult();
138}
139
140struct {uint32_t a; float b;} s;
141void frame() {
142 s.a = (s.a + 1) % 256;
Corentin Wallez83e779d2017-07-10 21:44:06 -0400143 s.b += 0.02f;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400144 if (s.b >= 1.0f) {s.b = 0.0f;}
145 static const uint32_t vertexBufferOffsets[1] = {0};
146 nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700147 .BeginRenderPass(renderpass, framebuffer)
Kai Ninomiyafa37f222017-06-29 23:53:08 -0700148 .BeginRenderSubpass()
Corentin Wallez66ff4472017-07-14 11:32:57 -0400149 .SetRenderPipeline(pipeline)
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700150 .SetBindGroup(0, bindGroup)
151 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
152 .SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32)
153 .DrawElements(3, 1, 0, 0)
Kai Ninomiyafa37f222017-06-29 23:53:08 -0700154 .EndRenderSubpass()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700155 .EndRenderPass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400156 .GetResult();
157
158 queue.Submit(1, &commands);
Corentin Wallez583e9a82017-05-29 11:30:29 -0700159 DoSwapBuffers();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400160}
161
162int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400163 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400164 return 1;
165 }
166 init();
167
168 while (!ShouldQuit()) {
169 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400170 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400171 }
172
173 // TODO release stuff
174}