Skip to content

Commit 6fb4405

Browse files
authored
feat(webgl2): implement WebGL2 framebufferTextureLayer for layered texture rendering (#417)
1 parent 9747672 commit 6fb4405

File tree

7 files changed

+150
-1
lines changed

7 files changed

+150
-1
lines changed

src/client/graphics/webgl_context.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,12 @@ namespace endor
19361936
int level,
19371937
int layer)
19381938
{
1939-
NOT_IMPLEMENTED();
1939+
auto req = FramebufferTextureLayerCommandBufferRequest(static_cast<uint32_t>(target),
1940+
static_cast<uint32_t>(attachment),
1941+
texture != nullptr ? texture->id : 0,
1942+
level,
1943+
layer);
1944+
sendCommandBufferRequest(req);
19401945
}
19411946

19421947
string WebGL2Context::getActiveUniformBlockName(shared_ptr<WebGLProgram> program, int uniformBlockIndex)

src/client/script_bindings/webgl/webgl2_rendering_context.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "./buffer.hpp"
44
#include "./program.hpp"
55
#include "./vertex_array.hpp"
6+
#include "./texture.hpp"
67

78
namespace endor
89
{
@@ -417,6 +418,7 @@ namespace endor
417418
// Enhanced framebuffer operations
418419
ADD_WEBGL2_METHOD("blitFramebuffer", BlitFramebuffer)
419420
ADD_WEBGL2_METHOD("renderbufferStorageMultisample", RenderbufferStorageMultisample)
421+
ADD_WEBGL2_METHOD("framebufferTextureLayer", FramebufferTextureLayer)
420422

421423
// Enhanced texture operations
422424
ADD_WEBGL2_METHOD("texImage3D", TexImage3D)
@@ -1701,6 +1703,81 @@ namespace endor
17011703
args.GetReturnValue().SetUndefined();
17021704
}
17031705

1706+
void WebGL2RenderingContext::FramebufferTextureLayer(const v8::FunctionCallbackInfo<v8::Value> &args)
1707+
{
1708+
Isolate *isolate = args.GetIsolate();
1709+
HandleScope scope(isolate);
1710+
Local<Context> context = isolate->GetCurrentContext();
1711+
1712+
if (args.Length() < 5)
1713+
{
1714+
isolate->ThrowException(Exception::TypeError(
1715+
MakeMethodArgCountError(isolate, "framebufferTextureLayer", 5, args.Length())));
1716+
return;
1717+
}
1718+
if (!args[0]->IsNumber())
1719+
{
1720+
isolate->ThrowException(Exception::TypeError(
1721+
MakeMethodArgTypeError(isolate, "framebufferTextureLayer", 0, "number", args[0])));
1722+
return;
1723+
}
1724+
if (!args[1]->IsNumber())
1725+
{
1726+
isolate->ThrowException(Exception::TypeError(
1727+
MakeMethodArgTypeError(isolate, "framebufferTextureLayer", 1, "number", args[1])));
1728+
return;
1729+
}
1730+
if (!args[2]->IsNull() && !WebGLTexture::IsInstanceOf(isolate, args[2]))
1731+
{
1732+
isolate->ThrowException(Exception::TypeError(
1733+
MakeMethodArgTypeError(isolate, "framebufferTextureLayer", 2, "WebGLTexture or null", args[2])));
1734+
return;
1735+
}
1736+
if (!args[3]->IsNumber())
1737+
{
1738+
isolate->ThrowException(Exception::TypeError(
1739+
MakeMethodArgTypeError(isolate, "framebufferTextureLayer", 3, "number", args[3])));
1740+
return;
1741+
}
1742+
if (!args[4]->IsNumber())
1743+
{
1744+
isolate->ThrowException(Exception::TypeError(
1745+
MakeMethodArgTypeError(isolate, "framebufferTextureLayer", 4, "number", args[4])));
1746+
return;
1747+
}
1748+
1749+
client_graphics::WebGLFramebufferBindingTarget target;
1750+
{
1751+
int value = args[0]->Int32Value(context).ToChecked();
1752+
target = static_cast<client_graphics::WebGLFramebufferBindingTarget>(value);
1753+
}
1754+
client_graphics::WebGLFramebufferAttachment attachment;
1755+
{
1756+
int value = args[1]->Int32Value(context).ToChecked();
1757+
attachment = static_cast<client_graphics::WebGLFramebufferAttachment>(value);
1758+
}
1759+
int level = args[3]->Int32Value(context).ToChecked();
1760+
int layer = args[4]->Int32Value(context).ToChecked();
1761+
1762+
if (args[2]->IsNull())
1763+
{
1764+
handle()->framebufferTextureLayer(target, attachment, nullptr, level, layer);
1765+
}
1766+
else
1767+
{
1768+
auto textureObj = args[2].As<Object>();
1769+
auto textureBinding = WebGLTexture::Unwrap(isolate, textureObj);
1770+
if (textureBinding == nullptr || !textureBinding->hasData())
1771+
{
1772+
isolate->ThrowException(Exception::TypeError(
1773+
MakeMethodError(isolate, "framebufferTextureLayer", "Invalid WebGLTexture object")));
1774+
return;
1775+
}
1776+
handle()->framebufferTextureLayer(target, attachment, textureBinding->handle(), level, layer);
1777+
}
1778+
args.GetReturnValue().SetUndefined();
1779+
}
1780+
17041781
// Enhanced texture operations
17051782
void WebGL2RenderingContext::TexImage3D(const FunctionCallbackInfo<Value> &args)
17061783
{

src/client/script_bindings/webgl/webgl2_rendering_context.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ namespace endor
100100
// Enhanced framebuffer operations
101101
void BlitFramebuffer(const v8::FunctionCallbackInfo<v8::Value> &args);
102102
void RenderbufferStorageMultisample(const v8::FunctionCallbackInfo<v8::Value> &args);
103+
void FramebufferTextureLayer(const v8::FunctionCallbackInfo<v8::Value> &args);
103104

104105
// Enhanced texture operations
105106
void TexImage3D(const v8::FunctionCallbackInfo<v8::Value> &args);

src/common/command_buffers/details/buffer.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,52 @@ namespace commandbuffers
557557
uint32_t level;
558558
};
559559

560+
class FramebufferTextureLayerCommandBufferRequest final
561+
: public TrCommandBufferSimpleRequest<FramebufferTextureLayerCommandBufferRequest,
562+
COMMAND_BUFFER_FRAMEBUFFER_TEXTURE_LAYER_REQ>
563+
{
564+
public:
565+
FramebufferTextureLayerCommandBufferRequest() = delete;
566+
FramebufferTextureLayerCommandBufferRequest(uint32_t target, uint32_t attachment, uint32_t texture, uint32_t level, uint32_t layer)
567+
: TrCommandBufferSimpleRequest()
568+
, target(target)
569+
, attachment(attachment)
570+
, texture(texture)
571+
, level(level)
572+
, layer(layer)
573+
{
574+
}
575+
FramebufferTextureLayerCommandBufferRequest(const FramebufferTextureLayerCommandBufferRequest &that, bool clone = false)
576+
: TrCommandBufferSimpleRequest(that, clone)
577+
, target(that.target)
578+
, attachment(that.attachment)
579+
, texture(that.texture)
580+
, level(that.level)
581+
, layer(that.layer)
582+
{
583+
}
584+
585+
std::string toString(const char *line_prefix) const override
586+
{
587+
std::stringstream ss;
588+
ss << TrCommandBufferSimpleRequest::toString(line_prefix) << "("
589+
<< WebGLHelper::WebGLEnumToString(target) << ", "
590+
<< "attachment=" << WebGLHelper::WebGLFramebufferAttachmentToString(attachment) << ", "
591+
<< "texture=" << texture << ", "
592+
<< "level=" << level << ", "
593+
<< "layer=" << layer
594+
<< ")";
595+
return ss.str();
596+
}
597+
598+
public:
599+
uint32_t target;
600+
uint32_t attachment;
601+
uint32_t texture;
602+
uint32_t level;
603+
uint32_t layer;
604+
};
605+
560606
class CheckFramebufferStatusCommandBufferRequest final
561607
: public TrCommandBufferSimpleRequest<CheckFramebufferStatusCommandBufferRequest,
562608
COMMAND_BUFFER_CHECK_FRAMEBUFFER_STATUS_REQ>

src/common/command_buffers/macros.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
XX(BIND_FRAMEBUFFER, BindFramebufferCommandBufferRequest, "GL::BindFramebuffer") \
3333
XX(FRAMEBUFFER_RENDERBUFFER, FramebufferRenderbufferCommandBufferRequest, "GL::FramebufferRenderbuffer") \
3434
XX(FRAMEBUFFER_TEXTURE2D, FramebufferTexture2DCommandBufferRequest, "GL::FramebufferTexture2D") \
35+
XX(FRAMEBUFFER_TEXTURE_LAYER, FramebufferTextureLayerCommandBufferRequest, "GL::FramebufferTextureLayer") \
3536
XX(CHECK_FRAMEBUFFER_STATUS, CheckFramebufferStatusCommandBufferRequest, "GL::CheckFramebufferStatus") \
3637
XX(CREATE_RENDERBUFFER, CreateRenderbufferCommandBufferRequest, "GL::CreateRenderbuffer") \
3738
XX(DELETE_RENDERBUFFER, DeleteRenderbufferCommandBufferRequest, "GL::DeleteRenderbuffer") \

src/common/command_buffers/shared.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ namespace commandbuffers
7575
COMMAND_BUFFER_BIND_FRAMEBUFFER_REQ,
7676
COMMAND_BUFFER_FRAMEBUFFER_RENDERBUFFER_REQ,
7777
COMMAND_BUFFER_FRAMEBUFFER_TEXTURE2D_REQ,
78+
COMMAND_BUFFER_FRAMEBUFFER_TEXTURE_LAYER_REQ,
7879
COMMAND_BUFFER_CHECK_FRAMEBUFFER_STATUS_REQ,
7980
COMMAND_BUFFER_CHECK_FRAMEBUFFER_STATUS_RES,
8081
COMMAND_BUFFER_CREATE_RENDERBUFFER_REQ,

src/renderer/render_api_opengles.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,23 @@ class RHI_OpenGL : public TrRenderHardwareInterface
10281028
if (TR_UNLIKELY(CheckError(req, reqContentRenderer) != GL_NO_ERROR || options.printsCall))
10291029
PrintDebugInfo(req, nullptr, nullptr, options);
10301030
}
1031+
1032+
TR_OPENGL_FUNC void OnFramebufferTextureLayer(FramebufferTextureLayerCommandBufferRequest *req,
1033+
renderer::TrContentRenderer *reqContentRenderer,
1034+
ApiCallOptions &options)
1035+
{
1036+
auto &glObjectManager = reqContentRenderer->getContextGL()->ObjectManagerRef();
1037+
auto target = req->target;
1038+
auto attachment = req->attachment;
1039+
auto texture = glObjectManager.FindTexture(req->texture);
1040+
auto level = req->level;
1041+
auto layer = req->layer;
1042+
1043+
glFramebufferTextureLayer(target, attachment, texture, level, layer);
1044+
if (TR_UNLIKELY(CheckError(req, reqContentRenderer) != GL_NO_ERROR || options.printsCall))
1045+
PrintDebugInfo(req, nullptr, nullptr, options);
1046+
}
1047+
10311048
TR_OPENGL_FUNC void OnCheckFramebufferStatus(CheckFramebufferStatusCommandBufferRequest *req,
10321049
renderer::TrContentRenderer *reqContentRenderer,
10331050
ApiCallOptions &options)
@@ -2899,6 +2916,7 @@ bool RHI_OpenGL::ExecuteCommandBuffer(vector<commandbuffers::TrCommandBufferBase
28992916
ADD_COMMAND_BUFFER_HANDLER(BIND_FRAMEBUFFER, BindFramebufferCommandBufferRequest, BindFramebuffer)
29002917
ADD_COMMAND_BUFFER_HANDLER(FRAMEBUFFER_RENDERBUFFER, FramebufferRenderbufferCommandBufferRequest, FramebufferRenderbuffer)
29012918
ADD_COMMAND_BUFFER_HANDLER(FRAMEBUFFER_TEXTURE2D, FramebufferTexture2DCommandBufferRequest, FramebufferTexture2D)
2919+
ADD_COMMAND_BUFFER_HANDLER(FRAMEBUFFER_TEXTURE_LAYER, FramebufferTextureLayerCommandBufferRequest, FramebufferTextureLayer)
29022920
ADD_COMMAND_BUFFER_HANDLER(CHECK_FRAMEBUFFER_STATUS, CheckFramebufferStatusCommandBufferRequest, CheckFramebufferStatus)
29032921
ADD_COMMAND_BUFFER_HANDLER(CREATE_RENDERBUFFER, CreateRenderbufferCommandBufferRequest, CreateRenderbuffer)
29042922
ADD_COMMAND_BUFFER_HANDLER(DELETE_RENDERBUFFER, DeleteRenderbufferCommandBufferRequest, DeleteRenderbuffer)

0 commit comments

Comments
 (0)