mirror of
https://github.com/MartinOpat/cuda-based-raytrace.git
synced 2025-06-07 02:13:10 +02:00
added shader
This commit is contained in:
133
src/main.cu
133
src/main.cu
@@ -1,15 +1,15 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cmath>
|
||||
#include <cuda_runtime.h>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include "hurricanedata/datareader.h"
|
||||
#include "linalg/linalg.h"
|
||||
#include "img/handler.h"
|
||||
#include <cmath>
|
||||
#include "consts.h"
|
||||
#include <cuda_runtime.h>
|
||||
#include <fstream>
|
||||
#include "gui/MainWindow.h"
|
||||
#include "hurricanedata/datareader.h"
|
||||
#include "illumination/illumination.h"
|
||||
#include "img/handler.h"
|
||||
#include <iostream>
|
||||
#include "linalg/linalg.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
static float* d_volume = nullptr;
|
||||
@@ -45,69 +45,72 @@ void getSpeed(std::vector<float>& speedData, int idx = 0) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: incorporate this main into main.cpp
|
||||
int unmain(int argc, char** argv) {
|
||||
std::vector<float> data;
|
||||
// getTemperature(data);
|
||||
getSpeed(data);
|
||||
|
||||
int main() {
|
||||
std::vector<float> data;
|
||||
// getTemperature(data);
|
||||
getSpeed(data);
|
||||
|
||||
|
||||
// TODO: Eveontually remove debug below (i.e., eliminate for-loop etc.)
|
||||
// Generate debug volume data
|
||||
float* hostVolume = new float[VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH];
|
||||
// generateVolume(hostVolume, VOLUME_WIDTH, VOLUME_HEIGHT, VOLUME_DEPTH);
|
||||
for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) { // TODO: This is technically an unnecessary artifact of the old code taking in a float* instead of a std::vector
|
||||
// Discard temperatures above a small star (supposedly, missing temperature values)
|
||||
hostVolume[i] = data[i];
|
||||
if (data[i] + epsilon >= infty) hostVolume[i] = 0.0f;
|
||||
}
|
||||
// TODO: Eveontually remove debug below (i.e., eliminate for-loop etc.)
|
||||
// Generate debug volume data
|
||||
float* hostVolume = new float[VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH];
|
||||
// generateVolume(hostVolume, VOLUME_WIDTH, VOLUME_HEIGHT, VOLUME_DEPTH);
|
||||
for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) { // TODO: This is technically an unnecessary artifact of the old code taking in a float* instead of a std::vector
|
||||
// Discard temperatures above a small star (supposedly, missing temperature values)
|
||||
hostVolume[i] = data[i];
|
||||
if (data[i] + epsilon >= infty) hostVolume[i] = 0.0f;
|
||||
}
|
||||
|
||||
// Min-max normalization
|
||||
float minVal = *std::min_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH);
|
||||
float maxVal = *std::max_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH);
|
||||
for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) {
|
||||
hostVolume[i] = (hostVolume[i] - minVal) / (maxVal - minVal);
|
||||
}
|
||||
// Min-max normalization
|
||||
float minVal = *std::min_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH);
|
||||
float maxVal = *std::max_element(hostVolume, hostVolume + VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH);
|
||||
for (int i = 0; i < VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH; i++) {
|
||||
hostVolume[i] = (hostVolume[i] - minVal) / (maxVal - minVal);
|
||||
}
|
||||
|
||||
// Allocate + copy data to GPU
|
||||
size_t volumeSize = sizeof(float) * VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH;
|
||||
cudaMalloc((void**)&d_volume, volumeSize);
|
||||
cudaMemcpy(d_volume, hostVolume, volumeSize, cudaMemcpyHostToDevice);
|
||||
// Allocate + copy data to GPU
|
||||
size_t volumeSize = sizeof(float) * VOLUME_WIDTH * VOLUME_HEIGHT * VOLUME_DEPTH;
|
||||
cudaMalloc((void**)&d_volume, volumeSize);
|
||||
cudaMemcpy(d_volume, hostVolume, volumeSize, cudaMemcpyHostToDevice);
|
||||
|
||||
// Allocate framebuffer
|
||||
unsigned char* d_framebuffer;
|
||||
size_t fbSize = IMAGE_WIDTH * IMAGE_HEIGHT * 3 * sizeof(unsigned char);
|
||||
cudaMalloc((void**)&d_framebuffer, fbSize);
|
||||
cudaMemset(d_framebuffer, 0, fbSize);
|
||||
// Allocate framebuffer
|
||||
// unsigned char* d_framebuffer;
|
||||
// size_t fbSize = IMAGE_WIDTH * IMAGE_HEIGHT * 3 * sizeof(unsigned char);
|
||||
// cudaMalloc((void**)&d_framebuffer, fbSize);
|
||||
// cudaMemset(d_framebuffer, 0, fbSize);
|
||||
|
||||
// Copy external constants from consts.h to cuda
|
||||
copyConstantsToDevice();
|
||||
// Copy external constants from consts.h to cuda
|
||||
copyConstantsToDevice();
|
||||
|
||||
// NOTE: this shold be done within the rayTracer class
|
||||
// // Launch kernel
|
||||
// dim3 blockSize(16, 16);
|
||||
// dim3 gridSize((IMAGE_WIDTH + blockSize.x - 1)/blockSize.x,
|
||||
// (IMAGE_HEIGHT + blockSize.y - 1)/blockSize.y);
|
||||
//
|
||||
// raycastKernel<<<gridSize, blockSize>>>(
|
||||
// d_volume,
|
||||
// d_framebuffer
|
||||
// );
|
||||
// cudaDeviceSynchronize();
|
||||
// NOTE: this is done within the rayTracer class
|
||||
// // Launch kernel
|
||||
// dim3 blockSize(16, 16);
|
||||
// dim3 gridSize((IMAGE_WIDTH + blockSize.x - 1)/blockSize.x,
|
||||
// (IMAGE_HEIGHT + blockSize.y - 1)/blockSize.y);
|
||||
//
|
||||
// raycastKernel<<<gridSize, blockSize>>>(
|
||||
// d_volume,
|
||||
// d_framebuffer
|
||||
// );
|
||||
// cudaDeviceSynchronize();
|
||||
|
||||
// Copy framebuffer back to CPU
|
||||
unsigned char* hostFramebuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 3];
|
||||
cudaMemcpy(hostFramebuffer, d_framebuffer, fbSize, cudaMemcpyDeviceToHost);
|
||||
Window window(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
return window.init(d_volume);
|
||||
|
||||
// Export image
|
||||
saveImage("output.ppm", hostFramebuffer, IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
// Cleanup
|
||||
delete[] hostVolume;
|
||||
delete[] hostFramebuffer;
|
||||
cudaFree(d_volume);
|
||||
cudaFree(d_framebuffer);
|
||||
|
||||
std::cout << "Phong-DVR rendering done. Image saved to output.ppm" << std::endl;
|
||||
return 0;
|
||||
// // Copy framebuffer back to CPU
|
||||
// unsigned char* hostFramebuffer = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 3];
|
||||
// cudaMemcpy(hostFramebuffer, d_framebuffer, fbSize, cudaMemcpyDeviceToHost);
|
||||
//
|
||||
// // Export image
|
||||
// saveImage("output.ppm", hostFramebuffer, IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
//
|
||||
// // Cleanup //TODO: cleanup properly
|
||||
// delete[] hostVolume;
|
||||
// delete[] hostFramebuffer;
|
||||
// cudaFree(d_volume);
|
||||
// cudaFree(d_framebuffer);
|
||||
//
|
||||
// std::cout << "Phong-DVR rendering done. Image saved to output.ppm" << std::endl;
|
||||
// return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user