2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
#include "SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
#include "utils/NXTHelpers.h"
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
nxt::Device device;
|
|
|
|
|
|
|
|
nxt::Buffer indexBuffer;
|
|
|
|
nxt::Buffer vertexBuffer;
|
|
|
|
|
|
|
|
nxt::Queue queue;
|
|
|
|
nxt::Pipeline pipeline;
|
2017-05-16 21:04:22 +00:00
|
|
|
nxt::RenderPass renderpass;
|
|
|
|
nxt::Framebuffer framebuffer;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
void initBuffers() {
|
|
|
|
static const uint32_t indexData[3] = {
|
|
|
|
0, 1, 2,
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::Index);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
static const float vertexData[12] = {
|
|
|
|
0.0f, 0.5f, 0.0f, 1.0f,
|
|
|
|
-0.5f, -0.5f, 0.0f, 1.0f,
|
|
|
|
0.5f, -0.5f, 0.0f, 1.0f,
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
2017-05-29 18:30:29 +00:00
|
|
|
device = CreateCppNXTDevice();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
queue = device.CreateQueueBuilder().GetResult();
|
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 0) in vec4 pos;
|
|
|
|
void main() {
|
|
|
|
gl_Position = pos;
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
out vec4 fragColor;
|
|
|
|
void main() {
|
|
|
|
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
|
|
|
auto inputState = device.CreateInputStateBuilder()
|
|
|
|
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
|
|
|
|
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
|
|
|
|
.GetResult();
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
utils::CreateDefaultRenderPass(device, &renderpass, &framebuffer);
|
2017-04-20 18:38:20 +00:00
|
|
|
pipeline = device.CreatePipelineBuilder()
|
2017-05-16 21:04:22 +00:00
|
|
|
.SetSubpass(renderpass, 0)
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
|
|
|
.SetInputState(inputState)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
|
|
|
static const uint32_t vertexBufferOffsets[1] = {0};
|
|
|
|
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
2017-05-16 21:04:22 +00:00
|
|
|
.BeginRenderPass(renderpass, framebuffer)
|
|
|
|
.SetPipeline(pipeline)
|
|
|
|
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
|
|
|
|
.SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32)
|
|
|
|
.DrawElements(3, 1, 0, 0)
|
|
|
|
.EndRenderPass()
|
2017-04-20 18:38:20 +00:00
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
queue.Submit(1, &commands);
|
2017-05-29 18:30:29 +00:00
|
|
|
DoSwapBuffers();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
|
|
|
frame();
|
2017-05-29 18:30:29 +00:00
|
|
|
USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|