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;
void init() {
device = CreateNXTDevice();
device = CreateCppNXTDevice().Release();
{
nxtQueueBuilder builder = nxtDeviceCreateQueueBuilder(device);
@ -35,7 +35,7 @@ void init() {
"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 init() {
"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);

View File

@ -34,8 +34,9 @@ add_library(utils STATIC ${UTILS_SOURCES})
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)

View File

@ -271,85 +271,75 @@ nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* da
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;
}
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;
}
glfwPollEvents();
binding->SwapBuffers();
}
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);
}
GLFWwindow* GetGLFWWindow() {
return window;
}
bool ShouldQuit() {
return glfwWindowShouldClose(window);
}
GLFWwindow* GetGLFWWindow() {
return window;
}

View File

@ -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);