-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
孙万捷
authored and
孙万捷
committed
May 26, 2016
1 parent
52333b1
commit bd5bf86
Showing
20 changed files
with
27,875 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Created by 孙万捷 on 16/5/4. | ||
// | ||
|
||
#ifndef SUNPATHTRACER_CUDA_ENVIRONMENT_LIGHT_H | ||
#define SUNPATHTRACER_CUDA_ENVIRONMENT_LIGHT_H | ||
|
||
#define GLM_FORCE_INLINE | ||
#include <glm/glm.hpp> | ||
|
||
#include <cuda_runtime.h> | ||
|
||
class cudaEnvironmentLight | ||
{ | ||
public: | ||
__host__ __device__ cudaEnvironmentLight() {} | ||
|
||
__host__ __device__ void Set(cudaTextureObject_t tex) | ||
{ | ||
this->tex = tex; | ||
} | ||
|
||
__host__ __device__ void Set(const glm::vec3& radiance) | ||
{ | ||
tex = 0; | ||
defaultRadiance = radiance; | ||
} | ||
|
||
__host__ __device__ cudaTextureObject_t Get() | ||
{ | ||
return tex; | ||
} | ||
|
||
__device__ glm::vec3 GetEnvRadiance(const glm::vec2& texcoord) | ||
{ | ||
#ifdef __CUDACC__ | ||
auto val = tex2D<float4>(tex, texcoord.x, texcoord.y); | ||
return tex ? glm::vec3(val.x, val.y, val.z) : defaultRadiance; | ||
#else | ||
return glm::vec3(0.f); | ||
#endif | ||
} | ||
|
||
__device__ glm::vec3 GetEnvRadiance(const glm::vec3& dir, float u_offset = 0.f, float v_offset = 0.f) | ||
{ | ||
float theta = acosf(dir.y); | ||
float phi = atan2f(dir.x, dir.z); | ||
phi = phi < 0.f ? phi + 2.f * M_PI : phi; | ||
float u = phi * 0.5f * M_1_PI; | ||
float v = theta * M_1_PI; | ||
|
||
#ifdef __CUDACC__ | ||
auto val = tex2D<float4>(tex, u + u_offset, v + v_offset); | ||
return tex ? glm::vec3(val.x, val.y, val.z) : defaultRadiance; | ||
#else | ||
return glm::vec3(0.f); | ||
#endif | ||
} | ||
|
||
private: | ||
cudaTextureObject_t tex; | ||
glm::vec3 defaultRadiance; | ||
}; | ||
|
||
#endif //SUNPATHTRACER_CUDA_ENVIRONMENT_LIGHT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// Created by 孙万捷 on 16/5/25. | ||
// | ||
|
||
#ifndef SUNVOLUMERENDER_CUDALIGHTS_H | ||
#define SUNVOLUMERENDER_CUDALIGHTS_H | ||
|
||
#include <cuda_runtime.h> | ||
|
||
#define GLM_FORCE_INLINE | ||
#include <glm/glm.hpp> | ||
|
||
#include "cuda_environment_light.h" | ||
|
||
class cudaLights | ||
{ | ||
public: | ||
__device__ glm::vec3 GetEnvironmentRadiance(const glm::vec3& dir, float u_offset = 0.f, float v_offset = 0.f) | ||
{ | ||
return environmentLight.GetEnvRadiance(dir, u_offset, v_offset); | ||
} | ||
|
||
public: | ||
cudaEnvironmentLight environmentLight; | ||
}; | ||
|
||
#endif //SUNVOLUMERENDER_CUDALIGHTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// Created by 孙万捷 on 16/5/25. | ||
// | ||
|
||
#include "lights.h" | ||
#include "../../utils/helper_cuda.h" | ||
|
||
#define STB_IMAGE_IMPLEMENTATION | ||
#include "../../utils/stb_image.h" | ||
|
||
Lights::Lights() | ||
{ | ||
environmentLight.Set(glm::vec3(0.03f)); | ||
} | ||
|
||
Lights::~Lights() | ||
{ | ||
if(!environmentLight.Get()) | ||
{ | ||
checkCudaErrors(cudaDestroyTextureObject(environmentLight.Get())); | ||
environmentLight.Set(0); | ||
} | ||
|
||
if(envMapArray) | ||
{ | ||
checkCudaErrors(cudaFreeArray(envMapArray)); | ||
envMapArray = nullptr; | ||
} | ||
} | ||
|
||
void Lights::SetEnvironmentLight(std::string filename) | ||
{ | ||
int w = 0, h = 0, n = 0; | ||
float* data = stbi_loadf(filename.c_str(), &w, &h, &n, 0); | ||
if(!data) | ||
{ | ||
std::cerr<<"Unable to load environment map: "<<filename<<std::endl; | ||
exit(0); | ||
} | ||
|
||
//create channel desc | ||
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float4>(); | ||
//create cudaArray | ||
checkCudaErrors(cudaMallocArray(&envMapArray, &channelDesc, w, h)); | ||
if(n == 3) | ||
{ | ||
uint32_t count = w * h; | ||
std::vector<float4> ext_data; | ||
ext_data.reserve(count); | ||
for(auto i = 0; i < count; ++i) | ||
ext_data.push_back(make_float4(data[i * 3], data[i * 3 + 1], data[i * 3 + 2], 0.f)); | ||
|
||
checkCudaErrors(cudaMemcpyToArray(envMapArray, 0, 0, ext_data.data(), sizeof(float4) * w * h, cudaMemcpyHostToDevice)); | ||
} | ||
else | ||
checkCudaErrors(cudaMemcpyToArray(envMapArray, 0, 0, data, sizeof(float4) * w * h, cudaMemcpyHostToDevice)); | ||
//create resource desc | ||
cudaResourceDesc resDesc; | ||
memset(&resDesc, 0, sizeof(resDesc)); | ||
resDesc.resType = cudaResourceTypeArray; | ||
resDesc.res.array.array = envMapArray; | ||
//create texture desc | ||
cudaTextureDesc texDesc; | ||
memset(&texDesc, 0, sizeof(texDesc)); | ||
texDesc.addressMode[0] = cudaAddressModeWrap; | ||
texDesc.addressMode[1] = cudaAddressModeWrap; | ||
texDesc.filterMode = cudaFilterModeLinear; | ||
texDesc.readMode = cudaReadModeElementType; | ||
texDesc.normalizedCoords = true; | ||
//create cudaTextureObject | ||
cudaTextureObject_t tex; | ||
checkCudaErrors(cudaCreateTextureObject(&tex, &resDesc, &texDesc, NULL)); | ||
|
||
environmentLight.Set(tex); | ||
} | ||
|
||
void Lights::SetEnvionmentLight(const glm::vec3 &radiance) | ||
{ | ||
environmentLight.Set(radiance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// Created by 孙万捷 on 16/5/25. | ||
// | ||
|
||
#ifndef SUNVOLUMERENDER_LIGHTS_H | ||
#define SUNVOLUMERENDER_LIGHTS_H | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
#include <glm/glm.hpp> | ||
|
||
#include "cuda_environment_light.h" | ||
|
||
class Lights | ||
{ | ||
public: | ||
Lights(); | ||
~Lights(); | ||
void SetEnvironmentLight(std::string filename); | ||
void SetEnvionmentLight(const glm::vec3& radiance); | ||
|
||
public: | ||
cudaArray* envMapArray = nullptr; | ||
cudaEnvironmentLight environmentLight; | ||
}; | ||
|
||
#endif //SUNVOLUMERENDER_LIGHTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.