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-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
nxtDevice device;
|
|
|
|
nxtQueue queue;
|
2017-07-28 01:30:57 +00:00
|
|
|
nxtSwapChain swapchain;
|
2017-07-14 15:32:57 +00:00
|
|
|
nxtRenderPipeline pipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-01-19 17:51:18 +00:00
|
|
|
nxtTextureFormat swapChainFormat;
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
void init() {
|
2017-06-16 22:51:14 +00:00
|
|
|
device = CreateCppNXTDevice().Release();
|
2018-06-15 00:26:27 +00:00
|
|
|
queue = nxtDeviceCreateQueue(device);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
|
|
|
nxtSwapChainBuilder builder = nxtDeviceCreateSwapChainBuilder(device);
|
|
|
|
uint64_t swapchainImpl = GetSwapChainImplementation();
|
|
|
|
nxtSwapChainBuilderSetImplementation(builder, swapchainImpl);
|
|
|
|
swapchain = nxtSwapChainBuilderGetResult(builder);
|
|
|
|
nxtSwapChainBuilderRelease(builder);
|
|
|
|
}
|
2018-01-19 17:51:18 +00:00
|
|
|
swapChainFormat = static_cast<nxtTextureFormat>(GetPreferredSwapChainTextureFormat());
|
|
|
|
nxtSwapChainConfigure(swapchain, swapChainFormat, NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
|
|
|
|
480);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
const char* vs =
|
|
|
|
"#version 450\n"
|
|
|
|
"const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
2017-06-19 17:09:41 +00:00
|
|
|
nxtShaderModule vsModule = utils::CreateShaderModule(nxt::Device(device), nxt::ShaderStage::Vertex, vs).Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
const char* fs =
|
|
|
|
"#version 450\n"
|
2017-08-29 17:37:45 +00:00
|
|
|
"layout(location = 0) out vec4 fragColor;"
|
2017-04-20 18:38:20 +00:00
|
|
|
"void main() {\n"
|
|
|
|
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
2017-06-19 17:09:41 +00:00
|
|
|
nxtShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, fs).Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
{
|
2017-07-14 15:32:57 +00:00
|
|
|
nxtRenderPipelineBuilder builder = nxtDeviceCreateRenderPipelineBuilder(device);
|
2018-05-02 22:10:13 +00:00
|
|
|
nxtRenderPipelineBuilderSetColorAttachmentFormat(builder, 0, swapChainFormat);
|
2017-07-14 15:32:57 +00:00
|
|
|
nxtRenderPipelineBuilderSetStage(builder, NXT_SHADER_STAGE_VERTEX, vsModule, "main");
|
|
|
|
nxtRenderPipelineBuilderSetStage(builder, NXT_SHADER_STAGE_FRAGMENT, fsModule, "main");
|
|
|
|
pipeline = nxtRenderPipelineBuilderGetResult(builder);
|
|
|
|
nxtRenderPipelineBuilderRelease(builder);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nxtShaderModuleRelease(vsModule);
|
|
|
|
nxtShaderModuleRelease(fsModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2017-07-28 01:30:57 +00:00
|
|
|
nxtTexture backbuffer = nxtSwapChainGetNextTexture(swapchain);
|
|
|
|
nxtTextureView backbufferView;
|
|
|
|
{
|
|
|
|
nxtTextureViewBuilder builder = nxtTextureCreateTextureViewBuilder(backbuffer);
|
|
|
|
backbufferView = nxtTextureViewBuilderGetResult(builder);
|
|
|
|
nxtTextureViewBuilderRelease(builder);
|
|
|
|
}
|
2018-05-11 17:04:44 +00:00
|
|
|
nxtRenderPassDescriptor renderpassInfo;
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2018-05-11 17:04:44 +00:00
|
|
|
nxtRenderPassDescriptorBuilder builder = nxtDeviceCreateRenderPassDescriptorBuilder(device);
|
|
|
|
nxtRenderPassDescriptorBuilderSetColorAttachment(builder, 0, backbufferView, NXT_LOAD_OP_CLEAR);
|
|
|
|
renderpassInfo = nxtRenderPassDescriptorBuilderGetResult(builder);
|
|
|
|
nxtRenderPassDescriptorBuilderRelease(builder);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
nxtCommandBuffer commands;
|
|
|
|
{
|
|
|
|
nxtCommandBufferBuilder builder = nxtDeviceCreateCommandBufferBuilder(device);
|
2018-05-02 22:10:13 +00:00
|
|
|
nxtCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
|
2017-07-14 15:32:57 +00:00
|
|
|
nxtCommandBufferBuilderSetRenderPipeline(builder, pipeline);
|
2017-04-20 18:38:20 +00:00
|
|
|
nxtCommandBufferBuilderDrawArrays(builder, 3, 1, 0, 0);
|
2017-05-16 21:04:22 +00:00
|
|
|
nxtCommandBufferBuilderEndRenderPass(builder);
|
2017-04-20 18:38:20 +00:00
|
|
|
commands = nxtCommandBufferBuilderGetResult(builder);
|
|
|
|
nxtCommandBufferBuilderRelease(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
nxtQueueSubmit(queue, 1, &commands);
|
|
|
|
nxtCommandBufferRelease(commands);
|
2017-07-28 01:30:57 +00:00
|
|
|
nxtTextureTransitionUsage(backbuffer, NXT_TEXTURE_USAGE_BIT_PRESENT);
|
|
|
|
nxtSwapChainPresent(swapchain, backbuffer);
|
2018-05-11 17:04:44 +00:00
|
|
|
nxtRenderPassDescriptorRelease(renderpassInfo);
|
2017-07-28 01:30:57 +00:00
|
|
|
nxtTextureViewRelease(backbufferView);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
DoFlush();
|
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-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|