fix: forgor to allocate gpu memory

This commit is contained in:
Djairo Hougee 2025-01-07 13:52:50 +01:00
parent 04defb563f
commit d5a426e0c5
4 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,6 @@
#include "MainWindow.h" #include "MainWindow.h"
#include "cuda_runtime.h"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -56,7 +57,7 @@ int Window::init(float* data) {
Window::tick(); Window::tick();
} }
Window::free(); Window::free(data);
return 0; return 0;
} }
@ -73,10 +74,11 @@ int Window::init_quad(float* data) {
} }
void Window::free() { void Window::free(float* data) {
// To preserve the proper destruction order we forcefully set the quads to null (calling their destructor in the process) // To preserve the proper destruction order we forcefully set the quads to null (calling their destructor in the process)
// Not strictly necessary, but i saw some weird errors on exit without this so best to keep it in. // Not strictly necessary, but i saw some weird errors on exit without this so best to keep it in.
this->current_quad = nullptr; this->current_quad = nullptr;
cudaFree(data);
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();

View File

@ -17,7 +17,7 @@ public:
Window(unsigned int w, unsigned int h); Window(unsigned int w, unsigned int h);
int init(float* data); int init(float* data);
void free(); void free(float* data);
void resize(unsigned int w, unsigned int h); void resize(unsigned int w, unsigned int h);
private: private:

View File

@ -3,10 +3,11 @@
__host__ FrameBuffer::FrameBuffer(unsigned int w, unsigned int h) : w(w), h(h) { __host__ FrameBuffer::FrameBuffer(unsigned int w, unsigned int h) : w(w), h(h) {
this->buffer_size = w*h*4; this->buffer_size = w*h*sizeof(unsigned int);
cudaMalloc((void**)&this->buffer, this->buffer_size);
cudaMemset(this->buffer, 0, this->buffer_size);
} }
__device__ void FrameBuffer::writePixel(int x, int y, float r, float g, float b) { __device__ void FrameBuffer::writePixel(int x, int y, float r, float g, float b) {
int i = y * this->w + x; int i = y * this->w + x;

View File

@ -168,7 +168,7 @@ void Raycaster::resize(int w, int h) {
this->w = w; this->w = w;
this->h = h; this->h = h;
delete fb; delete this->fb;
this->fb = new FrameBuffer(w, h); this->fb = new FrameBuffer(w, h);
// TODO: should be globals probably // TODO: should be globals probably