blob: ae1546939f73ff50c6b10362f5f26a3b6703f08b [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
15#include "Utils.h"
16
17#include <unistd.h>
18#include <vector>
19
20nxt::Device device;
21
22nxt::Buffer indexBuffer;
23nxt::Buffer vertexBuffer;
24
25nxt::Texture texture;
26nxt::Sampler sampler;
27
28nxt::Queue queue;
29nxt::Pipeline pipeline;
Kai Ninomiya68df8b02017-05-16 14:04:22 -070030nxt::RenderPass renderpass;
31nxt::Framebuffer framebuffer;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040032nxt::BindGroup bindGroup;
33
34void initBuffers() {
35 static const uint32_t indexData[3] = {
36 0, 1, 2,
37 };
38 indexBuffer = device.CreateBufferBuilder()
39 .SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Index)
40 .SetInitialUsage(nxt::BufferUsageBit::Mapped)
41 .SetSize(sizeof(indexData))
42 .GetResult();
43 indexBuffer.SetSubData(0, sizeof(indexData) / sizeof(uint32_t), indexData);
44 indexBuffer.FreezeUsage(nxt::BufferUsageBit::Index);
45
46 static const float vertexData[12] = {
47 0.0f, 0.5f, 0.0f, 1.0f,
48 -0.5f, -0.5f, 0.0f, 1.0f,
49 0.5f, -0.5f, 0.0f, 1.0f,
50 };
51 vertexBuffer = device.CreateBufferBuilder()
52 .SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
53 .SetInitialUsage(nxt::BufferUsageBit::Mapped)
54 .SetSize(sizeof(vertexData))
55 .GetResult();
56 vertexBuffer.SetSubData(0, sizeof(vertexData) / sizeof(uint32_t),
57 reinterpret_cast<const uint32_t*>(vertexData));
58 vertexBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
59}
60
61void initTextures() {
62 texture = device.CreateTextureBuilder()
63 .SetDimension(nxt::TextureDimension::e2D)
64 .SetExtent(1024, 1024, 1)
65 .SetFormat(nxt::TextureFormat::R8G8B8A8Unorm)
66 .SetMipLevels(1)
67 .SetAllowedUsage(nxt::TextureUsageBit::TransferDst | nxt::TextureUsageBit::Sampled)
68 .GetResult();
69
70 sampler = device.CreateSamplerBuilder()
71 .SetFilterMode(nxt::FilterMode::Linear, nxt::FilterMode::Linear, nxt::FilterMode::Linear)
72 .GetResult();
73
74 // Initialize the texture with arbitrary data until we can load images
75 std::vector<uint8_t> data(4 * 1024 * 1024, 0);
76 for (size_t i = 0; i < data.size(); ++i) {
77 data[i] = i % 253;
78 }
79
80 nxt::Buffer stagingBuffer = device.CreateBufferBuilder()
81 .SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::TransferSrc)
82 .SetInitialUsage(nxt::BufferUsageBit::Mapped)
83 .SetSize(data.size())
84 .GetResult();
85 stagingBuffer.SetSubData(0, data.size() / sizeof(uint32_t), reinterpret_cast<uint32_t*>(data.data()));
86 stagingBuffer.FreezeUsage(nxt::BufferUsageBit::TransferSrc);
87
88 nxt::CommandBuffer copy = device.CreateCommandBufferBuilder()
89 .TransitionTextureUsage(texture, nxt::TextureUsageBit::TransferDst)
Corentin Wallez7f433a52017-05-09 16:57:52 +020090 .CopyBufferToTexture(stagingBuffer, 0, texture, 0, 0, 0, 1024, 1024, 1, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091 .GetResult();
92
93 queue.Submit(1, &copy);
94 texture.FreezeUsage(nxt::TextureUsageBit::Sampled);
95}
96
97void init() {
98 nxtProcTable procs;
99 GetProcTableAndDevice(&procs, &device);
100 nxtSetProcs(&procs);
101
102 queue = device.CreateQueueBuilder().GetResult();
103
104 initBuffers();
105 initTextures();
106
107 nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
108 #version 450
109 layout(location = 0) in vec4 pos;
110 void main() {
111 gl_Position = pos;
112 })"
113 );
114
115 nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
116 #version 450
117 layout(set = 0, binding = 0) uniform sampler mySampler;
118 layout(set = 0, binding = 1) uniform texture2D myTexture;
119
120 out vec4 fragColor;
121 void main() {
122 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
123 })"
124 );
125
126 auto inputState = device.CreateInputStateBuilder()
127 .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
128 .SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
129 .GetResult();
130
131 nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
132 .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler, 0, 1)
133 .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture, 1, 1)
134 .GetResult();
135
136 nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
137 .SetBindGroupLayout(0, bgl)
138 .GetResult();
139
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700140 CreateDefaultRenderPass(device, &renderpass, &framebuffer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400141 pipeline = device.CreatePipelineBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700142 .SetSubpass(renderpass, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400143 .SetLayout(pl)
144 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
145 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
146 .SetInputState(inputState)
147 .GetResult();
148
149 nxt::TextureView view = texture.CreateTextureViewBuilder().GetResult();
150
151 bindGroup = device.CreateBindGroupBuilder()
152 .SetLayout(bgl)
153 .SetUsage(nxt::BindGroupUsage::Frozen)
154 .SetSamplers(0, 1, &sampler)
155 .SetTextureViews(1, 1, &view)
156 .GetResult();
157}
158
159struct {uint32_t a; float b;} s;
160void frame() {
161 s.a = (s.a + 1) % 256;
162 s.b += 0.02;
163 if (s.b >= 1.0f) {s.b = 0.0f;}
164 static const uint32_t vertexBufferOffsets[1] = {0};
165 nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700166 .BeginRenderPass(renderpass, framebuffer)
167 .SetPipeline(pipeline)
168 .SetBindGroup(0, bindGroup)
169 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
170 .SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32)
171 .DrawElements(3, 1, 0, 0)
172 .EndRenderPass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400173 .GetResult();
174
175 queue.Submit(1, &commands);
176 SwapBuffers();
177}
178
179int main(int argc, const char* argv[]) {
180 if (!InitUtils(argc, argv)) {
181 return 1;
182 }
183 init();
184
185 while (!ShouldQuit()) {
186 frame();
187 usleep(16000);
188 }
189
190 // TODO release stuff
191}