Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 18, 2024
1 parent 81c72dc commit 1724b9a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
4 changes: 3 additions & 1 deletion include/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ constexpr int HEIGHT = 480;
constexpr int FRAME_RATE = 60;
constexpr int FRAME_TIME = 1000 / FRAME_RATE;
constexpr int FOV = 640;
} // namespace graphics

constexpr int NUM_VERTICES = 4 * 4 * 4;
} // namespace graphics
2 changes: 1 addition & 1 deletion include/display.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@


namespace graphics {
__global__ void matrixMultiplyKernel(float *A, float *B, float *C, int Arows, int Acols, int Bcols);
__global__ void transformVerticesKernel(Vector3D* vertices, Vector2D* projectedVertices, int size, Vector3D rotation, Vector3D camera);
} // namespace graphics
7 changes: 6 additions & 1 deletion include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ class Display {
SDL_Event *event;
SDL_Renderer *renderer;
std::unique_ptr<FrameBuffer> frameBuffer;
uint32_t *d_frameBuffer;
std::vector<Vector3D> vertices;
std::vector<Vector2D> projectedVertices;
Vector3D *d_vertices;
Vector2D *d_projectedVertices;


Vector3D camera;
Vector3D rotation;
Expand Down Expand Up @@ -60,6 +62,9 @@ class Display {
// Method to initialize Metal
virtual void InitalizeMetal();

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

// Method to free Metal
virtual void FreeMetal();
};
Expand Down
Empty file added include/vector3d.cuh
Empty file.
25 changes: 20 additions & 5 deletions src/display.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/display.hpp"
#include "../include/constants.hpp"

#ifdef USE_CUDA
#include "../include/display.cuh"
Expand Down Expand Up @@ -34,25 +35,32 @@ Display::Display() {
}

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

#ifdef USE_CUDA
InitalizeCuda();
#elif USE_METAL
InitalizeMetal();
#else
frameBuffer = std::make_unique<FrameBuffer>(displayMode.w, displayMode.h);
vertices.resize(NUM_VERTICES);
projectedVertices.resize(NUM_VERTICES);
#endif

int numVertices = 0;
// Initialize the vertices
// Start loading my array of vectors
// From -1 to 1 (in this 9x9x9 cube)
for (float x = -1; x <= 1; x += 0.25) {
for (float y = -1; y <= 1; y += 0.25) {
for (float z = -1; z <= 1; z += 0.25) {
vertices.push_back(Vector3D(x, y, z));
if (numVertices >= NUM_VERTICES) {
break;
}
vertices[numVertices] = Vector3D(x, y, z);
numVertices++;
}
}
}
projectedVertices.resize(vertices.size());


// Initialize the SDL window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, frameBuffer->getWidth(), frameBuffer->getHeight(),
Expand Down Expand Up @@ -110,6 +118,11 @@ void Display::update() {
prevTime = currentTime;

if (true) {
#ifdef USE_CUDA
LaunchCuda();
#elif USE_METAL
LaunchMetal();
#else
for (int i = 0; i < vertices.size(); i++) {
// Transform the vertices
auto vertex = vertices[i];
Expand All @@ -126,6 +139,8 @@ void Display::update() {
// Project the transformed vertices
projectedVertices[i] = vertex.project();
}
#endif
// Update rotation
rotation.translate(0.01, 0, 0);
deltaTime = 0;
}
Expand Down
49 changes: 47 additions & 2 deletions src/display.cu
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
#include "../include/display.cuh"
#include "../include/constants.hpp"

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

namespace graphics {
__host__ __device__ void cudaRotate(Vector3D* vertex, Vector2D* projectedVertices, double roll, double pitch, double yaw, int idx) {
vertex->rotate(roll, pitch, yaw);
}
__host__ __device__ void cudaTranslate(Vector3D* vertex, Vector2D* projectedVertices, double x, double y, double z, int idx) {
vertex->x += x;
vertex->y += y;
vertex->z += z;
}
__host__ __device__ void cudaProject(Vector3D* vertex, Vector2D* projectedVertices, int idx) {
projectedVertices[idx].x = (vertex->x * FOV) / vertex->z;
projectedVertices[idx].y = (vertex->y * FOV) / vertex->z;
}
__global__ void transformVerticesKernel(Vector3D* vertices, Vector2D* projectedVertices, int size, Vector3D rotation, Vector3D camera) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < size) {
Vector3D vertex = vertices[idx];

// Rotate the vertex
cudaRotate(&vertex, projectedVertices, rotation.x, rotation.y, rotation.z, idx);

// Translate the vertex
cudaTranslate(&vertex, projectedVertices, camera.x, camera.y, -camera.z, idx);

// Project the transformed vertex
cudaProject(&vertex, projectedVertices, idx);
}
}
void Display::InitalizeCuda() {
cudaMalloc((void**)&d_frameBuffer, displayMode.w * displayMode.h * sizeof(uint32_t));
cudaMalloc((void**)&d_vertices, NUM_VERTICES * sizeof(Vector3D));
cudaMalloc((void**)&d_projectedVertices, NUM_VERTICES * sizeof(Vector2D));
}
void Display::FreeCuda() {
cudaFree(d_frameBuffer);
cudaFree(d_vertices);
cudaFree(d_projectedVertices);
}
void Display::LaunchCuda() {
// Copy vertices to device
cudaMemcpy(d_vertices, vertices.data(), NUM_VERTICES * sizeof(Vector3D), cudaMemcpyHostToDevice);

// Launch kernel
int threadsPerBlock = 256;
int blocksPerGrid = (NUM_VERTICES + threadsPerBlock - 1) / threadsPerBlock;
transformVerticesKernel<<<blocksPerGrid, threadsPerBlock>>>(d_vertices, d_projectedVertices, NUM_VERTICES, rotation, camera);

// Copy projected vertices back to host
cudaMemcpy(projectedVertices.data(), d_projectedVertices, NUM_VERTICES * sizeof(Vector2D), cudaMemcpyDeviceToHost);

// Synchronize
cudaDeviceSynchronize();
}
} // namespace graphics
Empty file added src/vector3d.cu
Empty file.

0 comments on commit 1724b9a

Please sign in to comment.