blob: 75b32c075772527d2341e345d71b72573d15619e [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
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040017#include <vector>
18
19nxt::Device device;
20
21nxt::Buffer indexBuffer;
22nxt::Buffer vertexBuffer;
23
24nxt::Texture texture;
25nxt::Sampler sampler;
26
27nxt::Queue queue;
28nxt::Pipeline pipeline;
Kai Ninomiya68df8b02017-05-16 14:04:22 -070029nxt::RenderPass renderpass;
30nxt::Framebuffer framebuffer;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031nxt::BindGroup bindGroup;
32
33void initBuffers() {
34 static const uint32_t indexData[3] = {
35 0, 1, 2,
36 };
Corentin Wallezf45bdb82017-06-05 15:42:14 -040037 indexBuffer = CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::Index);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040038
39 static const float vertexData[12] = {
40 0.0f, 0.5f, 0.0f, 1.0f,
41 -0.5f, -0.5f, 0.0f, 1.0f,
42 0.5f, -0.5f, 0.0f, 1.0f,
43 };
Corentin Wallezf45bdb82017-06-05 15:42:14 -040044 vertexBuffer = CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040045}
46
47void initTextures() {
48 texture = device.CreateTextureBuilder()
49 .SetDimension(nxt::TextureDimension::e2D)
50 .SetExtent(1024, 1024, 1)
51 .SetFormat(nxt::TextureFormat::R8G8B8A8Unorm)
52 .SetMipLevels(1)
53 .SetAllowedUsage(nxt::TextureUsageBit::TransferDst | nxt::TextureUsageBit::Sampled)
54 .GetResult();
55
56 sampler = device.CreateSamplerBuilder()
57 .SetFilterMode(nxt::FilterMode::Linear, nxt::FilterMode::Linear, nxt::FilterMode::Linear)
58 .GetResult();
59
60 // Initialize the texture with arbitrary data until we can load images
61 std::vector<uint8_t> data(4 * 1024 * 1024, 0);
62 for (size_t i = 0; i < data.size(); ++i) {
63 data[i] = i % 253;
64 }
65
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040066
Corentin Wallezf45bdb82017-06-05 15:42:14 -040067 nxt::Buffer stagingBuffer = CreateFrozenBufferFromData(device, data.data(), data.size(), nxt::BufferUsageBit::TransferSrc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040068 nxt::CommandBuffer copy = device.CreateCommandBufferBuilder()
69 .TransitionTextureUsage(texture, nxt::TextureUsageBit::TransferDst)
Corentin Wallez7f433a52017-05-09 16:57:52 +020070 .CopyBufferToTexture(stagingBuffer, 0, texture, 0, 0, 0, 1024, 1024, 1, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040071 .GetResult();
72
73 queue.Submit(1, &copy);
74 texture.FreezeUsage(nxt::TextureUsageBit::Sampled);
75}
76
77void init() {
Corentin Wallez583e9a82017-05-29 11:30:29 -070078 device = CreateCppNXTDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040079
80 queue = device.CreateQueueBuilder().GetResult();
81
82 initBuffers();
83 initTextures();
84
85 nxt::ShaderModule vsModule = CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
86 #version 450
87 layout(location = 0) in vec4 pos;
88 void main() {
89 gl_Position = pos;
90 })"
91 );
92
93 nxt::ShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
94 #version 450
95 layout(set = 0, binding = 0) uniform sampler mySampler;
96 layout(set = 0, binding = 1) uniform texture2D myTexture;
97
98 out vec4 fragColor;
99 void main() {
100 fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
101 })"
102 );
103
104 auto inputState = device.CreateInputStateBuilder()
105 .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
106 .SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
107 .GetResult();
108
109 nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
110 .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler, 0, 1)
111 .SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture, 1, 1)
112 .GetResult();
113
114 nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
115 .SetBindGroupLayout(0, bgl)
116 .GetResult();
117
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700118 CreateDefaultRenderPass(device, &renderpass, &framebuffer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400119 pipeline = device.CreatePipelineBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700120 .SetSubpass(renderpass, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400121 .SetLayout(pl)
122 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
123 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
124 .SetInputState(inputState)
125 .GetResult();
126
127 nxt::TextureView view = texture.CreateTextureViewBuilder().GetResult();
128
129 bindGroup = device.CreateBindGroupBuilder()
130 .SetLayout(bgl)
131 .SetUsage(nxt::BindGroupUsage::Frozen)
132 .SetSamplers(0, 1, &sampler)
133 .SetTextureViews(1, 1, &view)
134 .GetResult();
135}
136
137struct {uint32_t a; float b;} s;
138void frame() {
139 s.a = (s.a + 1) % 256;
140 s.b += 0.02;
141 if (s.b >= 1.0f) {s.b = 0.0f;}
142 static const uint32_t vertexBufferOffsets[1] = {0};
143 nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700144 .BeginRenderPass(renderpass, framebuffer)
145 .SetPipeline(pipeline)
146 .SetBindGroup(0, bindGroup)
147 .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
148 .SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32)
149 .DrawElements(3, 1, 0, 0)
150 .EndRenderPass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400151 .GetResult();
152
153 queue.Submit(1, &commands);
Corentin Wallez583e9a82017-05-29 11:30:29 -0700154 DoSwapBuffers();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400155}
156
157int main(int argc, const char* argv[]) {
158 if (!InitUtils(argc, argv)) {
159 return 1;
160 }
161 init();
162
163 while (!ShouldQuit()) {
164 frame();
Corentin Wallez583e9a82017-05-29 11:30:29 -0700165 USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400166 }
167
168 // TODO release stuff
169}