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.
diff --git a/examples/HelloTriangle.c b/examples/CHelloTriangle.cpp
similarity index 92%
rename from examples/HelloTriangle.c
rename to examples/CHelloTriangle.cpp
index b5edda1..479fe91 100644
--- a/examples/HelloTriangle.c
+++ b/examples/CHelloTriangle.cpp
@@ -21,7 +21,7 @@
nxtFramebuffer framebuffer;
void init() {
- device = CreateNXTDevice();
+ device = CreateCppNXTDevice().Release();
{
nxtQueueBuilder builder = nxtDeviceCreateQueueBuilder(device);
@@ -35,7 +35,7 @@
"void main() {\n"
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\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 =
"#version 450\n"
@@ -43,7 +43,7 @@
"void main() {\n"
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
- nxtShaderModule fsModule = CreateShaderModule(device, NXT_SHADER_STAGE_FRAGMENT, fs);
+ nxtShaderModule fsModule = CreateShaderModule(device, nxt::ShaderStage::Fragment, fs).Release();
{
nxtRenderPassBuilder builder = nxtDeviceCreateRenderPassBuilder(device);
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 431b0a2..1f84b40 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -34,8 +34,9 @@
target_link_libraries(utils nxt_backend nxt_wire shaderc nxtcpp nxt)
SetCXX14(utils)
-add_executable(CHelloTriangle HelloTriangle.c)
+add_executable(CHelloTriangle CHelloTriangle.cpp)
target_link_libraries(CHelloTriangle utils)
+SetCXX14(CHelloTriangle)
add_executable(CppHelloTriangle HelloTriangle.cpp)
target_link_libraries(CppHelloTriangle utils)
diff --git a/examples/Utils.cpp b/examples/Utils.cpp
index cf129b3..85ee678 100644
--- a/examples/Utils.cpp
+++ b/examples/Utils.cpp
@@ -271,85 +271,75 @@
return buffer;
}
-extern "C" {
- bool InitUtils(int argc, const char** argv) {
- for (int i = 0; i < argc; i++) {
- if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
- i++;
- if (i < argc && std::string("opengl") == argv[i]) {
- backendType = BackendType::OpenGL;
- 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;
+bool InitUtils(int argc, const char** argv) {
+ for (int i = 0; i < argc; i++) {
+ if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
+ i++;
+ if (i < argc && std::string("opengl") == argv[i]) {
+ backendType = BackendType::OpenGL;
+ continue;
}
- if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) {
- i++;
- if (i < argc && std::string("none") == argv[i]) {
- 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 (i < argc && std::string("metal") == argv[i]) {
+ backendType = BackendType::Metal;
+ continue;
}
- if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
- 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;
+ 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;
}
- return true;
- }
-
- nxtDevice CreateNXTDevice() {
- return CreateCppNXTDevice().Release();
- }
-
- nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source) {
- return CreateShaderModule(device, static_cast<nxt::ShaderStage>(stage), source).Release();
- }
-
- void DoSwapBuffers() {
- if (cmdBufType == CmdBufType::Terrible) {
- c2sBuf->Flush();
- s2cBuf->Flush();
+ if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) {
+ i++;
+ if (i < argc && std::string("none") == argv[i]) {
+ 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;
}
- glfwPollEvents();
- binding->SwapBuffers();
+ if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
+ 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;
+ }
}
+ return true;
+}
+
+void DoSwapBuffers() {
+ if (cmdBufType == CmdBufType::Terrible) {
+ c2sBuf->Flush();
+ s2cBuf->Flush();
+ }
+ glfwPollEvents();
+ binding->SwapBuffers();
+}
#ifdef _WIN32
- void USleep(uint64_t usecs) {
- Sleep(usecs / 1000);
- }
+void USleep(uint64_t usecs) {
+ Sleep(usecs / 1000);
+}
#else
- void USleep(uint64_t usecs) {
- usleep(usecs);
- }
+void USleep(uint64_t usecs) {
+ usleep(usecs);
+}
#endif
- bool ShouldQuit() {
- return glfwWindowShouldClose(window);
- }
+bool ShouldQuit() {
+ return glfwWindowShouldClose(window);
+}
- GLFWwindow* GetGLFWWindow() {
- return window;
- }
+GLFWwindow* GetGLFWWindow() {
+ return window;
}
diff --git a/examples/Utils.h b/examples/Utils.h
index e168837..a279567 100644
--- a/examples/Utils.h
+++ b/examples/Utils.h
@@ -12,30 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include <nxt/nxt.h>
+#include <nxt/nxtcpp.h>
-#if defined(__cplusplus)
-extern "C" {
-#endif
- bool InitUtils(int argc, const char** argv);
- void DoSwapBuffers();
- bool ShouldQuit();
- void USleep(uint64_t usecs);
+bool InitUtils(int argc, const char** argv);
+void DoSwapBuffers();
+bool ShouldQuit();
+void USleep(uint64_t usecs);
- struct GLFWwindow;
- struct GLFWwindow* GetGLFWWindow();
-#if defined(__cplusplus)
-}
-#endif
+struct GLFWwindow;
+struct GLFWwindow* GetGLFWWindow();
-// Yuck
-#if defined(__cplusplus)
- #include <nxt/nxtcpp.h>
- nxt::Device CreateCppNXTDevice();
- 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
+nxt::Device CreateCppNXTDevice();
+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);