Skip to content

Commit

Permalink
Cooking
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 18, 2024
1 parent 3b3f0a7 commit 81c72dc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 19 deletions.
11 changes: 5 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions include/display.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include "../include/display.hpp"


namespace graphics {
__global__ void matrixMultiplyKernel(float *A, float *B, float *C, int Arows, int Acols, int Bcols);
} // namespace graphics
14 changes: 14 additions & 0 deletions include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> frameBuffer;
uint32_t *d_frameBuffer;
std::vector<Vector3D> vertices;
std::vector<Vector2D> projectedVertices;

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions include/display.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ifdef __APPLE__
#include <Metal/Metal.h>
#endif
17 changes: 13 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "../include/display.hpp"

#ifdef USE_CUDA
#include "../include/display.cuh"
#elif USE_METAL
#include "../include/display.metal"
#endif


#include <SDL2/SDL.h>

#include "../include/constants.hpp"
Expand All @@ -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<FrameBuffer>(displayMode.w, displayMode.h);
#endif

// Initialize the vertices
// Start loading my array of vectors
Expand Down Expand Up @@ -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];
Expand Down
14 changes: 14 additions & 0 deletions src/display.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../include/display.cuh"

#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#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
7 changes: 4 additions & 3 deletions src/frame_buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/frame_buffer.hpp"
#include <cmath> // Add this at the top of the file

namespace graphics {

Expand All @@ -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;
Expand Down Expand Up @@ -75,4 +76,4 @@ void FrameBuffer::clear(const Color &color) {
uint32_t clearColor = static_cast<uint32_t>(color);
std::fill(buffer.begin(), buffer.end(), clearColor);
}
} // namespace graphics
} // namespace graphics
6 changes: 0 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#include <stddef.h>
#include <stdio.h>

#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
Expand Down

0 comments on commit 81c72dc

Please sign in to comment.