Skip to content

Commit

Permalink
Expose RenderingSceneBuffers through ClassDB
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jul 10, 2023
1 parent b7c2fd2 commit 4f3b306
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 17 deletions.
31 changes: 31 additions & 0 deletions doc/classes/RenderSceneBuffers.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RenderSceneBuffers" inherits="RefCounted" version="4.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
A scene buffers object is created for each viewport for which 3D rendering is done.
</brief_description>
<description>
A scene buffers object is created for each viewport for which 3D rendering is done. It manages any additional buffers used during rendering and will discard buffers when the viewport is resized.
[b]Note:[/b] this is an internal rendering server object only exposed for GDExtension plugins.
</description>
<tutorials>
</tutorials>
<methods>
<method name="configure">
<return type="void" />
<param index="0" name="render_target" type="RID" />
<param index="1" name="internal_size" type="Vector2i" />
<param index="2" name="target_size" type="Vector2i" />
<param index="3" name="scaling_3d_mode" type="int" enum="RenderingServer.ViewportScaling3DMode" />
<param index="4" name="fsr_sharpness" type="float" />
<param index="5" name="texture_mipmap_bias" type="float" />
<param index="6" name="msaa" type="int" enum="RenderingServer.ViewportMSAA" />
<param index="7" name="screen_space_aa" type="int" enum="RenderingServer.ViewportScreenSpaceAA" />
<param index="8" name="use_taa" type="bool" />
<param index="9" name="use_debanding" type="bool" />
<param index="10" name="view_count" type="int" />
<description>
This method is called by the rendering server when the associated viewports configuration is changed. It will discard the old buffers and recreate the internal buffers used.
</description>
</method>
</methods>
</class>
49 changes: 49 additions & 0 deletions doc/classes/RenderSceneBuffersExtension.xml
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RenderSceneBuffersExtension" inherits="RenderSceneBuffers" version="4.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<methods>
<method name="_configure" qualifiers="virtual">
<return type="void" />
<param index="0" name="render_target" type="RID" />
<param index="1" name="internal_size" type="Vector2i" />
<param index="2" name="target_size" type="Vector2i" />
<param index="3" name="scaling_3d_mode" type="int" enum="RenderingServer.ViewportScaling3DMode" />
<param index="4" name="fsr_sharpness" type="float" />
<param index="5" name="texture_mipmap_bias" type="float" />
<param index="6" name="msaa" type="int" enum="RenderingServer.ViewportMSAA" />
<param index="7" name="screen_space_aa" type="int" enum="RenderingServer.ViewportScreenSpaceAA" />
<param index="8" name="use_taa" type="bool" />
<param index="9" name="use_debanding" type="bool" />
<param index="10" name="view_count" type="int" />
<description>
Implement this in GDExtension to handle the (re)sizing of a viewport.
</description>
</method>
<method name="_set_fsr_sharpness" qualifiers="virtual">
<return type="void" />
<param index="0" name="fsr_sharpness" type="float" />
<description>
Implement this in GDExtension to record a new FSR sharpness value.
</description>
</method>
<method name="_set_texture_mipmap_bias" qualifiers="virtual">
<return type="void" />
<param index="0" name="texture_mipmap_bias" type="float" />
<description>
Implement this in GDExtension to change the texture mipmap bias.
</description>
</method>
<method name="_set_use_debanding" qualifiers="virtual">
<return type="void" />
<param index="0" name="use_debanding" type="bool" />
<description>
Implement this in GDExtension to react to the debanding flag changing.
</description>
</method>
</methods>
</class>
128 changes: 128 additions & 0 deletions doc/classes/RenderSceneBuffersRD.xml
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RenderSceneBuffersRD" inherits="RenderSceneBuffers" version="4.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
This is the render scene buffer implementation for the rendering device based renderers.
</brief_description>
<description>
This object manages all 3D rendering buffers for the rendering device based renderers. An instance of this object is created for every viewport that has 3D rendering enabled.
All buffers are organised in [b]contexts[/b]. The default context is called [b]render_buffers[/b] and can contain amongst others the color buffer, depth buffer, velocity buffers, VRS density map and MSAA variants of these buffers.
Buffers are only guaranteed to exist during rendering of the viewport.
[b]Note:[/b] this is an internal rendering server object only exposed for GDExtension plugins.
</description>
<tutorials>
</tutorials>
<methods>
<method name="clear_context">
<return type="void" />
<param index="0" name="context" type="StringName" />
<description>
Frees all buffers related to this context.
</description>
</method>
<method name="create_texture">
<return type="RID" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<param index="2" name="data_format" type="int" enum="RenderingDevice.DataFormat" />
<param index="3" name="usage_bits" type="int" />
<param index="4" name="texture_samples" type="int" enum="RenderingDevice.TextureSamples" />
<param index="5" name="size" type="Vector2i" />
<param index="6" name="layers" type="int" />
<param index="7" name="mipmaps" type="int" />
<param index="8" name="unique" type="bool" />
<description>
Create a new texture with the given definition and cache this under the given name. Will return the existing texture if it already exists.
</description>
</method>
<method name="create_texture_from_format">
<return type="RID" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<param index="2" name="format" type="RDTextureFormat" />
<param index="3" name="view" type="RDTextureView" />
<param index="4" name="unique" type="bool" />
<description>
Create a new texture using the given format and view and cache this under the given name. Will return the existing texture if it already exists.
</description>
</method>
<method name="create_texture_view">
<return type="RID" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<param index="2" name="view_name" type="StringName" />
<param index="3" name="view" type="RDTextureView" />
<description>
Create a new texture view for an existing texture and cache this under the given view_name. Will return the existing teture view if it already exists. Will error if the source texture doesn't exist.
</description>
</method>
<method name="get_internal_size" qualifiers="const">
<return type="Vector2i" />
<description>
Returns the internal size of the render buffer (size before upscaling) with which textures are created by default.
</description>
</method>
<method name="get_render_target" qualifiers="const">
<return type="RID" />
<description>
Returns the render target associated with this buffers object.
</description>
</method>
<method name="get_texture" qualifiers="const">
<return type="RID" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<description>
Returns a cached texture with this name.
</description>
</method>
<method name="get_texture_format" qualifiers="const">
<return type="RDTextureFormat" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<description>
Returns the texture format information with which a cached texture was created.
</description>
</method>
<method name="get_texture_slice">
<return type="RID" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<param index="2" name="layer" type="int" />
<param index="3" name="mipmap" type="int" />
<param index="4" name="layers" type="int" />
<param index="5" name="mipmaps" type="int" />
<description>
Returns a specific slice (layer or mipmap) for a cached texture.
</description>
</method>
<method name="get_texture_slice_size">
<return type="Vector2i" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<param index="2" name="mipmap" type="int" />
<description>
Returns the texture size of a given slice of a cached texture.
</description>
</method>
<method name="get_use_taa" qualifiers="const">
<return type="bool" />
<description>
Returns [b]true[/b] if TAA is enabled.
</description>
</method>
<method name="get_view_count" qualifiers="const">
<return type="int" />
<description>
Returns the view count for the associated viewport.
</description>
</method>
<method name="has_texture" qualifiers="const">
<return type="bool" />
<param index="0" name="context" type="StringName" />
<param index="1" name="name" type="StringName" />
<description>
Returns [b]true[/b] if a cached texture exists for this name.
</description>
</method>
</methods>
</class>
6 changes: 6 additions & 0 deletions servers/register_server_types.cpp
Expand Up @@ -69,8 +69,10 @@
#include "physics_server_3d.h"
#include "physics_server_3d_wrap_mt.h"
#include "rendering/renderer_compositor.h"
#include "rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
#include "rendering/rendering_device.h"
#include "rendering/rendering_device_binds.h"
#include "rendering/storage/render_scene_buffers.h"
#include "rendering_server.h"
#include "servers/extensions/physics_server_2d_extension.h"
#include "servers/extensions/physics_server_3d_extension.h"
Expand Down Expand Up @@ -247,6 +249,10 @@ void register_server_types() {
GDREGISTER_CLASS(RDShaderFile);
GDREGISTER_CLASS(RDPipelineSpecializationConstant);

GDREGISTER_ABSTRACT_CLASS(RenderSceneBuffers);
GDREGISTER_CLASS(RenderSceneBuffersExtension);
GDREGISTER_CLASS(RenderSceneBuffersRD);

GDREGISTER_CLASS(CameraFeed);

GDREGISTER_ABSTRACT_CLASS(PhysicsDirectBodyState2D);
Expand Down
Expand Up @@ -44,15 +44,20 @@ RenderSceneBuffersRD::~RenderSceneBuffersRD() {

void RenderSceneBuffersRD::_bind_methods() {
ClassDB::bind_method(D_METHOD("has_texture", "context", "name"), &RenderSceneBuffersRD::has_texture);
// FIXME we can't pass RD::DataFormat, RD::TextureSamples and RD::TextureView in ClassDB, need to solve views differently...
// ClassDB::bind_method(D_METHOD("create_texture", "context", "name", "data_format", "usage_bits", "texture_samples", "size", "layers", "mipmaps", "unique"), &RenderSceneBuffersRD::create_texture);
// ClassDB::bind_method(D_METHOD("create_texture_from_format", "context", "name", "format", "view", "unique"), &RenderSceneBuffersRD::create_texture_from_format);
// ClassDB::bind_method(D_METHOD("create_texture_view", "context", "name", "view_name", "view"), &RenderSceneBuffersRD::has_texture);
ClassDB::bind_method(D_METHOD("create_texture", "context", "name", "data_format", "usage_bits", "texture_samples", "size", "layers", "mipmaps", "unique"), &RenderSceneBuffersRD::create_texture);
ClassDB::bind_method(D_METHOD("create_texture_from_format", "context", "name", "format", "view", "unique"), &RenderSceneBuffersRD::_create_texture_from_format);
ClassDB::bind_method(D_METHOD("create_texture_view", "context", "name", "view_name", "view"), &RenderSceneBuffersRD::_create_texture_view);
ClassDB::bind_method(D_METHOD("get_texture", "context", "name"), &RenderSceneBuffersRD::get_texture);
// ClassDB::bind_method(D_METHOD("get_texture_format", "context", "name"), &RenderSceneBuffersRD::get_texture_format);
ClassDB::bind_method(D_METHOD("get_texture_format", "context", "name"), &RenderSceneBuffersRD::_get_texture_format);
ClassDB::bind_method(D_METHOD("get_texture_slice", "context", "name", "layer", "mipmap", "layers", "mipmaps"), &RenderSceneBuffersRD::get_texture_slice);
ClassDB::bind_method(D_METHOD("get_texture_slice_size", "context", "name", "mipmap"), &RenderSceneBuffersRD::get_texture_slice_size);
ClassDB::bind_method(D_METHOD("clear_context", "context"), &RenderSceneBuffersRD::clear_context);

// Expose a few properties we're likely to use externally
ClassDB::bind_method(D_METHOD("get_render_target"), &RenderSceneBuffersRD::get_render_target);
ClassDB::bind_method(D_METHOD("get_view_count"), &RenderSceneBuffersRD::get_view_count);
ClassDB::bind_method(D_METHOD("get_internal_size"), &RenderSceneBuffersRD::get_internal_size);
ClassDB::bind_method(D_METHOD("get_use_taa"), &RenderSceneBuffersRD::get_use_taa);
}

void RenderSceneBuffersRD::update_sizes(NamedTexture &p_named_texture) {
Expand Down Expand Up @@ -244,6 +249,17 @@ RID RenderSceneBuffersRD::create_texture(const StringName &p_context, const Stri
return create_texture_from_format(p_context, p_texture_name, tf, RD::TextureView(), p_unique);
}

RID RenderSceneBuffersRD::_create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const Ref<RDTextureFormat> &p_texture_format, const Ref<RDTextureView> &p_view, bool p_unique) {
ERR_FAIL_COND_V(p_texture_format.is_null(), RID());

RD::TextureView texture_view;
if (p_view.is_valid()) { // only use when supplied, else default.
texture_view = p_view->base;
}

return create_texture_from_format(p_context, p_texture_name, p_texture_format->base, texture_view, p_unique);
}

RID RenderSceneBuffersRD::create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const RD::TextureFormat &p_texture_format, RD::TextureView p_view, bool p_unique) {
// TODO p_unique, if p_unique is true, this is a texture that can be shared. This will be implemented later as an optimization.

Expand Down Expand Up @@ -272,6 +288,15 @@ RID RenderSceneBuffersRD::create_texture_from_format(const StringName &p_context
return named_texture.texture;
}

RID RenderSceneBuffersRD::_create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName p_view_name, const Ref<RDTextureView> p_view) {
RD::TextureView texture_view;
if (p_view.is_valid()) { // only use when supplied, else default.
texture_view = p_view->base;
}

return create_texture_view(p_context, p_texture_name, p_view_name, texture_view);
}

RID RenderSceneBuffersRD::create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName p_view_name, RD::TextureView p_view) {
NTKey view_key(p_context, p_view_name);

Expand Down Expand Up @@ -310,6 +335,15 @@ RID RenderSceneBuffersRD::get_texture(const StringName &p_context, const StringN
return named_textures[key].texture;
}

Ref<RDTextureFormat> RenderSceneBuffersRD::_get_texture_format(const StringName &p_context, const StringName &p_texture_name) const {
Ref<RDTextureFormat> tf;
tf.instantiate();

tf->base = get_texture_format(p_context, p_texture_name);

return tf;
}

const RD::TextureFormat RenderSceneBuffersRD::get_texture_format(const StringName &p_context, const StringName &p_texture_name) const {
NTKey key(p_context, p_texture_name);

Expand Down
Expand Up @@ -36,6 +36,7 @@
#include "core/templates/hash_map.h"
#include "render_buffer_custom_data_rd.h"
#include "servers/rendering/rendering_device.h"
#include "servers/rendering/rendering_device_binds.h"
#include "servers/rendering/rendering_method.h"
#include "servers/rendering/storage/render_scene_buffers.h"

Expand Down Expand Up @@ -153,6 +154,11 @@ class RenderSceneBuffersRD : public RenderSceneBuffers {
protected:
static void _bind_methods();

// wrapper functions for access through ClassDB
RID _create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const Ref<RDTextureFormat> &p_texture_format, const Ref<RDTextureView> &p_view = Ref<RDTextureView>(), bool p_unique = true);
RID _create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName p_view_name, const Ref<RDTextureView> p_view = Ref<RDTextureView>());
Ref<RDTextureFormat> _get_texture_format(const StringName &p_context, const StringName &p_texture_name) const;

public:
RenderSceneBuffersRD();
virtual ~RenderSceneBuffersRD();
Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/rendering_device_binds.h
Expand Up @@ -53,7 +53,9 @@

class RDTextureFormat : public RefCounted {
GDCLASS(RDTextureFormat, RefCounted)

friend class RenderingDevice;
friend class RenderSceneBuffersRD;

RD::TextureFormat base;

Expand Down Expand Up @@ -91,6 +93,7 @@ class RDTextureView : public RefCounted {
GDCLASS(RDTextureView, RefCounted)

friend class RenderingDevice;
friend class RenderSceneBuffersRD;

RD::TextureView base;

Expand Down
17 changes: 12 additions & 5 deletions servers/rendering/storage/render_scene_buffers.cpp
Expand Up @@ -31,21 +31,28 @@
#include "render_scene_buffers.h"

void RenderSceneBuffers::_bind_methods() {
ClassDB::bind_method(D_METHOD("configure", "render_target", "internal_size", "target_size", "fsr_sharpness", "texture_mipmap_bias", "msaa", "screen_space_aa", "use_taa", "use_debanding", "view_count"), &RenderSceneBuffers::configure);
ClassDB::bind_method(D_METHOD("configure", "render_target", "internal_size", "target_size", "scaling_3d_mode", "fsr_sharpness", "texture_mipmap_bias", "msaa", "screen_space_aa", "use_taa", "use_debanding", "view_count"), &RenderSceneBuffers::configure);
}

void RenderSceneBuffers::configure(RID p_render_target, const Size2i p_internal_size, const Size2i p_target_size, RS::ViewportScaling3DMode p_scaling_3d_mode, float p_fsr_sharpness, float p_texture_mipmap_bias, RS::ViewportMSAA p_msaa, RenderingServer::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_taa, bool p_use_debanding, uint32_t p_view_count) {
void RenderSceneBuffersExtension::_bind_methods() {
GDVIRTUAL_BIND(_configure, "render_target", "internal_size", "target_size", "scaling_3d_mode", "fsr_sharpness", "texture_mipmap_bias", "msaa", "screen_space_aa", "use_taa", "use_debanding", "view_count");
GDVIRTUAL_BIND(_set_fsr_sharpness, "fsr_sharpness");
GDVIRTUAL_BIND(_set_texture_mipmap_bias, "texture_mipmap_bias");
GDVIRTUAL_BIND(_set_use_debanding, "use_debanding");
}

void RenderSceneBuffersExtension::configure(RID p_render_target, const Size2i p_internal_size, const Size2i p_target_size, RS::ViewportScaling3DMode p_scaling_3d_mode, float p_fsr_sharpness, float p_texture_mipmap_bias, RS::ViewportMSAA p_msaa, RenderingServer::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_taa, bool p_use_debanding, uint32_t p_view_count) {
GDVIRTUAL_CALL(_configure, p_render_target, p_internal_size, p_target_size, p_scaling_3d_mode, p_fsr_sharpness, p_texture_mipmap_bias, p_msaa, p_screen_space_aa, p_use_taa, p_use_debanding, p_view_count);
};

void RenderSceneBuffers::set_fsr_sharpness(float p_fsr_sharpness) {
void RenderSceneBuffersExtension::set_fsr_sharpness(float p_fsr_sharpness) {
GDVIRTUAL_CALL(_set_fsr_sharpness, p_fsr_sharpness);
}

void RenderSceneBuffers::set_texture_mipmap_bias(float p_texture_mipmap_bias) {
void RenderSceneBuffersExtension::set_texture_mipmap_bias(float p_texture_mipmap_bias) {
GDVIRTUAL_CALL(_set_texture_mipmap_bias, p_texture_mipmap_bias);
}

void RenderSceneBuffers::set_use_debanding(bool p_use_debanding) {
void RenderSceneBuffersExtension::set_use_debanding(bool p_use_debanding) {
GDVIRTUAL_CALL(_set_use_debanding, p_use_debanding);
}

0 comments on commit 4f3b306

Please sign in to comment.