summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrett weiland <brettsweiland@gmail.com>2024-06-01 01:36:18 -0500
committerbrett weiland <brettsweiland@gmail.com>2024-06-01 01:36:18 -0500
commitb0dd97ee6bf8d5daa587da40ad941efac68152df (patch)
treef162d32767e0b0f84bed284f6e8ab2c5309ff248
parent093200a449ea38952de52012e324036c106e294b (diff)
got raycasting laid out
-rw-r--r--.gitignore2
l---------3d_fractals_cuda.prf1
-rw-r--r--build/main.obin32912 -> 0 bytes
-rw-r--r--entity.cuh31
-rw-r--r--makefile15
-rw-r--r--render_object.cuh12
-rw-r--r--scene.h12
-rw-r--r--sphere.cuh17
-rw-r--r--src/.vscode/launch.json19
-rw-r--r--src/camera.cuh (renamed from camera.cuh)26
-rw-r--r--src/common.cuh (renamed from common.cuh)26
-rw-r--r--src/entity.cuh30
-rw-r--r--src/include/helper_math.h (renamed from include/helper_math.h)0
-rw-r--r--src/kernel.cu (renamed from kernel.cu)3
-rw-r--r--src/kernel.cuh (renamed from kernel.cuh)0
-rw-r--r--src/main.cu (renamed from main.cu)8
-rw-r--r--src/makefile25
-rw-r--r--src/render_object.cuh14
-rw-r--r--src/scene.cu14
-rw-r--r--src/scene.cuh (renamed from scene.cuh)23
-rw-r--r--src/sphere.cu9
-rw-r--r--src/sphere.cuh17
22 files changed, 178 insertions, 126 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8765681
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+old_cuda/*
+src/build/*
diff --git a/3d_fractals_cuda.prf b/3d_fractals_cuda.prf
new file mode 120000
index 0000000..8e05f78
--- /dev/null
+++ b/3d_fractals_cuda.prf
@@ -0,0 +1 @@
+/home/indigo/.unison/3d_fractals_cuda.prf \ No newline at end of file
diff --git a/build/main.o b/build/main.o
deleted file mode 100644
index 737de98..0000000
--- a/build/main.o
+++ /dev/null
Binary files differ
diff --git a/entity.cuh b/entity.cuh
deleted file mode 100644
index 737ef8b..0000000
--- a/entity.cuh
+++ /dev/null
@@ -1,31 +0,0 @@
-#ifndef ENTITY_H
-#define ENTITY_H
-#include "common.cuh"
-
-//we could make a template to allow double percision, but start with float
-//idk how nessesary it is yet so I'll go ahead.
-//I know I needed it for zoomin far into the mandelbrot ig, so it's not
-//out of the question
-template<class T> class entity {
- using T3 = typename vect_t3<T>::vect_t;
- public:
- __device__ entity() : pos_(vect_create<T3>(0)), rot_(vect_create<T3>(0)), scale_(vect_create<T3>(0)) {};
- __device__ entity(const T3 pos, const T3 rot, const T3 scale) : pos_(pos), rot_(rot), scale_(scale) {};
- __device__ entity(const float3 pos) : pos_(pos), rot_(vect_create<T3>(0)), scale_(vect_create<T3>(0)) {};
-
-
- T3 get_pos() const { return pos_; }
- T3 get_rot() const { return rot_; }
- T3 get_scale() const { return scale_; }
-
- __device__ void set_pos(const T3 pos) { pos_ = pos; }
- __device__ void set_rot(const T3 rot) { rot_ = rot; }
- __device__ void set_scale(const T3 scale) { scale_ = scale; }
-
- protected:
- T3 pos_;
- T3 rot_;
- T3 scale_;
-
-};
-#endif
diff --git a/makefile b/makefile
deleted file mode 100644
index 7f6bddb..0000000
--- a/makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-LIBS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
-$CC = gcc
-INC = -I /opt/cuda/include
-make:
- nvcc $(LIBS) $(INC) -O0 --debug -c main.cu -o build/main.o
- nvcc --device-debug --compile kernel.cu -o build/kernel.o
- nvcc $(LIBS) -O0 -o build/indigo_worlds build/main.o build/kernel.o
-
-run:
- build/indigo_worlds
-
-clean:
- rm -rf build/*
-
-
diff --git a/render_object.cuh b/render_object.cuh
deleted file mode 100644
index 0063d94..0000000
--- a/render_object.cuh
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef RENDER_OBJECT_H
-#define RENDER_OBJECT_H
-#include "entity.cuh"
-
-template<class T> class render_object : public entity<T> {
- using T3 = typename vect_t3<T>::vect_t;
- using entity<T>::entity;
- public:
- virtual __device__ T distance_estimator(T3 point) const = 0;
-};
-
-#endif
diff --git a/scene.h b/scene.h
deleted file mode 100644
index 05ce2af..0000000
--- a/scene.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef SCENE_H
-#include SCENE_H
-#include "camera.cuh"
-#include "render_object.cuh"
-
-//for now we only neeed once scene, later we'll expand this to just be a virtual template
-template <class T> class scene {
-
-
-}
-
-#endif \ No newline at end of file
diff --git a/sphere.cuh b/sphere.cuh
deleted file mode 100644
index 33c77f2..0000000
--- a/sphere.cuh
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef SPHERE_H
-#define SPHERE_H
-#include "render_object.cuh"
-template<class T> class sphere : public render_object<T> {
- using render_object<T>::render_object;
- using T3 = typename vect_t3<T>::vect_t;
- public:
- __device__ T distance_estimator(T3 point) const;
- private:
- T r_ = 1;
-};
-
-template <class T> __device__ T sphere<T>::distance_estimator(T3 point) const {
- return length(point) - r_;
-}
-
-#endif
diff --git a/src/.vscode/launch.json b/src/.vscode/launch.json
new file mode 100644
index 0000000..33752cd
--- /dev/null
+++ b/src/.vscode/launch.json
@@ -0,0 +1,19 @@
+{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "CUDA C++: Launch",
+ "type": "cuda-gdb",
+ "request": "launch",
+ "program": "${workspaceFolder}/build/indigo_worlds"
+ },
+ {
+ "name": "CUDA C++: Attach",
+ "type": "cuda-gdb",
+ "request": "attach"
+ }
+ ]
+} \ No newline at end of file
diff --git a/camera.cuh b/src/camera.cuh
index 61944fe..4d04fcd 100644
--- a/camera.cuh
+++ b/src/camera.cuh
@@ -9,38 +9,32 @@
//template <class T> class scene;
//I am soooo high lol
-template<class T>
-class camera : public entity<T> {
- using T3 = typename vect_t3<T>::vect_t;
- using T2 = typename vect_t2<T>::vect_t;
+class camera : public entity {
public:
- __device__ void render();
- __device__ camera(scene<T> *pscene, const T fov = 1, const T3 pos = vect_create<T3>(0), const T3 rot = vect_create<T3>(0))
- : pscene(pscene), fov(fov), entity<T>(pos, rot, vect_create<T3>(0)) {};
-
- //__device__ ~camera();
+ __device__ camera(scene *pscene, const T fov = 1, const vect3 pos = make_vect3(0), const vect3 rot = make_vect3(0))
+ : pscene(pscene), fov(fov), entity(pos, rot, make_vect3(0)) {};
private:
T fov;
- T2 size;
+ float2 size;
int steps = 100;
T clip_min = .1;
T clip_max = 100;
- scene<T> *pscene;
+ //scene *pscene;
};
/**
//later we'll make scenes objects, rn im lazy (TODO)
-template <class T> __device__ void camera<T>::render() {
+template <class T> __device__ void camera::render() {
//TODO *really* need to clean this up once you get further
//extra dimentions is extra math
//either generisize float3 or stop using this fucking template nonsense
const uint3 unnormalized_uv = ((blockDim * blockIdx) + threadIdx);
const unsigned int img_index = (unnormalized_uv.x + (unnormalized_uv.y * blockDim.x * gridDim.x)) * 4;
- const T3 uv = ((2 * vect_create<T3>(unnormalized_uv)) / vect_create<T3>(gridDim * blockDim)) - 1;
- const T3 ray_direction = normalize(vect_create<T3>(uv.x, uv.y, 1));
+ const vect3 uv = ((2 * make_vect3(unnormalized_uv)) / make_vect3(gridDim * blockDim)) - 1;
+ const vect3 ray_direction = normalize(make_vect3(uv.x, uv.y, 1));
T dist;
T total_dist = 0;
- T3 ray;
+ vect3 ray;
int i;
@@ -48,7 +42,7 @@ template <class T> __device__ void camera<T>::render() {
T min_dist = clip_max;
- render_object<T> **objs = pscene->get_objs();
+ render_object **objs = pscene->get_objs();
for(i = 0; i < steps; i++) {
ray = this->pos_ + (total_dist * ray_direction);
//gyagh memory lookups
diff --git a/common.cuh b/src/common.cuh
index d5a9cdf..9e026f4 100644
--- a/common.cuh
+++ b/src/common.cuh
@@ -6,16 +6,16 @@
/**
template <class T> class vect_t2;
-template <class T> class vect_t3;
+template <class T> class vect3;
template <class T> class vect_t4;
//this feels so hacky... idk why people are so scared of metaprogramming
template <> class vect_t2<double> { public: using vect_t = double2; };
-template <> class vect_t3<double> { public: using vect_t = double3; };
+template <> class vect3<double> { public: using vect_t = double3; };
template <> class vect_t4<double> { public: using vect_t = double4; };
template <> class vect_t2<float> { public: using vect_t = float2; };
-template <> class vect_t3<float> { public: using vect_t = float3; };
+template <> class vect3<float> { public: using vect_t = float3; };
template <> class vect_t4<float> { public: using vect_t = float4; };
@@ -32,12 +32,22 @@ template <class float3, class X, class Y, class Z> __device__ inline float3 vect
templates, but this changes the structure of my entire project in unwanted
ways, so I'm switching over to typedefs. **/
-typedef float2 vect_t2;
-typedef float3 vect_t3;
-typedef float4 vect_t4;
+typedef float2 vect2;
+typedef float3 vect3;
typedef float T;
-#define vect1to3(x) (make_float3(x))
-#define make_vect(x, y, z) (make_float3(x, y, z))
+#define make_vect3(...) (make_float3(__VA_ARGS__))
+
+//TODO move to color.cuh
+typedef float3 Color;
+#define make_color(...) (make_float3(__VA_ARGS__))
+
+//TODO move to ray.cuh
+struct Ray {
+ Color color;
+ vect3 start;
+ vect3 direction; //MUST BE A UNIT VECTOR
+ int bounces;
+};
#endif
diff --git a/src/entity.cuh b/src/entity.cuh
new file mode 100644
index 0000000..b086644
--- /dev/null
+++ b/src/entity.cuh
@@ -0,0 +1,30 @@
+#ifndef ENTITY_H
+#define ENTITY_H
+#include "common.cuh"
+
+//we could make a template to allow double percision, but start with float
+//idk how nessesary it is yet so I'll go ahead.
+//I know I needed it for zoomin far into the mandelbrot ig, so it's not
+//out of the question
+class Entity {
+ public:
+ __device__ Entity() : pos_(make_vect3(0)), rot_(make_vect3(0)), scale_(make_vect3(0)) {};
+ __device__ Entity(const vect3 pos, const vect3 rot, const vect3 scale) : pos_(pos), rot_(rot), scale_(scale) {};
+ __device__ Entity(const float3 pos) : pos_(pos), rot_(make_vect3(0)), scale_(make_vect3(0)) {};
+
+
+ vect3 get_pos() const { return pos_; }
+ vect3 get_rot() const { return rot_; }
+ vect3 get_scale() const { return scale_; }
+
+ __device__ void set_pos(const vect3 pos) { pos_ = pos; }
+ __device__ void set_rot(const vect3 rot) { rot_ = rot; }
+ __device__ void set_scale(const vect3 scale) { scale_ = scale; }
+
+ protected:
+ vect3 pos_;
+ vect3 rot_;
+ vect3 scale_;
+
+};
+#endif
diff --git a/include/helper_math.h b/src/include/helper_math.h
index 6d2f63a..6d2f63a 100644
--- a/include/helper_math.h
+++ b/src/include/helper_math.h
diff --git a/kernel.cu b/src/kernel.cu
index c6895dc..40944c0 100644
--- a/kernel.cu
+++ b/src/kernel.cu
@@ -4,6 +4,7 @@
#include "scene.cuh"
__global__ void render(uint8_t *image) {
- scene<float> scene;
+ Scene scene;
+ scene.debug();
//scene.render(image);
}
diff --git a/kernel.cuh b/src/kernel.cuh
index 2c5256e..2c5256e 100644
--- a/kernel.cuh
+++ b/src/kernel.cuh
diff --git a/main.cu b/src/main.cu
index 4aa6cbb..aa74a6c 100644
--- a/main.cu
+++ b/src/main.cu
@@ -1,10 +1,11 @@
+#include <stdint.h>
#include <stdio.h>
#include <stdint.h>
#include <cuda_runtime.h>
#include <string.h>
+#include <raylib.h>
#include "kernel.cuh"
-#include "raylib.h"
int main() {
//bluuuuugh i'll figure out occupancy later, this res are easy
@@ -37,9 +38,13 @@ int main() {
Texture tex = LoadTextureFromImage(image);
while(!WindowShouldClose()) {
+ cudaError_t err;
//cuda stuff
cudaMalloc((void **)&image_d, res_x * res_y * sizeof(Color));
render<<<blockCount, threadCount>>>(image_d);
+ if((err = cudaGetLastError()) != cudaSuccess) {
+ printf("kernel did not launch! Error: %s\n", cudaGetErrorString(err));
+ }
cudaDeviceSynchronize();
cudaMemcpy(texture_data, (void **)image_d, res_x * res_y * sizeof(Color), cudaMemcpyDeviceToHost);
@@ -49,6 +54,5 @@ int main() {
DrawFPS(0, 0);
EndDrawing();
}
-
return 0;
}
diff --git a/src/makefile b/src/makefile
new file mode 100644
index 0000000..4d86411
--- /dev/null
+++ b/src/makefile
@@ -0,0 +1,25 @@
+LIBS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
+$CC = gcc
+INC = -I /opt/cuda/include
+COMPILED_BIN = build/indigo_worlds
+
+CU_SRCFILES := $(wildcard *.cu)
+CU_OBJFILES := $(patsubst %.cu, %.o, $(CU_SRCFILES))
+
+all: $(CU_OBJFILES)
+ nvcc $(LIBS) -o build/indigo_worlds build/*.o
+
+%.o: %.cu
+ nvcc --device-debug -dc -c $< -o build/$@
+
+run: all
+ build/indigo_worlds
+
+clean:
+ rm -rf build/*
+
+
+#make:
+# nvcc $(LIBS) $(INC) -O0 --debug -c main.cu -o build/main.o
+# nvcc --device-debug --compile kernel.cu -o build/kernel.o
+# nvcc $(LIBS) -O0 -o build/indigo_worlds build/main.o build/kernel.o
diff --git a/src/render_object.cuh b/src/render_object.cuh
new file mode 100644
index 0000000..f25f835
--- /dev/null
+++ b/src/render_object.cuh
@@ -0,0 +1,14 @@
+#ifndef RENDER_OBJECT_H
+#define RENDER_OBJECT_H
+#include "entity.cuh"
+#include "common.cuh" //TODO color
+
+class Render_object : public Entity {
+ using Entity::Entity;
+ public:
+ virtual __device__ T distance_estimator(vect3 point) const = 0;
+ virtual __device__ Color get_color(struct Ray ray) const = 0;
+
+};
+
+#endif
diff --git a/src/scene.cu b/src/scene.cu
new file mode 100644
index 0000000..100af14
--- /dev/null
+++ b/src/scene.cu
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include "scene.cuh"
+
+__device__ void Scene::debug() {
+ //const uint3 unnormalized_uv = ((blockDim * blockIdx) + threadIdx);
+ //const unsigned int img_index = (unnormalized_uv.x + (unnormalized_uv.y * blockDim.x * gridDim.x)) * 4;
+}
+
+__device__ Color Scene::raycast(struct Ray ray) {
+ if(ray.bounces++ > max_bounces) return make_color(0);
+ const size_t obj_count = sizeof(objs) / sizeof(objs[0]);
+ for(size_t obj_i = 0; obj_i < obj_count; obj_i++);
+ return make_color(0);
+}
diff --git a/scene.cuh b/src/scene.cuh
index 31bb99f..e3d5896 100644
--- a/scene.cuh
+++ b/src/scene.cuh
@@ -5,28 +5,27 @@
#include "sphere.cuh"
#include "render_object.cuh"
#include "include/helper_math.h"
+#include <stdint.h>
-template <class T> class camera;
+//template <class T> class camera;
//when we get animations with multiple scenes, we'll make this a virtual function
//with array of DE objects and cam
-template <class T>
-class scene {
- using T3 = typename vect_t3<T>::vect_t;
+class Scene {
+ const int max_bounces = 10;
public:
//__device__ void render(uint8_t *image) { cam.render(); };
- __device__ render_object<T> **get_objs() { return objs; }
- __device__ render_object<T> **get_image() { return image; }
+ __device__ Render_object **get_objs() { return objs; }
+ __device__ void debug();
+ __device__ Color raycast(struct Ray ray);
private:
- camera<T> cam = camera<T>();
- sphere<T> sp1 = sphere<T>(vect_create<T3>(0,0.4,-5));
- sphere<T> sp2 = sphere<T>(vect_create<T3>(0,-0.4,-5));
+ //camera cam = camera();
+ Sphere sp1 = Sphere(make_vect3(0, .4, -5));
+ Sphere sp2 = Sphere(make_vect3(0, -0.4,-5));
protected:
//idk why I need to specify the size... why can't the compiler figure that out?
- render_object<T> *objs[3] = {&sp1, &sp2, NULL};
+ Render_object *objs[3] = {&sp1, &sp2, NULL};
uint8_t *image;
};
-#include "camera.cuh"
-
#endif
diff --git a/src/sphere.cu b/src/sphere.cu
new file mode 100644
index 0000000..849a37d
--- /dev/null
+++ b/src/sphere.cu
@@ -0,0 +1,9 @@
+#include "sphere.cuh"
+
+__device__ T Sphere::distance_estimator(vect3 point) const {
+ return length(point) - r_;
+}
+
+__device__ Color Sphere::get_color(struct Ray ray) const {
+ return make_color(0);
+}
diff --git a/src/sphere.cuh b/src/sphere.cuh
new file mode 100644
index 0000000..0d228ff
--- /dev/null
+++ b/src/sphere.cuh
@@ -0,0 +1,17 @@
+#ifndef SPHERE_H
+#define SPHERE_H
+
+#include "render_object.cuh"
+#include "common.cuh"
+
+class Sphere : public Render_object {
+ using Render_object::Render_object;
+ public:
+ __device__ T distance_estimator(vect3 point) const;
+ __device__ Color get_color(struct Ray ray) const;
+ private:
+ T r_ = 1;
+};
+
+
+#endif