Skip to content

Commit

Permalink
Add extension API
Browse files Browse the repository at this point in the history
  • Loading branch information
Derpius committed Aug 11, 2022
1 parent 8674e56 commit 5218e26
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 79 deletions.
1 change: 1 addition & 0 deletions Module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_include_directories(
${PROJECT_NAME} PRIVATE
"source"
"source/objects"
"include"
"libs/BSPParser"
"libs/bvh/include"
"libs/glm"
Expand Down
80 changes: 80 additions & 0 deletions Module/include/IRenderTarget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include <cstdint>

namespace RT
{
struct Pixel
{
float r = 0, g = 0, b = 0, a = 0;

float& operator[](size_t i)
{
switch (i) {
case 0:
return r;
case 1:
return g;
case 2:
return b;
case 3:
return a;
default:
return r;
}
}
};

enum class Format : uint8_t
{
R8,
RG88,
RGB888,
RGBFFF,
Size,

Albedo = RGBFFF,
Normal = RGBFFF
};

static const uint8_t CHANNELS[static_cast<size_t>(Format::Size)] = {
1,
2,
3,
3
};

static const size_t STRIDES[static_cast<size_t>(Format::Size)] = {
sizeof(uint8_t),
sizeof(uint8_t),
sizeof(uint8_t),
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 IsValid() const = 0;
virtual uint16_t GetWidth() const = 0;
virtual uint16_t GetHeight() const = 0;
virtual Format GetFormat() const = 0;

virtual uint8_t* GetRawData() = 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;
};
}
5 changes: 5 additions & 0 deletions Module/include/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# VisTrace Includes

These files can be included without any dependencies for use in your own projects, allowing you to interface with VisTrace objects on the stack

Use `GetProcAddress` to get metatable type IDs from the VisTrace dll. These are declared in the respective object's header file, and will be -1 if invalid
46 changes: 27 additions & 19 deletions Module/source/VisTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ LUA_FUNCTION(CreateRenderTarget)
case RT::Format::RG88:
case RT::Format::RGB888:
case RT::Format::RGBFFF:
LUA->PushUserType_Value(new RT::Texture(width, height, format), RT::Texture::id);
LUA->PushUserType_Value(new RT::Texture(width, height, format), g_IRenderTargetID);
return 1;
default:
LUA->ArgError(3, "Invalid format");
Expand All @@ -91,8 +91,8 @@ LUA_FUNCTION(CreateRenderTarget)
}
LUA_FUNCTION(RT_gc)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);

LUA->SetUserType(1, NULL);
delete pRt;
Expand All @@ -102,45 +102,45 @@ LUA_FUNCTION(RT_gc)

LUA_FUNCTION(RT_IsValid)
{
RT::Texture** ppRt = LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
RT::Texture** ppRt = LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
LUA->PushBool(ppRt != nullptr && (*ppRt)->IsValid());
return 1;
}

LUA_FUNCTION(RT_Resize)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
LUA->PushBool(pRt->Resize(LUA->CheckNumber(2), LUA->CheckNumber(3)));
return 1;
}

LUA_FUNCTION(RT_GetWidth)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
LUA->PushNumber(pRt->GetWidth());
return 1;
}
LUA_FUNCTION(RT_GetHeight)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
LUA->PushNumber(pRt->GetHeight());
return 1;
}
LUA_FUNCTION(RT_GetFormat)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
LUA->PushNumber(static_cast<double>(pRt->GetFormat()));
return 1;
}

LUA_FUNCTION(RT_GetPixel)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
if (!pRt->IsValid()) LUA->ThrowError("Invalid render target");

uint16_t x = LUA->CheckNumber(2), y = LUA->CheckNumber(3);
Expand All @@ -154,8 +154,8 @@ LUA_FUNCTION(RT_GetPixel)
}
LUA_FUNCTION(RT_SetPixel)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
if (!pRt->IsValid()) LUA->ThrowError("Invalid render target");

uint16_t x = LUA->CheckNumber(2), y = LUA->CheckNumber(3);
Expand All @@ -172,8 +172,8 @@ LUA_FUNCTION(RT_SetPixel)

LUA_FUNCTION(RT_Tonemap)
{
LUA->CheckType(1, RT::Texture::id);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, RT::Texture::id);
LUA->CheckType(1, g_IRenderTargetID);
RT::Texture* pRt = *LUA->GetUserType<RT::Texture*>(1, g_IRenderTargetID);
if (!pRt->IsValid()) LUA->ThrowError("Invalid render target");
if (pRt->GetFormat() != RT::Format::RGBFFF) LUA->ThrowError("Render target's format must be RGBFFF");

Expand Down Expand Up @@ -907,7 +907,7 @@ GMOD_MODULE_OPEN()
LUA->Call(3, 0);
LUA->Pop(2); // _G and hook

RT::Texture::id = LUA->CreateMetaTable("VisTraceRT");
g_IRenderTargetID = LUA->CreateMetaTable("VisTraceRT");
LUA->Push(-1);
LUA->SetField(-2, "__index");
LUA->PushCFunction(RT_tostring);
Expand Down Expand Up @@ -1106,6 +1106,14 @@ GMOD_MODULE_OPEN()
LUA->Pop();

printLua(LUA, "VisTrace Loaded!");

LUA->PushSpecial(SPECIAL_GLOB);
LUA->GetField(-1, "hook");
LUA->GetField(-1, "Call");
LUA->PushString("VisTraceInit");
LUA->Call(1, 0);
LUA->Pop(2);

return 0;
}

Expand Down
8 changes: 4 additions & 4 deletions Module/source/objects/RenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

using namespace RT;

int Texture::id{ -1 };
DLL_EXPORT int g_IRenderTargetID = -1;

Texture::Texture(uint16_t width, uint16_t height, Format format)
: mFormat(format),
mChannelSize(STRIDES[static_cast<uint8_t>(format)]),
mPixelSize(STRIDES[static_cast<uint8_t>(format)] * CHANNELS[static_cast<uint8_t>(format)])
{
mFormat = format;
mChannelSize = STRIDES[static_cast<uint8_t>(mFormat)];
mPixelSize = mChannelSize * CHANNELS[static_cast<uint8_t>(mFormat)];
Resize(width, height);
}

Expand Down
59 changes: 3 additions & 56 deletions Module/source/objects/RenderTarget.h
Original file line number Diff line number Diff line change
@@ -1,69 +1,16 @@
#pragma once

#include <cstdint>
#include "IRenderTarget.h"

#include "GarrysMod/Lua/Interface.h"
DLL_EXPORT int g_IRenderTargetID;

namespace RT
{
struct Pixel
class Texture : public ITexture
{
float r = 0, g = 0, b = 0, a = 0;

float& operator[](size_t i)
{
switch (i) {
case 0:
return r;
case 1:
return g;
case 2:
return b;
case 3:
return a;
default:
return r;
}
}
};

enum class Format : uint8_t
{
R8,
RG88,
RGB888,
RGBFFF,
Size,

Albedo = RGBFFF,
Normal = RGBFFF
};

static const uint8_t CHANNELS[static_cast<size_t>(Format::Size)] = {
1,
2,
3,
3
};

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

class Texture
{
private:
const Format mFormat;
const size_t mChannelSize, mPixelSize;
uint8_t* pBuffer = nullptr;
uint16_t mWidth = 0, mHeight = 0;

public:
static int id;

Texture(uint16_t width, uint16_t height, Format format);
~Texture();

Expand Down

1 comment on commit 5218e26

@yogwoggf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.