From 81c72dc3c33f097d8acfaaa3834856fab7b72e3f Mon Sep 17 00:00:00 2001 From: Miguel Villa Floran Date: Fri, 18 Oct 2024 01:36:31 -0700 Subject: [PATCH] Cooking --- CMakeLists.txt | 11 +++++------ include/display.cuh | 10 ++++++++++ include/display.hpp | 14 ++++++++++++++ include/display.metal | 3 +++ src/display.cpp | 17 +++++++++++++---- src/display.cu | 14 ++++++++++++++ src/frame_buffer.cpp | 7 ++++--- src/main.cpp | 6 ------ 8 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 include/display.cuh create mode 100644 include/display.metal create mode 100644 src/display.cu diff --git a/CMakeLists.txt b/CMakeLists.txt index f030097..c9e53c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,9 +11,11 @@ include(CheckLanguage) check_language(CUDA) if(CMAKE_CUDA_COMPILER) enable_language(CUDA) - set(CUDA_FOUND TRUE) + find_package(CUDA REQUIRED) + include_directories(${CUDA_INCLUDE_DIRS}) set(CMAKE_CUDA_STANDARD 14) target_compile_definitions(${PROJECT_NAME} PRIVATE USE_CUDA) + set(CUDA_FOUND TRUE) message(STATUS "CUDA found. Building with CUDA support.") else() set(CUDA_FOUND FALSE) @@ -35,12 +37,9 @@ endif() # Adding our source files file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/src/*.c" "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/src/*.cc" "${CMAKE_CURRENT_LIST_DIR}/src/*.cxx" "${CMAKE_CURRENT_LIST_DIR}/src/*.cu") # Define PROJECT_SOURCES as a list of all source files -# Adding our include files -file(GLOB_RECURSE PROJECT_INCLUDE CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/include/*.h" "${CMAKE_CURRENT_LIST_DIR}/include/*.hpp" "${CMAKE_CURRENT_LIST_DIR}/include/*.hh" "${CMAKE_CURRENT_LIST_DIR}/include/*.hxx" "${CMAKE_CURRENT_LIST_DIR}/include/*.cuh") # Define PROJECT_INCLUDE to be the path to the include directory of the project - - target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCES}) -target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE}) +# Adding our include files +target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include) # Setting ASSETS_PATH target_compile_definitions(${PROJECT_NAME} PUBLIC ASSETS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/assets/") # Set the asset path macro to the absolute path on the dev machine diff --git a/include/display.cuh b/include/display.cuh new file mode 100644 index 0000000..e15d110 --- /dev/null +++ b/include/display.cuh @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include "../include/display.hpp" + + +namespace graphics { +__global__ void matrixMultiplyKernel(float *A, float *B, float *C, int Arows, int Acols, int Bcols); +} // namespace graphics \ No newline at end of file diff --git a/include/display.hpp b/include/display.hpp index 82ebcd2..7f038fb 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -13,12 +13,14 @@ namespace graphics { class Display { private: // Constants for display + SDL_DisplayMode displayMode; SDL_Window *window; SDL_Texture *texture; SDL_Surface *surface; SDL_Event *event; SDL_Renderer *renderer; std::unique_ptr frameBuffer; + uint32_t *d_frameBuffer; std::vector vertices; std::vector projectedVertices; @@ -48,5 +50,17 @@ class Display { // Method to check if the display should close bool shouldClose() const; + + // Method to initialize CUDA + virtual void InitalizeCuda(); + + // Method to free CUDA + virtual void FreeCuda(); + + // Method to initialize Metal + virtual void InitalizeMetal(); + + // Method to free Metal + virtual void FreeMetal(); }; } // namespace graphics diff --git a/include/display.metal b/include/display.metal new file mode 100644 index 0000000..76526a5 --- /dev/null +++ b/include/display.metal @@ -0,0 +1,3 @@ +#ifdef __APPLE__ +#include +#endif diff --git a/src/display.cpp b/src/display.cpp index 50d289f..36d63e4 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1,5 +1,12 @@ #include "../include/display.hpp" +#ifdef USE_CUDA +#include "../include/display.cuh" +#elif USE_METAL +#include "../include/display.metal" +#endif + + #include #include "../include/constants.hpp" @@ -21,14 +28,18 @@ Display::Display() { } // Query SDL for the display mode - SDL_DisplayMode displayMode; if (SDL_GetCurrentDisplayMode(0, &displayMode) != 0) { fprintf(stderr, "SDL_GetCurrentDisplayMode failed: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } // Initialize the frame buffer + #ifdef USE_CUDA + InitalizeCuda(); + InitalizeMetal(); + #else frameBuffer = std::make_unique(displayMode.w, displayMode.h); + #endif // Initialize the vertices // Start loading my array of vectors @@ -98,9 +109,7 @@ void Display::update() { int deltaTime = currentTime - prevTime; prevTime = currentTime; - if (deltaTime < FRAME_TIME) { - SDL_Delay(FRAME_TIME - deltaTime); - } else { + if (true) { for (int i = 0; i < vertices.size(); i++) { // Transform the vertices auto vertex = vertices[i]; diff --git a/src/display.cu b/src/display.cu new file mode 100644 index 0000000..9a20631 --- /dev/null +++ b/src/display.cu @@ -0,0 +1,14 @@ +#include "../include/display.cuh" + +#include +#include +#include "../include/display.hpp" + +namespace graphics { +void Display::InitalizeCuda() { + cudaMalloc((void**)&d_frameBuffer, displayMode.w * displayMode.h * sizeof(uint32_t)); +} +void Display::FreeCuda() { + cudaFree(d_frameBuffer); +} +} // namespace graphics diff --git a/src/frame_buffer.cpp b/src/frame_buffer.cpp index 8e1c210..3fcc3ea 100644 --- a/src/frame_buffer.cpp +++ b/src/frame_buffer.cpp @@ -1,4 +1,5 @@ #include "../include/frame_buffer.hpp" +#include // Add this at the top of the file namespace graphics { @@ -13,8 +14,8 @@ void FrameBuffer::drawPixel(int x, int y, const Color &color) { void FrameBuffer::drawLine(int x1, int y1, int x2, int y2, const Color &color) { // Bresenham's line algorithm - int dx = abs(x2 - x1); - int dy = abs(y2 - y1); + int dx = std::abs(x2 - x1); // Change abs to std::abs + int dy = std::abs(y2 - y1); int sx = (x1 < x2) ? 1 : -1; int sy = (y1 < y2) ? 1 : -1; int err = dx - dy; @@ -75,4 +76,4 @@ void FrameBuffer::clear(const Color &color) { uint32_t clearColor = static_cast(color); std::fill(buffer.begin(), buffer.end(), clearColor); } -} // namespace graphics \ No newline at end of file +} // namespace graphics diff --git a/src/main.cpp b/src/main.cpp index 9a702b7..c2ad9c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,7 @@ #include #include -#ifdef USE_CUDA -#include "../include/display.cuh" -#elif USE_METAL -#include "../include/display.metal" -#else #include "../include/display.hpp" -#endif #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1