Make CHelloTriangle in C++ to simplify Utils

The point of CHelloTriangle is to use the C version of NXT, so having
the code C++ is still ok.
This commit is contained in:
Corentin Wallez 2017-06-16 18:51:14 -04:00 committed by Corentin Wallez
parent 6aef6833b7
commit 931e6e82fd
4 changed files with 76 additions and 98 deletions

View File

@ -21,7 +21,7 @@ nxtRenderPass renderpass;
nxtFramebuffer framebuffer; nxtFramebuffer framebuffer;
void init() { void init() {
device = CreateNXTDevice(); device = CreateCppNXTDevice().Release();
{ {
nxtQueueBuilder builder = nxtDeviceCreateQueueBuilder(device); nxtQueueBuilder builder = nxtDeviceCreateQueueBuilder(device);
@ -35,7 +35,7 @@ void init() {
"void main() {\n" "void main() {\n"
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n" " gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
"}\n"; "}\n";
nxtShaderModule vsModule = CreateShaderModule(device, NXT_SHADER_STAGE_VERTEX, vs); nxtShaderModule vsModule = CreateShaderModule(nxt::Device(device), nxt::ShaderStage::Vertex, vs).Release();
const char* fs = const char* fs =
"#version 450\n" "#version 450\n"
@ -43,7 +43,7 @@ void init() {
"void main() {\n" "void main() {\n"
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n"; "}\n";
nxtShaderModule fsModule = CreateShaderModule(device, NXT_SHADER_STAGE_FRAGMENT, fs); nxtShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, fs).Release();
{ {
nxtRenderPassBuilder builder = nxtDeviceCreateRenderPassBuilder(device); nxtRenderPassBuilder builder = nxtDeviceCreateRenderPassBuilder(device);

View File

@ -34,8 +34,9 @@ add_library(utils STATIC ${UTILS_SOURCES})
target_link_libraries(utils nxt_backend nxt_wire shaderc nxtcpp nxt) target_link_libraries(utils nxt_backend nxt_wire shaderc nxtcpp nxt)
SetCXX14(utils) SetCXX14(utils)
add_executable(CHelloTriangle HelloTriangle.c) add_executable(CHelloTriangle CHelloTriangle.cpp)
target_link_libraries(CHelloTriangle utils) target_link_libraries(CHelloTriangle utils)
SetCXX14(CHelloTriangle)
add_executable(CppHelloTriangle HelloTriangle.cpp) add_executable(CppHelloTriangle HelloTriangle.cpp)
target_link_libraries(CppHelloTriangle utils) target_link_libraries(CppHelloTriangle utils)

View File

@ -271,85 +271,75 @@ nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* da
return buffer; return buffer;
} }
extern "C" { bool InitUtils(int argc, const char** argv) {
bool InitUtils(int argc, const char** argv) { for (int i = 0; i < argc; i++) {
for (int i = 0; i < argc; i++) { if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) { i++;
i++; if (i < argc && std::string("opengl") == argv[i]) {
if (i < argc && std::string("opengl") == argv[i]) { backendType = BackendType::OpenGL;
backendType = BackendType::OpenGL; continue;
continue;
}
if (i < argc && std::string("metal") == argv[i]) {
backendType = BackendType::Metal;
continue;
}
if (i < argc && std::string("d3d12") == argv[i]) {
backendType = BackendType::D3D12;
continue;
}
if (i < argc && std::string("null") == argv[i]) {
backendType = BackendType::Null;
continue;
}
fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null)\n");
return false;
} }
if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) { if (i < argc && std::string("metal") == argv[i]) {
i++; backendType = BackendType::Metal;
if (i < argc && std::string("none") == argv[i]) { continue;
cmdBufType = CmdBufType::None;
continue;
}
if (i < argc && std::string("terrible") == argv[i]) {
cmdBufType = CmdBufType::Terrible;
continue;
}
fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
return false;
} }
if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) { if (i < argc && std::string("d3d12") == argv[i]) {
printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]); backendType = BackendType::D3D12;
printf(" BACKEND is one of: opengl, metal, d3d12, null\n"); continue;
printf(" COMMAND_BUFFER is one of: none, terrible\n");
return false;
} }
if (i < argc && std::string("null") == argv[i]) {
backendType = BackendType::Null;
continue;
}
fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null)\n");
return false;
} }
return true; if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) {
} i++;
if (i < argc && std::string("none") == argv[i]) {
nxtDevice CreateNXTDevice() { cmdBufType = CmdBufType::None;
return CreateCppNXTDevice().Release(); continue;
} }
if (i < argc && std::string("terrible") == argv[i]) {
nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source) { cmdBufType = CmdBufType::Terrible;
return CreateShaderModule(device, static_cast<nxt::ShaderStage>(stage), source).Release(); continue;
} }
fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
void DoSwapBuffers() { return false;
if (cmdBufType == CmdBufType::Terrible) { }
c2sBuf->Flush(); if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
s2cBuf->Flush(); printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
printf(" BACKEND is one of: opengl, metal, d3d12, null\n");
printf(" COMMAND_BUFFER is one of: none, terrible\n");
return false;
} }
glfwPollEvents();
binding->SwapBuffers();
} }
return true;
}
void DoSwapBuffers() {
if (cmdBufType == CmdBufType::Terrible) {
c2sBuf->Flush();
s2cBuf->Flush();
}
glfwPollEvents();
binding->SwapBuffers();
}
#ifdef _WIN32 #ifdef _WIN32
void USleep(uint64_t usecs) { void USleep(uint64_t usecs) {
Sleep(usecs / 1000); Sleep(usecs / 1000);
} }
#else #else
void USleep(uint64_t usecs) { void USleep(uint64_t usecs) {
usleep(usecs); usleep(usecs);
} }
#endif #endif
bool ShouldQuit() { bool ShouldQuit() {
return glfwWindowShouldClose(window); return glfwWindowShouldClose(window);
} }
GLFWwindow* GetGLFWWindow() { GLFWwindow* GetGLFWWindow() {
return window; return window;
}
} }

View File

@ -12,30 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <nxt/nxt.h> #include <nxt/nxtcpp.h>
#if defined(__cplusplus) bool InitUtils(int argc, const char** argv);
extern "C" { void DoSwapBuffers();
#endif bool ShouldQuit();
bool InitUtils(int argc, const char** argv); void USleep(uint64_t usecs);
void DoSwapBuffers();
bool ShouldQuit();
void USleep(uint64_t usecs);
struct GLFWwindow; struct GLFWwindow;
struct GLFWwindow* GetGLFWWindow(); struct GLFWwindow* GetGLFWWindow();
#if defined(__cplusplus)
}
#endif
// Yuck nxt::Device CreateCppNXTDevice();
#if defined(__cplusplus) nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source);
#include <nxt/nxtcpp.h> void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer);
nxt::Device CreateCppNXTDevice(); nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage);
nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source);
void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer);
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage);
#else
nxtDevice CreateNXTDevice();
nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source);
#endif