From 09ee7bf3fcd83275ddbcc086d2f049eef18b668b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Aragon=C3=A9s?= Date: Tue, 26 Dec 2023 20:21:39 +0100 Subject: [PATCH 1/2] Fix samples --- src/02-events/CMakeLists.txt | 88 +++ src/02-events/src/XMain.cpp | 3 +- src/03-open-save-alert/CMakeLists.txt | 88 +++ src/03-open-save-alert/src/XMain.cpp | 36 +- .../src/Renderer.h | 524 +++++++++--------- 5 files changed, 463 insertions(+), 276 deletions(-) create mode 100644 src/02-events/CMakeLists.txt create mode 100644 src/03-open-save-alert/CMakeLists.txt diff --git a/src/02-events/CMakeLists.txt b/src/02-events/CMakeLists.txt new file mode 100644 index 0000000..43a010f --- /dev/null +++ b/src/02-events/CMakeLists.txt @@ -0,0 +1,88 @@ +# Project Info + +cmake_minimum_required(VERSION 3.18 FATAL_ERROR) +cmake_policy(VERSION 3.18) +project(WindowEvents + VERSION 1.0.0.0 + LANGUAGES C CXX +) + +# ============================================================= + +# CMake Settings + +set_property(GLOBAL PROPERTY USE_FOLDERS ON) +set(CMAKE_SUPPRESS_REGENERATION true) +set(DCMAKE_GENERATOR_PLATFORM "x64") +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) +SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) +if(NOT CMAKE_DEBUG_POSTFIX) + set(CMAKE_DEBUG_POSTFIX d) +endif() + +# ============================================================= + +# Dependencies + +# CrossWindow +add_subdirectory(../../external/crosswindow ${CMAKE_BINARY_DIR}/crosswindow) +set_property(TARGET CrossWindow PROPERTY FOLDER "Dependencies") + +# ============================================================= + +# Sources + +file(GLOB_RECURSE FILE_SOURCES RELATIVE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/src/XMain.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/${XGFX_API}Renderer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/${XGFX_API}Renderer.mm + ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h +) + +# Solution Filters +foreach(source IN LISTS FILE_SOURCES) + get_filename_component(source_path "${source}" PATH) + string(REPLACE "/" "\\" source_path_msvc "${source_path}") + string(REPLACE "src" "" source_path_final "${source_path_msvc}") + source_group("${source_path_final}" FILES "${source}") +endforeach() + +# ============================================================= + +# Finalize App + +xwin_add_executable( + ${PROJECT_NAME} + "${FILE_SOURCES}" +) + +# ============================================================= + +# Finish Dependencies + +target_link_libraries( + ${PROJECT_NAME} + CrossWindow +) + +# ============================================================= + +# Finish Settings + +# Change output dir to bin +set_target_properties(${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +) +# Change working directory to bin +if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +endif() diff --git a/src/02-events/src/XMain.cpp b/src/02-events/src/XMain.cpp index 15b194e..bf9a78e 100644 --- a/src/02-events/src/XMain.cpp +++ b/src/02-events/src/XMain.cpp @@ -1,5 +1,5 @@ #include "CrossWindow/CrossWindow.h" -#include "Renderer.h" +//#include "Renderer.h" void xmain(int argc, const char** argv) { @@ -57,6 +57,7 @@ void xmain(int argc, const char** argv) if (event.type == xwin::EventType::Close) { window.close(); + isRunning = false; } eventQueue.pop(); diff --git a/src/03-open-save-alert/CMakeLists.txt b/src/03-open-save-alert/CMakeLists.txt new file mode 100644 index 0000000..ff6a670 --- /dev/null +++ b/src/03-open-save-alert/CMakeLists.txt @@ -0,0 +1,88 @@ +# Project Info + +cmake_minimum_required(VERSION 3.18 FATAL_ERROR) +cmake_policy(VERSION 3.18) +project(WindowOpenSaveAlert + VERSION 1.0.0.0 + LANGUAGES C CXX +) + +# ============================================================= + +# CMake Settings + +set_property(GLOBAL PROPERTY USE_FOLDERS ON) +set(CMAKE_SUPPRESS_REGENERATION true) +set(DCMAKE_GENERATOR_PLATFORM "x64") +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) +SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) +SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin) +if(NOT CMAKE_DEBUG_POSTFIX) + set(CMAKE_DEBUG_POSTFIX d) +endif() + +# ============================================================= + +# Dependencies + +# CrossWindow +add_subdirectory(../../external/crosswindow ${CMAKE_BINARY_DIR}/crosswindow) +set_property(TARGET CrossWindow PROPERTY FOLDER "Dependencies") + +# ============================================================= + +# Sources + +file(GLOB_RECURSE FILE_SOURCES RELATIVE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/src/XMain.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/${XGFX_API}Renderer.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/${XGFX_API}Renderer.mm + ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h +) + +# Solution Filters +foreach(source IN LISTS FILE_SOURCES) + get_filename_component(source_path "${source}" PATH) + string(REPLACE "/" "\\" source_path_msvc "${source_path}") + string(REPLACE "src" "" source_path_final "${source_path_msvc}") + source_group("${source_path_final}" FILES "${source}") +endforeach() + +# ============================================================= + +# Finalize App + +xwin_add_executable( + ${PROJECT_NAME} + "${FILE_SOURCES}" +) + +# ============================================================= + +# Finish Dependencies + +target_link_libraries( + ${PROJECT_NAME} + CrossWindow +) + +# ============================================================= + +# Finish Settings + +# Change output dir to bin +set_target_properties(${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +) +# Change working directory to bin +if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +endif() diff --git a/src/03-open-save-alert/src/XMain.cpp b/src/03-open-save-alert/src/XMain.cpp index ca5aa04..41da02d 100644 --- a/src/03-open-save-alert/src/XMain.cpp +++ b/src/03-open-save-alert/src/XMain.cpp @@ -1,5 +1,6 @@ #include "CrossWindow/CrossWindow.h" -#include "Renderer.h" +#include "CrossWindow/Common/Dialogs.h" +//#include "Renderer.h" void xmain(int argc, const char** argv) { @@ -35,20 +36,29 @@ void xmain(int argc, const char** argv) const xwin::MouseInputData data = event.data.mouseInput; if (data.state == xwin::ButtonState::Released) { - if (data.button == xwin::MouseInput::Left) - { - // 💾 Open File Save Dialog - } + if (data.button == xwin::MouseInput::Left) + { + // 💾 Open File Save Dialog + xwin::OpenSaveDialogDesc desc {}; + std::string path; + showOpenDialog(desc, path); + } - if (data.button == xwin::MouseInput::Right) - { - // 📂 Open File Open Dialog - } + if (data.button == xwin::MouseInput::Right) + { + // 📂 Open File Open Dialog + xwin::OpenSaveDialogDesc desc {}; + std::string path; + showSaveDialog(desc, path); + } - if (data.button == xwin::MouseInput::Middle) - { - // ❗ Open Alert - } + if (data.button == xwin::MouseInput::Middle) + { + // ❗ Open Alert + xwin::MessageDesc desc {}; + desc.type = xwin::MessageType::Warning; + showMessageBox(desc); + } } } diff --git a/src/04-cross-platform-hello-triangle/src/Renderer.h b/src/04-cross-platform-hello-triangle/src/Renderer.h index 3de5fc9..8d9bf75 100644 --- a/src/04-cross-platform-hello-triangle/src/Renderer.h +++ b/src/04-cross-platform-hello-triangle/src/Renderer.h @@ -19,41 +19,41 @@ // Common Utils inline std::vector readFile(const std::string& filename) { - std::string path = filename; - char pBuf[1024]; + std::string path = filename; + char pBuf[1024]; #ifdef XWIN_WIN32 - _getcwd(pBuf, 1024); - path = pBuf; - path += "\\"; + _getcwd(pBuf, 1024); + path = pBuf; + path += "\\"; #else - getcwd(pBuf, 1024); - path = pBuf; - path += "/"; + getcwd(pBuf, 1024); + path = pBuf; + path += "/"; #endif - path += filename; - std::ifstream file(path, std::ios::ate | std::ios::binary); - bool exists = (bool)file; + path += filename; + std::ifstream file(path, std::ios::ate | std::ios::binary); + bool exists = (bool)file; - if (!exists || !file.is_open()) { - throw std::runtime_error("failed to open file!"); - } + if (!exists || !file.is_open()) { + throw std::runtime_error("failed to open file!"); + } - size_t fileSize = (size_t)file.tellg(); - std::vector buffer(fileSize); + size_t fileSize = (size_t)file.tellg(); + std::vector buffer(fileSize); - file.seekg(0); - file.read(buffer.data(), fileSize); + file.seekg(0); + file.read(buffer.data(), fileSize); - file.close(); + file.close(); - return buffer; + return buffer; }; template inline T clamp(const T& value, const T& low, const T& high) { - return value < low ? low : (value > high ? high : value); + return value < low ? low : (value > high ? high : value); } @@ -62,293 +62,293 @@ inline T clamp(const T& value, const T& low, const T& high) class Renderer { public: - Renderer(xwin::Window& window); + Renderer(xwin::Window& window); - ~Renderer(); + ~Renderer(); - // Render onto the render target - void render(); + // Render onto the render target + void render(); - // Resize the window and internal data structures - void resize(unsigned width, unsigned height); + // Resize the window and internal data structures + void resize(unsigned width, unsigned height); protected: - // Initialize your Graphics API - void initializeAPI(xwin::Window& window); + // Initialize your Graphics API + void initializeAPI(xwin::Window& window); - // Destroy any Graphics API data structures used in this example - void destroyAPI(); + // Destroy any Graphics API data structures used in this example + void destroyAPI(); - // Initialize any resources such as VBOs, IBOs, used in this example - void initializeResources(); + // Initialize any resources such as VBOs, IBOs, used in this example + void initializeResources(); - // Destroy any resources used in this example - void destroyResources(); + // Destroy any resources used in this example + void destroyResources(); - // Create graphics API specific data structures to send commands to the GPU - void createCommands(); + // Create graphics API specific data structures to send commands to the GPU + void createCommands(); - // Set up commands used when rendering frame by this app - void setupCommands(); + // Set up commands used when rendering frame by this app + void setupCommands(); - // Destroy all commands - void destroyCommands(); + // Destroy all commands + void destroyCommands(); - // Set up the FrameBuffer - void initFrameBuffer(); + // Set up the FrameBuffer + void initFrameBuffer(); - void destroyFrameBuffer(); + void destroyFrameBuffer(); - // Set up the RenderPass - void createRenderPass(); + // Set up the RenderPass + void createRenderPass(); - void createSynchronization(); + void createSynchronization(); - // Set up the swapchain - void setupSwapchain(unsigned width, unsigned height); + // Set up the swapchain + void setupSwapchain(unsigned width, unsigned height); - struct Vertex - { - float position[3]; - float color[3]; - }; + struct Vertex + { + float position[3]; + float color[3]; + }; - Vertex mVertexBufferData[3] = - { - { { 1.0f, 1.0f, 0.0f },{ 1.0f, 0.0f, 0.0f } }, - { { -1.0f, 1.0f, 0.0f },{ 0.0f, 1.0f, 0.0f } }, - { { 0.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0f } } - }; + Vertex mVertexBufferData[3] = + { + { { 1.0f, 1.0f, 0.0f },{ 1.0f, 0.0f, 0.0f } }, + { { -1.0f, 1.0f, 0.0f },{ 0.0f, 1.0f, 0.0f } }, + { { 0.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0f } } + }; - uint32_t mIndexBufferData[3] = { 0, 1, 2 }; + uint32_t mIndexBufferData[3] = { 0, 1, 2 }; - std::chrono::time_point tStart, tEnd; - float mElapsedTime = 0.0f; + std::chrono::time_point tStart, tEnd; + float mElapsedTime = 0.0f; - // Uniform data - struct { - Matrix4 projectionMatrix; - Matrix4 modelMatrix; - Matrix4 viewMatrix; - } uboVS; + // Uniform data + struct { + Matrix4 projectionMatrix; + Matrix4 modelMatrix; + Matrix4 viewMatrix; + } uboVS; #if defined(XGFX_VULKAN) - // Initialization - vk::Instance mInstance; - vk::PhysicalDevice mPhysicalDevice; - vk::Device mDevice; - - vk::SwapchainKHR mSwapchain; - vk::SurfaceKHR mSurface; - - float mQueuePriority; - vk::Queue mQueue; - uint32_t mQueueFamilyIndex; - - vk::CommandPool mCommandPool; - std::vector mCommandBuffers; - uint32_t mCurrentBuffer; - - vk::Extent2D mSurfaceSize; - vk::Rect2D mRenderArea; - vk::Viewport mViewport; - - // Resources - vk::Format mSurfaceColorFormat; - vk::ColorSpaceKHR mSurfaceColorSpace; - vk::Format mSurfaceDepthFormat; - vk::Image mDepthImage; - vk::DeviceMemory mDepthImageMemory; - - vk::DescriptorPool mDescriptorPool; - std::vector mDescriptorSetLayouts; - std::vector mDescriptorSets; - - vk::ShaderModule mVertModule; - vk::ShaderModule mFragModule; - - vk::RenderPass mRenderPass; - - vk::Buffer mVertexBuffer; - vk::Buffer mIndexBuffer; - - vk::PipelineCache mPipelineCache; - vk::Pipeline mPipeline; - vk::PipelineLayout mPipelineLayout; - - // Sync - vk::Semaphore mPresentCompleteSemaphore; - vk::Semaphore mRenderCompleteSemaphore; - std::vector mWaitFences; - - // Swpachain - struct SwapChainBuffer { - vk::Image image; - std::array views; - vk::Framebuffer frameBuffer; - }; - - std::vector mSwapchainBuffers; - - // Vertex buffer and attributes - struct { - vk::DeviceMemory memory; // Handle to the device memory for this buffer - vk::Buffer buffer; // Handle to the Vulkan buffer object that the memory is bound to - vk::PipelineVertexInputStateCreateInfo inputState; - vk::VertexInputBindingDescription inputBinding; - std::vector inputAttributes; - } mVertices; - - // Index buffer - struct - { - vk::DeviceMemory memory; - vk::Buffer buffer; - uint32_t count; - } mIndices; - - // Uniform block object - struct { - vk::DeviceMemory memory; - vk::Buffer buffer; - vk::DescriptorBufferInfo descriptor; - } mUniformDataVS; + // Initialization + vk::Instance mInstance; + vk::PhysicalDevice mPhysicalDevice; + vk::Device mDevice; + + vk::SwapchainKHR mSwapchain; + vk::SurfaceKHR mSurface; + + float mQueuePriority; + vk::Queue mQueue; + uint32_t mQueueFamilyIndex; + + vk::CommandPool mCommandPool; + std::vector mCommandBuffers; + uint32_t mCurrentBuffer; + + vk::Extent2D mSurfaceSize; + vk::Rect2D mRenderArea; + vk::Viewport mViewport; + + // Resources + vk::Format mSurfaceColorFormat; + vk::ColorSpaceKHR mSurfaceColorSpace; + vk::Format mSurfaceDepthFormat; + vk::Image mDepthImage; + vk::DeviceMemory mDepthImageMemory; + + vk::DescriptorPool mDescriptorPool; + std::vector mDescriptorSetLayouts; + std::vector mDescriptorSets; + + vk::ShaderModule mVertModule; + vk::ShaderModule mFragModule; + + vk::RenderPass mRenderPass; + + vk::Buffer mVertexBuffer; + vk::Buffer mIndexBuffer; + + vk::PipelineCache mPipelineCache; + vk::Pipeline mPipeline; + vk::PipelineLayout mPipelineLayout; + + // Sync + vk::Semaphore mPresentCompleteSemaphore; + vk::Semaphore mRenderCompleteSemaphore; + std::vector mWaitFences; + + // Swpachain + struct SwapChainBuffer { + vk::Image image; + std::array views; + vk::Framebuffer frameBuffer; + }; + + std::vector mSwapchainBuffers; + + // Vertex buffer and attributes + struct { + vk::DeviceMemory memory; // Handle to the device memory for this buffer + vk::Buffer buffer; // Handle to the Vulkan buffer object that the memory is bound to + vk::PipelineVertexInputStateCreateInfo inputState; + vk::VertexInputBindingDescription inputBinding; + std::vector inputAttributes; + } mVertices; + + // Index buffer + struct + { + vk::DeviceMemory memory; + vk::Buffer buffer; + uint32_t count; + } mIndices; + + // Uniform block object + struct { + vk::DeviceMemory memory; + vk::Buffer buffer; + vk::DescriptorBufferInfo descriptor; + } mUniformDataVS; #elif defined(XGFX_DIRECTX12) - static const UINT backbufferCount = 2; + static const UINT backbufferCount = 2; - xwin::Window* mWindow; - unsigned mWidth, mHeight; + xwin::Window* mWindow; + unsigned mWidth, mHeight; - // Initialization - IDXGIFactory4* mFactory; - IDXGIAdapter1* mAdapter; + // Initialization + IDXGIFactory4* mFactory; + IDXGIAdapter1* mAdapter; #if defined(_DEBUG) - ID3D12Debug1* mDebugController; - ID3D12DebugDevice* mDebugDevice; + ID3D12Debug1* mDebugController; + ID3D12DebugDevice* mDebugDevice; #endif - ID3D12Device* mDevice; - ID3D12CommandQueue* mCommandQueue; - ID3D12CommandAllocator* mCommandAllocator; - ID3D12GraphicsCommandList* mCommandList; - - // Current Frame - UINT mCurrentBuffer; - ID3D12DescriptorHeap* mRtvHeap; - ID3D12Resource* mRenderTargets[backbufferCount]; - IDXGISwapChain3* mSwapchain; - - // Resources - D3D12_VIEWPORT mViewport; - D3D12_RECT mSurfaceSize; - - ID3D12Resource* mVertexBuffer; - ID3D12Resource* mIndexBuffer; - - ID3D12Resource* mUniformBuffer; - ID3D12DescriptorHeap* mUniformBufferHeap; - UINT8* mMappedUniformBuffer; - - D3D12_VERTEX_BUFFER_VIEW mVertexBufferView; - D3D12_INDEX_BUFFER_VIEW mIndexBufferView; - - UINT mRtvDescriptorSize; - ID3D12RootSignature* mRootSignature; - ID3D12PipelineState* mPipelineState; - - // Sync - UINT mFrameIndex; - HANDLE mFenceEvent; - ID3D12Fence* mFence; - UINT64 mFenceValue; + ID3D12Device* mDevice; + ID3D12CommandQueue* mCommandQueue; + ID3D12CommandAllocator* mCommandAllocator; + ID3D12GraphicsCommandList* mCommandList; + + // Current Frame + UINT mCurrentBuffer; + ID3D12DescriptorHeap* mRtvHeap; + ID3D12Resource* mRenderTargets[backbufferCount]; + IDXGISwapChain3* mSwapchain; + + // Resources + D3D12_VIEWPORT mViewport; + D3D12_RECT mSurfaceSize; + + ID3D12Resource* mVertexBuffer; + ID3D12Resource* mIndexBuffer; + + ID3D12Resource* mUniformBuffer; + ID3D12DescriptorHeap* mUniformBufferHeap; + UINT8* mMappedUniformBuffer; + + D3D12_VERTEX_BUFFER_VIEW mVertexBufferView; + D3D12_INDEX_BUFFER_VIEW mIndexBufferView; + + UINT mRtvDescriptorSize; + ID3D12RootSignature* mRootSignature; + ID3D12PipelineState* mPipelineState; + + // Sync + UINT mFrameIndex; + HANDLE mFenceEvent; + ID3D12Fence* mFence; + UINT64 mFenceValue; #elif defined(XGFX_DIRECTX11) - bool mVsync; - xwin::Window* mWindow; - unsigned mWidth, mHeight; - D3D11_VIEWPORT mViewport; + bool mVsync; + xwin::Window* mWindow; + unsigned mWidth, mHeight; + D3D11_VIEWPORT mViewport; - IDXGIFactory* mFactory; - IDXGIAdapter* mAdapter; + IDXGIFactory* mFactory; + IDXGIAdapter* mAdapter; #if defined(_DEBUG) - ID3D11Debug* mDebugController; + ID3D11Debug* mDebugController; #endif - IDXGIOutput* mAdapterOutput; - unsigned mNumerator, mDenominator; - ID3D11Device* mDevice; - ID3D11DeviceContext* mDeviceContext; - - IDXGISwapChain* mSwapchain; - ID3D11Texture2D* mBackbufferTex; - ID3D11Texture2D* mDepthStencilBuffer; - ID3D11DepthStencilView* mDepthStencilView; - ID3D11RenderTargetView* mRenderTargetView; - - ID3D11DepthStencilState* mDepthStencilState; - ID3D11RasterizerState* mRasterState; - - ID3D11Buffer *mVertexBuffer, *mIndexBuffer; - ID3D11InputLayout* mLayout; - ID3D11Buffer* mUniformBuffer; - ID3D11VertexShader* mVertexShader; - ID3D11PixelShader* mPixelShader; + IDXGIOutput* mAdapterOutput; + unsigned mNumerator, mDenominator; + ID3D11Device* mDevice; + ID3D11DeviceContext* mDeviceContext; + + IDXGISwapChain* mSwapchain; + ID3D11Texture2D* mBackbufferTex; + ID3D11Texture2D* mDepthStencilBuffer; + ID3D11DepthStencilView* mDepthStencilView; + ID3D11RenderTargetView* mRenderTargetView; + + ID3D11DepthStencilState* mDepthStencilState; + ID3D11RasterizerState* mRasterState; + + ID3D11Buffer *mVertexBuffer, *mIndexBuffer; + ID3D11InputLayout* mLayout; + ID3D11Buffer* mUniformBuffer; + ID3D11VertexShader* mVertexShader; + ID3D11PixelShader* mPixelShader; #elif defined(XGFX_OPENGL) - //Initialization - xgfx::OpenGLState mOGLState; + //Initialization + xgfx::OpenGLState mOGLState; - unsigned mWidth, mHeight; + unsigned mWidth, mHeight; - GLuint mFrameBuffer; - GLuint mFrameBufferTex; - GLuint mRenderBufferDepth; + GLuint mFrameBuffer; + GLuint mFrameBufferTex; + GLuint mRenderBufferDepth; - // Resources - GLuint mVertexShader; - GLuint mFragmentShader; - GLuint mProgram; - GLuint mVertexArray; - GLuint mVertexBuffer; - GLuint mIndexBuffer; + // Resources + GLuint mVertexShader; + GLuint mFragmentShader; + GLuint mProgram; + GLuint mVertexArray; + GLuint mVertexBuffer; + GLuint mIndexBuffer; - GLuint mUniformUBO; + GLuint mUniformUBO; - GLint mPositionAttrib; - GLint mColorAttrib; + GLint mPositionAttrib; + GLint mColorAttrib; #elif defined(XGFX_METAL) - // MTLDevice - The device (aka GPU) we're using to render - void* mDevice; - //CAMetalLayer - void* mLayer; - //MTLCommandQueue - The command Queue from which we'll obtain command buffers - void* mCommandQueue; - // The current size of our view so we can use this in our render pipeline - unsigned mViewportSize[2]; - - //Resources - - //MTLLibrary - void* vertLibrary; - //MTLLibrary - void* fragLibrary; - //MTLFunction - void* vertexFunction; - //MTLFunction - void* fragmentFunction; - //MTLBuffer - void* mVertexBuffer; - //MTLBuffer - void* mIndexBuffer; - //MTLBuffer - void* mUniformBuffer; - //MTLRenderPipelineState - void* mPipelineState; - //MTLCommandBuffer - void* mCommandBuffer; + // MTLDevice - The device (aka GPU) we're using to render + void* mDevice; + //CAMetalLayer + void* mLayer; + //MTLCommandQueue - The command Queue from which we'll obtain command buffers + void* mCommandQueue; + // The current size of our view so we can use this in our render pipeline + unsigned mViewportSize[2]; + + //Resources + + //MTLLibrary + void* vertLibrary; + //MTLLibrary + void* fragLibrary; + //MTLFunction + void* vertexFunction; + //MTLFunction + void* fragmentFunction; + //MTLBuffer + void* mVertexBuffer; + //MTLBuffer + void* mIndexBuffer; + //MTLBuffer + void* mUniformBuffer; + //MTLRenderPipelineState + void* mPipelineState; + //MTLCommandBuffer + void* mCommandBuffer; #endif }; From 23ab849ec057255e7ab21cda5cac5b522f7d5d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Aragon=C3=A9s?= Date: Tue, 26 Dec 2023 20:26:34 +0100 Subject: [PATCH 2/2] Restore spaces like original --- .../src/Renderer.h | 524 +++++++++--------- 1 file changed, 262 insertions(+), 262 deletions(-) diff --git a/src/04-cross-platform-hello-triangle/src/Renderer.h b/src/04-cross-platform-hello-triangle/src/Renderer.h index 8d9bf75..d7f94ec 100644 --- a/src/04-cross-platform-hello-triangle/src/Renderer.h +++ b/src/04-cross-platform-hello-triangle/src/Renderer.h @@ -19,41 +19,41 @@ // Common Utils inline std::vector readFile(const std::string& filename) { - std::string path = filename; - char pBuf[1024]; + std::string path = filename; + char pBuf[1024]; #ifdef XWIN_WIN32 - _getcwd(pBuf, 1024); - path = pBuf; - path += "\\"; + _getcwd(pBuf, 1024); + path = pBuf; + path += "\\"; #else - getcwd(pBuf, 1024); - path = pBuf; - path += "/"; + getcwd(pBuf, 1024); + path = pBuf; + path += "/"; #endif - path += filename; - std::ifstream file(path, std::ios::ate | std::ios::binary); - bool exists = (bool)file; + path += filename; + std::ifstream file(path, std::ios::ate | std::ios::binary); + bool exists = (bool)file; - if (!exists || !file.is_open()) { - throw std::runtime_error("failed to open file!"); - } + if (!exists || !file.is_open()) { + throw std::runtime_error("failed to open file!"); + } - size_t fileSize = (size_t)file.tellg(); - std::vector buffer(fileSize); + size_t fileSize = (size_t)file.tellg(); + std::vector buffer(fileSize); - file.seekg(0); - file.read(buffer.data(), fileSize); + file.seekg(0); + file.read(buffer.data(), fileSize); - file.close(); + file.close(); - return buffer; + return buffer; }; template inline T clamp(const T& value, const T& low, const T& high) { - return value < low ? low : (value > high ? high : value); + return value < low ? low : (value > high ? high : value); } @@ -62,293 +62,293 @@ inline T clamp(const T& value, const T& low, const T& high) class Renderer { public: - Renderer(xwin::Window& window); + Renderer(xwin::Window& window); - ~Renderer(); + ~Renderer(); - // Render onto the render target - void render(); + // Render onto the render target + void render(); - // Resize the window and internal data structures - void resize(unsigned width, unsigned height); + // Resize the window and internal data structures + void resize(unsigned width, unsigned height); protected: - // Initialize your Graphics API - void initializeAPI(xwin::Window& window); + // Initialize your Graphics API + void initializeAPI(xwin::Window& window); - // Destroy any Graphics API data structures used in this example - void destroyAPI(); + // Destroy any Graphics API data structures used in this example + void destroyAPI(); - // Initialize any resources such as VBOs, IBOs, used in this example - void initializeResources(); + // Initialize any resources such as VBOs, IBOs, used in this example + void initializeResources(); - // Destroy any resources used in this example - void destroyResources(); + // Destroy any resources used in this example + void destroyResources(); - // Create graphics API specific data structures to send commands to the GPU - void createCommands(); + // Create graphics API specific data structures to send commands to the GPU + void createCommands(); - // Set up commands used when rendering frame by this app - void setupCommands(); + // Set up commands used when rendering frame by this app + void setupCommands(); - // Destroy all commands - void destroyCommands(); + // Destroy all commands + void destroyCommands(); - // Set up the FrameBuffer - void initFrameBuffer(); + // Set up the FrameBuffer + void initFrameBuffer(); - void destroyFrameBuffer(); + void destroyFrameBuffer(); - // Set up the RenderPass - void createRenderPass(); + // Set up the RenderPass + void createRenderPass(); - void createSynchronization(); + void createSynchronization(); - // Set up the swapchain - void setupSwapchain(unsigned width, unsigned height); + // Set up the swapchain + void setupSwapchain(unsigned width, unsigned height); - struct Vertex - { - float position[3]; - float color[3]; - }; + struct Vertex + { + float position[3]; + float color[3]; + }; - Vertex mVertexBufferData[3] = - { - { { 1.0f, 1.0f, 0.0f },{ 1.0f, 0.0f, 0.0f } }, - { { -1.0f, 1.0f, 0.0f },{ 0.0f, 1.0f, 0.0f } }, - { { 0.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0f } } - }; + Vertex mVertexBufferData[3] = + { + { { 1.0f, 1.0f, 0.0f },{ 1.0f, 0.0f, 0.0f } }, + { { -1.0f, 1.0f, 0.0f },{ 0.0f, 1.0f, 0.0f } }, + { { 0.0f, -1.0f, 0.0f },{ 0.0f, 0.0f, 1.0f } } + }; - uint32_t mIndexBufferData[3] = { 0, 1, 2 }; + uint32_t mIndexBufferData[3] = { 0, 1, 2 }; std::chrono::time_point tStart, tEnd; - float mElapsedTime = 0.0f; + float mElapsedTime = 0.0f; - // Uniform data - struct { - Matrix4 projectionMatrix; - Matrix4 modelMatrix; - Matrix4 viewMatrix; - } uboVS; + // Uniform data + struct { + Matrix4 projectionMatrix; + Matrix4 modelMatrix; + Matrix4 viewMatrix; + } uboVS; #if defined(XGFX_VULKAN) - // Initialization - vk::Instance mInstance; - vk::PhysicalDevice mPhysicalDevice; - vk::Device mDevice; - - vk::SwapchainKHR mSwapchain; - vk::SurfaceKHR mSurface; - - float mQueuePriority; - vk::Queue mQueue; - uint32_t mQueueFamilyIndex; - - vk::CommandPool mCommandPool; - std::vector mCommandBuffers; - uint32_t mCurrentBuffer; - - vk::Extent2D mSurfaceSize; - vk::Rect2D mRenderArea; - vk::Viewport mViewport; - - // Resources - vk::Format mSurfaceColorFormat; - vk::ColorSpaceKHR mSurfaceColorSpace; - vk::Format mSurfaceDepthFormat; - vk::Image mDepthImage; - vk::DeviceMemory mDepthImageMemory; - - vk::DescriptorPool mDescriptorPool; - std::vector mDescriptorSetLayouts; - std::vector mDescriptorSets; - - vk::ShaderModule mVertModule; - vk::ShaderModule mFragModule; - - vk::RenderPass mRenderPass; - - vk::Buffer mVertexBuffer; - vk::Buffer mIndexBuffer; - - vk::PipelineCache mPipelineCache; - vk::Pipeline mPipeline; - vk::PipelineLayout mPipelineLayout; - - // Sync - vk::Semaphore mPresentCompleteSemaphore; - vk::Semaphore mRenderCompleteSemaphore; - std::vector mWaitFences; - - // Swpachain - struct SwapChainBuffer { - vk::Image image; - std::array views; - vk::Framebuffer frameBuffer; - }; - - std::vector mSwapchainBuffers; - - // Vertex buffer and attributes - struct { - vk::DeviceMemory memory; // Handle to the device memory for this buffer - vk::Buffer buffer; // Handle to the Vulkan buffer object that the memory is bound to - vk::PipelineVertexInputStateCreateInfo inputState; - vk::VertexInputBindingDescription inputBinding; - std::vector inputAttributes; - } mVertices; - - // Index buffer - struct - { - vk::DeviceMemory memory; - vk::Buffer buffer; - uint32_t count; - } mIndices; - - // Uniform block object - struct { - vk::DeviceMemory memory; - vk::Buffer buffer; - vk::DescriptorBufferInfo descriptor; - } mUniformDataVS; + // Initialization + vk::Instance mInstance; + vk::PhysicalDevice mPhysicalDevice; + vk::Device mDevice; + + vk::SwapchainKHR mSwapchain; + vk::SurfaceKHR mSurface; + + float mQueuePriority; + vk::Queue mQueue; + uint32_t mQueueFamilyIndex; + + vk::CommandPool mCommandPool; + std::vector mCommandBuffers; + uint32_t mCurrentBuffer; + + vk::Extent2D mSurfaceSize; + vk::Rect2D mRenderArea; + vk::Viewport mViewport; + + // Resources + vk::Format mSurfaceColorFormat; + vk::ColorSpaceKHR mSurfaceColorSpace; + vk::Format mSurfaceDepthFormat; + vk::Image mDepthImage; + vk::DeviceMemory mDepthImageMemory; + + vk::DescriptorPool mDescriptorPool; + std::vector mDescriptorSetLayouts; + std::vector mDescriptorSets; + + vk::ShaderModule mVertModule; + vk::ShaderModule mFragModule; + + vk::RenderPass mRenderPass; + + vk::Buffer mVertexBuffer; + vk::Buffer mIndexBuffer; + + vk::PipelineCache mPipelineCache; + vk::Pipeline mPipeline; + vk::PipelineLayout mPipelineLayout; + + // Sync + vk::Semaphore mPresentCompleteSemaphore; + vk::Semaphore mRenderCompleteSemaphore; + std::vector mWaitFences; + + // Swpachain + struct SwapChainBuffer { + vk::Image image; + std::array views; + vk::Framebuffer frameBuffer; + }; + + std::vector mSwapchainBuffers; + + // Vertex buffer and attributes + struct { + vk::DeviceMemory memory; // Handle to the device memory for this buffer + vk::Buffer buffer; // Handle to the Vulkan buffer object that the memory is bound to + vk::PipelineVertexInputStateCreateInfo inputState; + vk::VertexInputBindingDescription inputBinding; + std::vector inputAttributes; + } mVertices; + + // Index buffer + struct + { + vk::DeviceMemory memory; + vk::Buffer buffer; + uint32_t count; + } mIndices; + + // Uniform block object + struct { + vk::DeviceMemory memory; + vk::Buffer buffer; + vk::DescriptorBufferInfo descriptor; + } mUniformDataVS; #elif defined(XGFX_DIRECTX12) - static const UINT backbufferCount = 2; + static const UINT backbufferCount = 2; - xwin::Window* mWindow; - unsigned mWidth, mHeight; + xwin::Window* mWindow; + unsigned mWidth, mHeight; - // Initialization - IDXGIFactory4* mFactory; - IDXGIAdapter1* mAdapter; + // Initialization + IDXGIFactory4* mFactory; + IDXGIAdapter1* mAdapter; #if defined(_DEBUG) - ID3D12Debug1* mDebugController; - ID3D12DebugDevice* mDebugDevice; + ID3D12Debug1* mDebugController; + ID3D12DebugDevice* mDebugDevice; #endif - ID3D12Device* mDevice; - ID3D12CommandQueue* mCommandQueue; - ID3D12CommandAllocator* mCommandAllocator; - ID3D12GraphicsCommandList* mCommandList; - - // Current Frame - UINT mCurrentBuffer; - ID3D12DescriptorHeap* mRtvHeap; - ID3D12Resource* mRenderTargets[backbufferCount]; - IDXGISwapChain3* mSwapchain; - - // Resources - D3D12_VIEWPORT mViewport; - D3D12_RECT mSurfaceSize; - - ID3D12Resource* mVertexBuffer; - ID3D12Resource* mIndexBuffer; - - ID3D12Resource* mUniformBuffer; - ID3D12DescriptorHeap* mUniformBufferHeap; - UINT8* mMappedUniformBuffer; - - D3D12_VERTEX_BUFFER_VIEW mVertexBufferView; - D3D12_INDEX_BUFFER_VIEW mIndexBufferView; - - UINT mRtvDescriptorSize; - ID3D12RootSignature* mRootSignature; - ID3D12PipelineState* mPipelineState; - - // Sync - UINT mFrameIndex; - HANDLE mFenceEvent; - ID3D12Fence* mFence; - UINT64 mFenceValue; + ID3D12Device* mDevice; + ID3D12CommandQueue* mCommandQueue; + ID3D12CommandAllocator* mCommandAllocator; + ID3D12GraphicsCommandList* mCommandList; + + // Current Frame + UINT mCurrentBuffer; + ID3D12DescriptorHeap* mRtvHeap; + ID3D12Resource* mRenderTargets[backbufferCount]; + IDXGISwapChain3* mSwapchain; + + // Resources + D3D12_VIEWPORT mViewport; + D3D12_RECT mSurfaceSize; + + ID3D12Resource* mVertexBuffer; + ID3D12Resource* mIndexBuffer; + + ID3D12Resource* mUniformBuffer; + ID3D12DescriptorHeap* mUniformBufferHeap; + UINT8* mMappedUniformBuffer; + + D3D12_VERTEX_BUFFER_VIEW mVertexBufferView; + D3D12_INDEX_BUFFER_VIEW mIndexBufferView; + + UINT mRtvDescriptorSize; + ID3D12RootSignature* mRootSignature; + ID3D12PipelineState* mPipelineState; + + // Sync + UINT mFrameIndex; + HANDLE mFenceEvent; + ID3D12Fence* mFence; + UINT64 mFenceValue; #elif defined(XGFX_DIRECTX11) - bool mVsync; - xwin::Window* mWindow; - unsigned mWidth, mHeight; - D3D11_VIEWPORT mViewport; + bool mVsync; + xwin::Window* mWindow; + unsigned mWidth, mHeight; + D3D11_VIEWPORT mViewport; - IDXGIFactory* mFactory; - IDXGIAdapter* mAdapter; + IDXGIFactory* mFactory; + IDXGIAdapter* mAdapter; #if defined(_DEBUG) - ID3D11Debug* mDebugController; + ID3D11Debug* mDebugController; #endif - IDXGIOutput* mAdapterOutput; - unsigned mNumerator, mDenominator; - ID3D11Device* mDevice; - ID3D11DeviceContext* mDeviceContext; - - IDXGISwapChain* mSwapchain; - ID3D11Texture2D* mBackbufferTex; - ID3D11Texture2D* mDepthStencilBuffer; - ID3D11DepthStencilView* mDepthStencilView; - ID3D11RenderTargetView* mRenderTargetView; - - ID3D11DepthStencilState* mDepthStencilState; - ID3D11RasterizerState* mRasterState; - - ID3D11Buffer *mVertexBuffer, *mIndexBuffer; - ID3D11InputLayout* mLayout; - ID3D11Buffer* mUniformBuffer; - ID3D11VertexShader* mVertexShader; - ID3D11PixelShader* mPixelShader; + IDXGIOutput* mAdapterOutput; + unsigned mNumerator, mDenominator; + ID3D11Device* mDevice; + ID3D11DeviceContext* mDeviceContext; + + IDXGISwapChain* mSwapchain; + ID3D11Texture2D* mBackbufferTex; + ID3D11Texture2D* mDepthStencilBuffer; + ID3D11DepthStencilView* mDepthStencilView; + ID3D11RenderTargetView* mRenderTargetView; + + ID3D11DepthStencilState* mDepthStencilState; + ID3D11RasterizerState* mRasterState; + + ID3D11Buffer *mVertexBuffer, *mIndexBuffer; + ID3D11InputLayout* mLayout; + ID3D11Buffer* mUniformBuffer; + ID3D11VertexShader* mVertexShader; + ID3D11PixelShader* mPixelShader; #elif defined(XGFX_OPENGL) - //Initialization - xgfx::OpenGLState mOGLState; + //Initialization + xgfx::OpenGLState mOGLState; - unsigned mWidth, mHeight; + unsigned mWidth, mHeight; - GLuint mFrameBuffer; - GLuint mFrameBufferTex; - GLuint mRenderBufferDepth; + GLuint mFrameBuffer; + GLuint mFrameBufferTex; + GLuint mRenderBufferDepth; - // Resources - GLuint mVertexShader; - GLuint mFragmentShader; - GLuint mProgram; - GLuint mVertexArray; - GLuint mVertexBuffer; - GLuint mIndexBuffer; + // Resources + GLuint mVertexShader; + GLuint mFragmentShader; + GLuint mProgram; + GLuint mVertexArray; + GLuint mVertexBuffer; + GLuint mIndexBuffer; - GLuint mUniformUBO; + GLuint mUniformUBO; - GLint mPositionAttrib; - GLint mColorAttrib; + GLint mPositionAttrib; + GLint mColorAttrib; #elif defined(XGFX_METAL) - // MTLDevice - The device (aka GPU) we're using to render - void* mDevice; - //CAMetalLayer - void* mLayer; - //MTLCommandQueue - The command Queue from which we'll obtain command buffers - void* mCommandQueue; - // The current size of our view so we can use this in our render pipeline - unsigned mViewportSize[2]; - - //Resources - - //MTLLibrary - void* vertLibrary; - //MTLLibrary - void* fragLibrary; - //MTLFunction - void* vertexFunction; - //MTLFunction - void* fragmentFunction; - //MTLBuffer - void* mVertexBuffer; - //MTLBuffer - void* mIndexBuffer; - //MTLBuffer - void* mUniformBuffer; - //MTLRenderPipelineState - void* mPipelineState; - //MTLCommandBuffer - void* mCommandBuffer; + // MTLDevice - The device (aka GPU) we're using to render + void* mDevice; + //CAMetalLayer + void* mLayer; + //MTLCommandQueue - The command Queue from which we'll obtain command buffers + void* mCommandQueue; + // The current size of our view so we can use this in our render pipeline + unsigned mViewportSize[2]; + + //Resources + + //MTLLibrary + void* vertLibrary; + //MTLLibrary + void* fragLibrary; + //MTLFunction + void* vertexFunction; + //MTLFunction + void* fragmentFunction; + //MTLBuffer + void* mVertexBuffer; + //MTLBuffer + void* mIndexBuffer; + //MTLBuffer + void* mUniformBuffer; + //MTLRenderPipelineState + void* mPipelineState; + //MTLCommandBuffer + void* mCommandBuffer; #endif -}; +}; \ No newline at end of file