Skip to content

Commit

Permalink
Add benchmarking capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 19, 2024
1 parent 6b63c84 commit cfe229a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"*.metal": "cpp"
}
}
25 changes: 14 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ set(CMAKE_CXX_STANDARD 14) # Use C++14 standard for more features
# Declaring our executable
add_executable(${PROJECT_NAME})

# Set benchmark mode to true
# target_compile_definitions(${PROJECT_NAME} PRIVATE BENCHMARK_MODE)

# Check for CUDA
include(CheckLanguage)
check_language(CUDA)
Expand All @@ -23,17 +26,17 @@ else()
endif()

# Check for Apple Metal (only if CUDA is not found)
if(NOT CUDA_FOUND AND APPLE)
find_library(METAL_LIBRARY Metal)
find_library(FOUNDATION_LIBRARY Foundation)
if(METAL_LIBRARY AND FOUNDATION_LIBRARY)
set(METAL_FOUND TRUE)
target_compile_definitions(${PROJECT_NAME} PRIVATE USE_METAL)
message(STATUS "Apple Metal found. Building with Metal support.")
else()
message(WARNING "Neither CUDA nor Apple Metal found. Building without GPU acceleration.")
endif()
endif()
# if(NOT CUDA_FOUND AND APPLE)
# find_library(METAL_LIBRARY Metal)
# find_library(FOUNDATION_LIBRARY Foundation)
# if(METAL_LIBRARY AND FOUNDATION_LIBRARY)
# set(METAL_FOUND TRUE)
# target_compile_definitions(${PROJECT_NAME} PRIVATE USE_METAL)
# message(STATUS "Apple Metal found. Building with Metal support.")
# else()
# message(WARNING "Neither CUDA nor Apple Metal found. Building without GPU acceleration.")
# endif()
# 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
Expand Down
27 changes: 18 additions & 9 deletions include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ class Display {
private:
// Constants for display
SDL_DisplayMode displayMode;
#ifndef BENCHMARK_MODE
SDL_Window *window;
SDL_Texture *texture;
SDL_Surface *surface;
SDL_Event *event;
SDL_Renderer *renderer;
SDL_Keycode keyPressed;
uint32_t prevTime;
#else
uint32_t count;
uint32_t frameCount;
#endif
std::unique_ptr<FrameBuffer> frameBuffer;
std::vector<Vector3D> vertices;
std::vector<Vector2D> projectedVertices;
Expand All @@ -29,13 +36,13 @@ class Display {
Vector3D rotation;
Vector3D rotationSpeed;

SDL_Keycode keyPressed;

int prevTime;

public:
#ifndef BENCHMARK_MODE
// Constructor to initialize memory
Display();
#else
Display(uint32_t numOfFrames);
#endif

// Destructor to free memory
~Display();
Expand All @@ -53,8 +60,9 @@ class Display {
void clear();

// Method to check if the display should close
bool shouldClose() const;
bool shouldClose();

#ifdef USE_CUDA
// Method to initialize CUDA
virtual void InitalizeCuda();

Expand All @@ -63,14 +71,15 @@ class Display {

// Method to launch CUDA
virtual void LaunchCuda();

#elif USE_METAL
// Method to initialize Metal
// virtual void InitalizeMetal();
virtual void InitalizeMetal();

// Method to free Metal
// virtual void FreeMetal();
virtual void FreeMetal();

// Method to launch Metal
// virtual void LaunchMetal();
virtual void LaunchMetal();
#endif
};
} // namespace graphics
47 changes: 38 additions & 9 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
#include "../include/vector3d.hpp"

namespace graphics {
#ifndef BENCHMARK_MODE
// Constructor to initialize memory
Display::Display() {
#else
Display::Display(uint32_t numOfFrames) {
#endif
// Initialize the camera
camera = Vector3D(0, 0, -5);
rotation = Vector3D(0, 0, 0);
rotationSpeed = Vector3D(0, 0, 0);
#ifndef BENCHMARK_MODE
keyPressed = SDLK_UNKNOWN;

prevTime = SDL_GetTicks();

// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(
Expand All @@ -35,15 +39,19 @@ Display::Display() {
fprintf(stderr, "SDL_GetCurrentDisplayMode failed: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
#else
count = 0;
frameCount = numOfFrames;
#endif

// Initialize the frame buffer
frameBuffer = std::make_unique<FrameBuffer>(displayMode.w, displayMode.h);

// Initialize the frame buffer
frameBuffer = std::make_unique<FrameBuffer>(displayMode.w, displayMode.h);

d_vertices == nullptr;
d_projectedVertices == nullptr;
d_vertices = nullptr;
d_projectedVertices = nullptr;

int numVertices = 0;

Expand All @@ -66,9 +74,10 @@ Display::Display() {
#ifdef USE_CUDA
InitalizeCuda();
#elif USE_METAL
// InitalizeMetal();
InitalizeMetal();
#endif

#ifndef BENCHMARK_MODE
// Initialize the SDL window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, frameBuffer->getWidth(), frameBuffer->getHeight(),
Expand Down Expand Up @@ -99,13 +108,15 @@ Display::Display() {

// Set the blend mode
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
#endif
}

Display::~Display() {
// Free the frame buffer
frameBuffer.reset();

// Free the window
#ifndef BENCHMARK_MODE
SDL_DestroyWindow(window);
window = nullptr;

Expand All @@ -118,12 +129,14 @@ Display::~Display() {

// Quit SDL subsystems
SDL_Quit();
#endif
}

void Display::update() {
while (!SDL_TICKS_PASSED(SDL_GetTicks(), prevTime + FRAME_TIME))
;
#ifndef BENCHMARK_MODE
while (!SDL_TICKS_PASSED(SDL_GetTicks(), prevTime + FRAME_TIME));
prevTime = SDL_GetTicks();
#endif

#ifdef USE_CUDA
LaunchCuda();
Expand All @@ -147,6 +160,7 @@ void Display::update() {
projectedVertices[i] = vertex.project();
}
#endif
#ifndef BENCHMARK_MODE
// Update rotation
switch (keyPressed) {
case SDLK_UP:
Expand All @@ -165,13 +179,15 @@ void Display::update() {
break;
}
rotation.translate(rotationSpeed.x, rotationSpeed.y, rotationSpeed.z);
#endif
}

void Display::render() {
// Clear the renderer
clear();

// frameBuffer->drawFilledRectangle(64, 64, 128, 128, Color(0, 255, 0, 255));
// frameBuffer->drawFilledRectangle(64, 64, 128, 128, Color(0, 255, 0,
// 255));

// frameBuffer->drawGrid(Color(0xFF444444));

Expand All @@ -180,6 +196,7 @@ void Display::render() {
vertex.y + (frameBuffer->getHeight() / 2), 4, 4, Color(0xFFFFFF00));
}

#ifndef BENCHMARK_MODE
// Update the texture with the frame buffer data
SDL_UpdateTexture(texture, nullptr, frameBuffer->getData().data(),
frameBuffer->getWidth() * sizeof(uint32_t));
Expand All @@ -189,9 +206,11 @@ void Display::render() {

// Present the renderer
SDL_RenderPresent(renderer);
#endif
}

void Display::processInput() {
#ifndef BENCHMARK_MODE
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
Expand All @@ -202,16 +221,26 @@ void Display::processInput() {
keyPressed = SDLK_UNKNOWN;
}
}
#endif
}

void Display::clear() {
#ifndef BENCHMARK_MODE
// Clear the renderer
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
#endif

// Clear the frame buffer
frameBuffer->clear(Color(0, 0, 0, 0));
}

bool Display::shouldClose() const { return SDL_QuitRequested(); }
bool Display::shouldClose() {
#ifndef BENCHMARK_MODE
return SDL_QuitRequested();
#else
count++;
return count >= frameCount;
#endif
}
} // namespace graphics
25 changes: 23 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@

#include "../include/display.hpp"

#ifdef USE_METAL
#include <cassert>

#define NS_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#define MTK_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#include <simd/simd.h>

#include <AppKit/AppKit.hpp>
#include <Metal/Metal.hpp>
#include <MetalKit/MetalKit.hpp>
#endif

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(int argc, char **argv) {
// Initialization of display
// Initialization of display
#ifndef BENCHMARK_MODE
graphics::Display display;

#else
graphics::Display display(10000);
#endif
// Main graphics loop
// Loop until window close button is pressed
while (!display.shouldClose()) {
#ifdef BENCHMARK_MODE
display.update();
#else
display.processInput();
display.update();
display.render();
#endif
}
return EXIT_SUCCESS;
}

0 comments on commit cfe229a

Please sign in to comment.