Skip to content

Commit

Permalink
Implement Falcor env map sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
Derpius committed Aug 15, 2022
1 parent 762b28f commit ce0928f
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 282 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

project("VisTrace" CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

find_package(OpenMP QUIET)

Expand Down
25 changes: 15 additions & 10 deletions include/vistrace/IRenderTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace RT
{
constexpr uint8_t MAX_MIPS = 32;

struct Pixel
{
float r = 0, g = 0, b = 0, a = 0;
Expand All @@ -30,6 +32,8 @@ namespace RT
R8,
RG88,
RGB888,
RF,
RGFF,
RGBFFF,
Size,

Expand All @@ -41,40 +45,41 @@ namespace RT
1,
2,
3,
1,
2,
3
};

static const size_t STRIDES[static_cast<size_t>(Format::Size)] = {
sizeof(uint8_t),
sizeof(uint8_t),
sizeof(uint8_t),
sizeof(float),
sizeof(float),
sizeof(float)
};

class ITexture
{
protected:
Format mFormat;
size_t mChannelSize, mPixelSize;
uint8_t* pBuffer = nullptr;
uint16_t mWidth = 0, mHeight = 0;

public:
ITexture() {};
virtual ~ITexture() {};

virtual bool Resize(uint16_t width, uint16_t height) = 0;
virtual bool Resize(uint16_t width, uint16_t height, uint8_t mips = 1) = 0;

virtual bool IsValid() const = 0;
virtual uint16_t GetWidth() const = 0;
virtual uint16_t GetHeight() const = 0;
virtual uint8_t GetMIPs() const = 0;
virtual Format GetFormat() const = 0;

virtual uint8_t* GetRawData() = 0;
virtual uint8_t* GetRawData(uint8_t mip = 1) = 0;
virtual size_t GetPixelSize() const = 0;
virtual size_t GetSize() const = 0;

virtual Pixel GetPixel(uint16_t x, uint16_t y) const = 0;
virtual void SetPixel(uint16_t x, uint16_t y, const Pixel& pixel) = 0;
virtual Pixel GetPixel(uint16_t x, uint16_t y, uint8_t mip = 0) const = 0;
virtual void SetPixel(uint16_t x, uint16_t y, const Pixel& pixel, uint8_t mip = 0) = 0;

virtual void GenerateMIPs() = 0;
};
}
83 changes: 40 additions & 43 deletions source/VisTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,20 +708,18 @@ LUA_FUNCTION(TraceResult_EvalPDF)
#pragma endregion

#pragma region HDRI API
static int HDRI_id;

LUA_FUNCTION(LoadHDRI)
{
LUA->CheckString(1);

float radianceThresh = 1000.f;
if (LUA->IsType(2, Type::Number)) {
radianceThresh = LUA->GetNumber(2);
uint16_t importanceDim = 512;
if (LUA->IsType(2, Type::Number) && LUA->GetNumber(2) > 0) {
importanceDim = LUA->GetNumber(2);
}

uint32_t areaThresh = 8 * 8;
if (LUA->IsType(3, Type::Number)) {
areaThresh = LUA->GetNumber(3);
uint16_t importanceSamples = 64;
if (LUA->IsType(3, Type::Number) && LUA->GetNumber(3) > 0) {
importanceSamples = LUA->GetNumber(3);
}

std::string texturePath = "vistrace_hdris/" + std::string(LUA->GetString(1)) + ".hdr";
Expand All @@ -736,41 +734,40 @@ LUA_FUNCTION(LoadHDRI)
FileSystem::Read(data, filesize, file);
FileSystem::Close(file);

HDRI* pHDRI = new HDRI(data, filesize, radianceThresh, areaThresh);
LUA->PushUserType_Value(pHDRI, HDRI_id);
HDRI* pHDRI = new HDRI(data, filesize, importanceDim, importanceSamples);
LUA->PushUserType_Value(pHDRI, HDRI::id);
free(data);

return 1;
}

LUA_FUNCTION(HDRI_IsValid)
{
LUA->CheckType(1, HDRI_id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI_id);
LUA->CheckType(1, HDRI::id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI::id);
LUA->PushBool(pHDRI->IsValid());
return 1;
}

LUA_FUNCTION(HDRI_GetPixel)
{
LUA->CheckType(1, HDRI_id);
LUA->CheckType(1, HDRI::id);
LUA->CheckType(2, Type::Vector);

HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI_id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI::id);
Vector direction = LUA->GetVector(2);

glm::vec4 colour = pHDRI->GetPixel(glm::vec3(direction.x, direction.y, direction.z));
glm::vec3 colour = pHDRI->GetPixel(glm::vec3(direction.x, direction.y, direction.z));
LUA->PushVector(MakeVector(colour.r, colour.g, colour.b));
LUA->PushNumber(colour.a); // Radiance
return 2;
return 1;
}

LUA_FUNCTION(HDRI_EvalPDF)
{
LUA->CheckType(1, HDRI_id);
LUA->CheckType(1, HDRI::id);
LUA->CheckType(2, Type::Vector);

HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI_id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI::id);
Vector direction = LUA->GetVector(2);

float pdf = pHDRI->EvalPDF(glm::vec3(direction.x, direction.y, direction.z));
Expand All @@ -780,10 +777,10 @@ LUA_FUNCTION(HDRI_EvalPDF)

LUA_FUNCTION(HDRI_Sample)
{
LUA->CheckType(1, HDRI_id);
LUA->CheckType(1, HDRI::id);
LUA->CheckType(2, Sampler_id);

HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI_id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI::id);
Sampler* pSampler = *LUA->GetUserType<Sampler*>(2, Sampler_id);

float pdf = 0.f;
Expand All @@ -806,10 +803,10 @@ LUA_FUNCTION(HDRI_Sample)

LUA_FUNCTION(HDRI_SetAngles)
{
LUA->CheckType(1, HDRI_id);
LUA->CheckType(1, HDRI::id);
LUA->CheckType(2, Type::Angle);

HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI_id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI::id);
QAngle ang = LUA->GetAngle(2);

pHDRI->SetAngle(glm::vec3(ang.x, ang.y, ang.z));
Expand All @@ -825,8 +822,8 @@ LUA_FUNCTION(HDRI_tostring)

LUA_FUNCTION(HDRI_gc)
{
LUA->CheckType(1, HDRI_id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI_id);
LUA->CheckType(1, HDRI::id);
HDRI* pHDRI = *LUA->GetUserType<HDRI*>(1, HDRI::id);
delete pHDRI;
return 0;
}
Expand Down Expand Up @@ -877,8 +874,6 @@ LUA_FUNCTION(CalcRayOrigin)
}
#pragma endregion

#define PUSH_C_FUNC(function) LUA->PushCFunction(function); LUA->SetField(-2, #function)

LUA_FUNCTION(GM_Initialize)
{
printLua(LUA, "VisTrace: Loading map...");
Expand Down Expand Up @@ -909,6 +904,12 @@ LUA_FUNCTION(GM_Initialize)
return 0;
}

#define PUSH_C_FUNC(function) LUA->PushCFunction(function); LUA->SetField(-2, #function)

#define PUSH_ENUM(enum, field) \
LUA->PushNumber(static_cast<double>(enum::field)); \
LUA->SetField(-2, #field)

GMOD_MODULE_OPEN()
{
switch (FileSystem::LoadFileSystem()) {
Expand Down Expand Up @@ -1057,7 +1058,7 @@ GMOD_MODULE_OPEN()
LUA->SetField(-2, "GetFloat2D");
LUA->Pop();

HDRI_id = LUA->CreateMetaTable("HDRI");
HDRI::id = LUA->CreateMetaTable("HDRI");
LUA->Push(-1);
LUA->SetField(-2, "__index");
LUA->PushCFunction(HDRI_tostring);
Expand Down Expand Up @@ -1120,21 +1121,17 @@ GMOD_MODULE_OPEN()
LUA->SetField(-2, "vistrace");

LUA->CreateTable();
LUA->PushNumber(0);
LUA->SetField(-2, "R8");
LUA->PushNumber(1);
LUA->SetField(-2, "RG88");
LUA->PushNumber(2);
LUA->SetField(-2, "RGB888");
LUA->PushNumber(3);
LUA->SetField(-2, "RGBFFF");
LUA->PushNumber(4);
LUA->SetField(-2, "Size");

LUA->PushNumber(3);
LUA->SetField(-2, "Albedo");
LUA->PushNumber(3);
LUA->SetField(-2, "Normal");
PUSH_ENUM(RT::Format, R8);
PUSH_ENUM(RT::Format, RG88);
PUSH_ENUM(RT::Format, RGB888);
PUSH_ENUM(RT::Format, RF);
PUSH_ENUM(RT::Format, RGFF);
PUSH_ENUM(RT::Format, RGBFFF);

PUSH_ENUM(RT::Format, Size);

PUSH_ENUM(RT::Format, Albedo);
PUSH_ENUM(RT::Format, Normal);
LUA->SetField(-2, "VisTraceRTFormat");
LUA->Pop();

Expand Down
Loading

0 comments on commit ce0928f

Please sign in to comment.