From 583e9a8f3cdf1a9d734b8a8bbcb8944f39cfd333 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Mon, 29 May 2017 11:30:29 -0700 Subject: [PATCH] Avoid name conflicts between Util and Windows function GetWindow and SwapBuffers are function of Windows.h and using the same name causes a compile error. This commit also changes Util's GetProcTableAndDevice to CreateNXTDevice because we might need to call NXT procs when creating the C++ device. --- examples/Animometer.cpp | 9 ++---- examples/ComputeBoids.cpp | 9 ++---- examples/HelloCompute.cpp | 9 ++---- examples/HelloIndices.cpp | 9 ++---- examples/HelloInstancing.cpp | 9 ++---- examples/HelloTriangle.c | 10 ++----- examples/HelloTriangle.cpp | 9 ++---- examples/HelloUBO.cpp | 10 ++----- examples/HelloVertices.cpp | 9 ++---- examples/RenderToTexture.cpp | 9 ++---- examples/Utils.cpp | 46 +++++++++++++++++++++--------- examples/Utils.h | 9 +++--- examples/glTFViewer/glTFViewer.cpp | 11 +++---- 13 files changed, 71 insertions(+), 87 deletions(-) diff --git a/examples/Animometer.cpp b/examples/Animometer.cpp index 7a041f9111..2af862a4e5 100644 --- a/examples/Animometer.cpp +++ b/examples/Animometer.cpp @@ -16,7 +16,6 @@ #include #include -#include #include nxt::Device device; @@ -42,9 +41,7 @@ struct ShaderData { static std::vector shaderData; void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -150,7 +147,7 @@ void frame() { } queue.Submit(50, commands.data()); - SwapBuffers(); + DoSwapBuffers(); fprintf(stderr, "frame %i\n", f); } @@ -162,7 +159,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/ComputeBoids.cpp b/examples/ComputeBoids.cpp index b9a54a2566..c37d0f62c6 100644 --- a/examples/ComputeBoids.cpp +++ b/examples/ComputeBoids.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include @@ -296,9 +295,7 @@ void initCommandBuffers() { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -310,7 +307,7 @@ void init() { void frame() { queue.Submit(1, &commandBuffers[pingpong]); - SwapBuffers(); + DoSwapBuffers(); pingpong = (pingpong + 1) % 2; } @@ -323,7 +320,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloCompute.cpp b/examples/HelloCompute.cpp index ffb91b722a..f0b9024204 100644 --- a/examples/HelloCompute.cpp +++ b/examples/HelloCompute.cpp @@ -15,7 +15,6 @@ #include "Utils.h" #include -#include nxt::Device device; nxt::Queue queue; @@ -28,9 +27,7 @@ nxt::Pipeline computePipeline; nxt::BindGroup computeBindGroup; void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -142,7 +139,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -153,7 +150,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloIndices.cpp b/examples/HelloIndices.cpp index 628ca6f6af..f67cf80aad 100644 --- a/examples/HelloIndices.cpp +++ b/examples/HelloIndices.cpp @@ -14,7 +14,6 @@ #include "Utils.h" -#include #include nxt::Device device; @@ -55,9 +54,7 @@ void initBuffers() { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -105,7 +102,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -116,7 +113,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloInstancing.cpp b/examples/HelloInstancing.cpp index 45422cff92..2cb291fb0d 100644 --- a/examples/HelloInstancing.cpp +++ b/examples/HelloInstancing.cpp @@ -14,7 +14,6 @@ #include "Utils.h" -#include #include nxt::Device device; @@ -59,9 +58,7 @@ void initBuffers() { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -112,7 +109,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -123,7 +120,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloTriangle.c b/examples/HelloTriangle.c index 126405f524..b5edda1fe0 100644 --- a/examples/HelloTriangle.c +++ b/examples/HelloTriangle.c @@ -14,8 +14,6 @@ #include "Utils.h" -#include - nxtDevice device; nxtQueue queue; nxtPipeline pipeline; @@ -23,9 +21,7 @@ nxtRenderPass renderpass; nxtFramebuffer framebuffer; void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateNXTDevice(); { nxtQueueBuilder builder = nxtDeviceCreateQueueBuilder(device); @@ -93,7 +89,7 @@ void frame() { nxtQueueSubmit(queue, 1, &commands); nxtCommandBufferRelease(commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -104,7 +100,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloTriangle.cpp b/examples/HelloTriangle.cpp index ae1546939f..cce22b179c 100644 --- a/examples/HelloTriangle.cpp +++ b/examples/HelloTriangle.cpp @@ -14,7 +14,6 @@ #include "Utils.h" -#include #include nxt::Device device; @@ -95,9 +94,7 @@ void initTextures() { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -173,7 +170,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -184,7 +181,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloUBO.cpp b/examples/HelloUBO.cpp index dd64b13890..364dd339e5 100644 --- a/examples/HelloUBO.cpp +++ b/examples/HelloUBO.cpp @@ -14,8 +14,6 @@ #include "Utils.h" -#include - nxt::Device device; nxt::Queue queue; nxt::Pipeline pipeline; @@ -27,9 +25,7 @@ nxt::BindGroup bindGroup; struct {uint32_t a; float b;} s; void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -104,7 +100,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -115,7 +111,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/HelloVertices.cpp b/examples/HelloVertices.cpp index ebdb1b775d..00bac28d53 100644 --- a/examples/HelloVertices.cpp +++ b/examples/HelloVertices.cpp @@ -14,7 +14,6 @@ #include "Utils.h" -#include #include nxt::Device device; @@ -43,9 +42,7 @@ void initBuffers() { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -92,7 +89,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -103,7 +100,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/RenderToTexture.cpp b/examples/RenderToTexture.cpp index 86a4952708..d70f7eb912 100644 --- a/examples/RenderToTexture.cpp +++ b/examples/RenderToTexture.cpp @@ -14,7 +14,6 @@ #include "Utils.h" -#include #include nxt::Device device; @@ -183,9 +182,7 @@ void initPipelinePost() { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -217,7 +214,7 @@ void frame() { .GetResult(); queue.Submit(1, &commands); - SwapBuffers(); + DoSwapBuffers(); } int main(int argc, const char* argv[]) { @@ -228,7 +225,7 @@ int main(int argc, const char* argv[]) { while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff diff --git a/examples/Utils.cpp b/examples/Utils.cpp index 4ab6f8b39b..9d344a117f 100644 --- a/examples/Utils.cpp +++ b/examples/Utils.cpp @@ -25,6 +25,12 @@ #include #include +#ifdef _WIN32 + #include +#else + #include +#endif + BackendBinding* CreateMetalBinding(); namespace backend { @@ -104,7 +110,7 @@ static nxt::wire::CommandHandler* wireClient = nullptr; static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr; static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr; -void GetProcTableAndDevice(nxtProcTable* procs, nxt::Device* device) { +nxt::Device CreateCppNXTDevice() { switch (backendType) { case BackendType::OpenGL: binding = new OpenGLBinding; @@ -122,13 +128,13 @@ void GetProcTableAndDevice(nxtProcTable* procs, nxt::Device* device) { } if (!glfwInit()) { - return; + return nxt::Device(); } binding->SetupGLFWWindowHints(); window = glfwCreateWindow(640, 480, "NXT window", nullptr, nullptr); if (!window) { - return; + return nxt::Device(); } binding->SetWindow(window); @@ -137,10 +143,12 @@ void GetProcTableAndDevice(nxtProcTable* procs, nxt::Device* device) { nxtProcTable backendProcs; binding->GetProcAndDevice(&backendProcs, &backendDevice); + nxtDevice cDevice = nullptr; + nxtProcTable procs; switch (cmdBufType) { case CmdBufType::None: - *procs = backendProcs; - *device = nxt::Device::Acquire(backendDevice); + procs = backendProcs; + cDevice = backendDevice; break; case CmdBufType::Terrible: @@ -156,13 +164,15 @@ void GetProcTableAndDevice(nxtProcTable* procs, nxt::Device* device) { wireClient = nxt::wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf); s2cBuf->SetHandler(wireClient); - *procs = clientProcs; - *device = nxt::Device::Acquire(clientDevice); + procs = clientProcs; + cDevice = clientDevice; } break; } - procs->deviceSetErrorCallback(device->Get(), PrintDeviceError, 0); + nxtSetProcs(&procs); + procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0); + return nxt::Device::Acquire(cDevice); } nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source) { @@ -278,17 +288,15 @@ extern "C" { return true; } - void GetProcTableAndDevice(nxtProcTable* procs, nxtDevice* device) { - nxt::Device cppDevice; - GetProcTableAndDevice(procs, &cppDevice); - *device = cppDevice.Release(); + nxtDevice CreateNXTDevice() { + return CreateCppNXTDevice().Release(); } nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source) { return CreateShaderModule(device, static_cast(stage), source).Release(); } - void SwapBuffers() { + void DoSwapBuffers() { if (cmdBufType == CmdBufType::Terrible) { c2sBuf->Flush(); s2cBuf->Flush(); @@ -297,11 +305,21 @@ extern "C" { binding->SwapBuffers(); } +#ifdef _WIN32 + void USleep(uint64_t usecs) { + Sleep(usecs / 1000); + } +#else + void USleep(uint64_t usecs) { + usleep(usecs); + } +#endif + bool ShouldQuit() { return glfwWindowShouldClose(window); } - GLFWwindow* GetWindow() { + GLFWwindow* GetGLFWWindow() { return window; } } diff --git a/examples/Utils.h b/examples/Utils.h index bd0f6ac569..b49eb20613 100644 --- a/examples/Utils.h +++ b/examples/Utils.h @@ -18,11 +18,12 @@ extern "C" { #endif bool InitUtils(int argc, const char** argv); - void SwapBuffers(); + void DoSwapBuffers(); bool ShouldQuit(); + void USleep(uint64_t usecs); struct GLFWwindow; - struct GLFWwindow* GetWindow(); + struct GLFWwindow* GetGLFWWindow(); #if defined(__cplusplus) } #endif @@ -30,10 +31,10 @@ extern "C" { // Yuck #if defined(__cplusplus) #include - void GetProcTableAndDevice(nxtProcTable* procs, nxt::Device* device); + 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); #else - void GetProcTableAndDevice(nxtProcTable* procs, nxtDevice* device); + nxtDevice CreateNXTDevice(); nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source); #endif diff --git a/examples/glTFViewer/glTFViewer.cpp b/examples/glTFViewer/glTFViewer.cpp index 66c6ab86f0..5c26d42577 100644 --- a/examples/glTFViewer/glTFViewer.cpp +++ b/examples/glTFViewer/glTFViewer.cpp @@ -15,7 +15,6 @@ #include "Utils.h" #include -#include #define GLM_FORCE_DEPTH_ZERO_TO_ONE #include #include @@ -450,9 +449,7 @@ namespace { } void init() { - nxtProcTable procs; - GetProcTableAndDevice(&procs, &device); - nxtSetProcs(&procs); + device = CreateCppNXTDevice(); queue = device.CreateQueueBuilder().GetResult(); @@ -575,7 +572,7 @@ namespace { const auto& node = scene.nodes.at(n); drawNode(node); } - SwapBuffers(); + DoSwapBuffers(); } } @@ -640,14 +637,14 @@ int main(int argc, const char* argv[]) { init(); - GLFWwindow* window = GetWindow(); + GLFWwindow* window = GetGLFWWindow(); glfwSetMouseButtonCallback(window, mouseButtonCallback); glfwSetCursorPosCallback(window, cursorPosCallback); glfwSetScrollCallback(window, scrollCallback); while (!ShouldQuit()) { frame(); - usleep(16000); + USleep(16000); } // TODO release stuff