Skip to content

Commit

Permalink
Add custom texture create function
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jul 26, 2023
1 parent 202e4b2 commit 63d6e9c
Show file tree
Hide file tree
Showing 26 changed files with 1,130 additions and 3 deletions.
7 changes: 7 additions & 0 deletions doc/classes/RenderingDevice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,13 @@
[b]Note:[/b] [param texture] requires the [constant TEXTURE_USAGE_CAN_COPY_FROM_BIT] to be retrieved. Otherwise, an error is printed and a empty [PackedByteArray] is returned.
</description>
</method>
<method name="texture_get_format">
<return type="RDTextureFormat" />
<param index="0" name="texture" type="RID" />
<description>
Returns the data format used to create this texture.
</description>
</method>
<method name="texture_get_native_handle">
<return type="int" />
<param index="0" name="texture" type="RID" />
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3281,6 +3281,13 @@
[b]Note:[/b] The [param texture] must have the same width, height, depth and format as the current texture data. Otherwise, an error will be printed and the original texture won't be modified. If you need to use different width, height, depth or format, use [method texture_replace] instead.
</description>
</method>
<method name="texture_get_format" qualifiers="const">
<return type="int" enum="Image.Format" />
<param index="0" name="texture" type="RID" />
<description>
Returns the [enum Image.Format] for the texture.
</description>
</method>
<method name="texture_get_native_handle" qualifiers="const">
<return type="int" />
<param index="0" name="texture" type="RID" />
Expand Down Expand Up @@ -3319,6 +3326,14 @@
[i]Deprecated.[/i] ProxyTexture was removed in Godot 4, so this method cannot be used anymore.
</description>
</method>
<method name="texture_rd_create">
<return type="RID" />
<param index="0" name="rd_texture" type="RID" />
<param index="1" name="layer_type" type="int" enum="RenderingServer.TextureLayeredType" default="0" />
<description>
Creates a new texture object based on a texture created directly on the [RenderingDevice]. If the texture contains layers, [param layer_type] is used to define the layer type.
</description>
</method>
<method name="texture_replace">
<return type="void" />
<param index="0" name="texture" type="RID" />
Expand Down
11 changes: 11 additions & 0 deletions doc/classes/Texture2DArrayRD.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="Texture2DArrayRD" inherits="TextureLayeredRD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Texture Array for 2D that is bound to a texture created on the [RenderingDevice].
</brief_description>
<description>
This texture array class allows you to use a 2D array texture created directly on the [RenderingDevice] as a texture for materials, meshes, etc.
</description>
<tutorials>
</tutorials>
</class>
17 changes: 17 additions & 0 deletions doc/classes/Texture2DRD.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="Texture2DRD" inherits="Texture2D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Texture for 2D that is bound to a texture created on the [RenderingDevice].
</brief_description>
<description>
This texture class allows you to use a 2D texture created directly on the [RenderingDevice] as a texture for materials, meshes, etc.
</description>
<tutorials>
</tutorials>
<members>
<member name="resource_local_to_scene" type="bool" setter="set_local_to_scene" getter="is_local_to_scene" overrides="Resource" default="false" />
<member name="texture_rd_rid" type="RID" setter="set_texture_rd_rid" getter="get_texture_rd_rid" default="RID()">
The RID of the texture object created on the [RenderingDevice].
</member>
</members>
</class>
16 changes: 16 additions & 0 deletions doc/classes/Texture3DRD.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="Texture3DRD" inherits="Texture3D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Texture for 3D that is bound to a texture created on the [RenderingDevice].
</brief_description>
<description>
This texture class allows you to use a 3D texture created directly on the [RenderingDevice] as a texture for materials, meshes, etc.
</description>
<tutorials>
</tutorials>
<members>
<member name="texture_rd_rid" type="RID" setter="set_texture_rd_rid" getter="get_texture_rd_rid" default="RID()">
The RID of the texture object created on the [RenderingDevice].
</member>
</members>
</class>
11 changes: 11 additions & 0 deletions doc/classes/TextureCubemapArrayRD.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="TextureCubemapArrayRD" inherits="TextureLayeredRD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Texture Array for Cubemaps that is bound to a texture created on the [RenderingDevice].
</brief_description>
<description>
This texture class allows you to use a cubemap array texture created directly on the [RenderingDevice] as a texture for materials, meshes, etc.
</description>
<tutorials>
</tutorials>
</class>
11 changes: 11 additions & 0 deletions doc/classes/TextureCubemapRD.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="TextureCubemapRD" inherits="TextureLayeredRD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Texture for Cubemap that is bound to a texture created on the [RenderingDevice].
</brief_description>
<description>
This texture class allows you to use a cubemap texture created directly on the [RenderingDevice] as a texture for materials, meshes, etc.
</description>
<tutorials>
</tutorials>
</class>
16 changes: 16 additions & 0 deletions doc/classes/TextureLayeredRD.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="TextureLayeredRD" inherits="TextureLayered" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Abstract base class for layered texture RD types.
</brief_description>
<description>
Base class for [Texture2DArrayRD], [TextureCubemapRD] and [TextureCubemapArrayRD]. Cannot be used directly, but contains all the functions necessary for accessing the derived resource types.
</description>
<tutorials>
</tutorials>
<members>
<member name="texture_rd_rid" type="RID" setter="set_texture_rd_rid" getter="get_texture_rd_rid" default="RID()">
The RID of the texture object created on the [RenderingDevice].
</member>
</members>
</class>
3 changes: 3 additions & 0 deletions drivers/gles3/storage/texture_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,9 @@ Size2 TextureStorage::texture_size_with_proxy(RID p_texture) {
}
}

void TextureStorage::texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type) {
}

RID TextureStorage::texture_get_rd_texture(RID p_texture, bool p_srgb) const {
return RID();
}
Expand Down
3 changes: 2 additions & 1 deletion drivers/gles3/storage/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,12 @@ class TextureStorage : public RendererTextureStorage {

virtual Size2 texture_size_with_proxy(RID p_proxy) override;

virtual void texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY) override;
virtual RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) const override;
virtual uint64_t texture_get_native_handle(RID p_texture, bool p_srgb = false) const override;

void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
Image::Format texture_get_format(RID p_texture) const;
virtual Image::Format texture_get_format(RID p_texture) const override;
uint32_t texture_get_texid(RID p_texture) const;
uint32_t texture_get_width(RID p_texture) const;
uint32_t texture_get_height(RID p_texture) const;
Expand Down
23 changes: 23 additions & 0 deletions drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,29 @@ bool RenderingDeviceVulkan::texture_is_valid(RID p_texture) {
return texture_owner.owns(p_texture);
}

RD::TextureFormat RenderingDeviceVulkan::texture_get_format(RID p_texture) {
_THREAD_SAFE_METHOD_

Texture *tex = texture_owner.get_or_null(p_texture);
ERR_FAIL_COND_V(!tex, TextureFormat());

TextureFormat tf;

tf.format = tex->format;
tf.width = tex->width;
tf.height = tex->height;
tf.depth = tex->depth;
tf.array_layers = tex->layers;
tf.mipmaps = tex->mipmaps;
tf.texture_type = tex->type;
tf.samples = tex->samples;
tf.usage_bits = tex->usage_flags;
tf.shareable_formats = tex->allowed_shared_formats;
tf.is_resolve_buffer = tex->is_resolve_buffer;

return tf;
}

Size2i RenderingDeviceVulkan::texture_size(RID p_texture) {
_THREAD_SAFE_METHOD_

Expand Down
1 change: 1 addition & 0 deletions drivers/vulkan/rendering_device_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ class RenderingDeviceVulkan : public RenderingDevice {
virtual bool texture_is_format_supported_for_usage(DataFormat p_format, BitField<RenderingDevice::TextureUsageBits> p_usage) const;
virtual bool texture_is_shared(RID p_texture);
virtual bool texture_is_valid(RID p_texture);
virtual TextureFormat texture_get_format(RID p_texture);
virtual Size2i texture_size(RID p_texture);
virtual uint64_t texture_get_native_handle(RID p_texture);

Expand Down
9 changes: 9 additions & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
#include "scene/resources/text_line.h"
#include "scene/resources/text_paragraph.h"
#include "scene/resources/texture.h"
#include "scene/resources/texture_rd.h"
#include "scene/resources/theme.h"
#include "scene/resources/tile_set.h"
#include "scene/resources/video_stream.h"
Expand Down Expand Up @@ -887,6 +888,14 @@ void register_scene_types() {
GDREGISTER_CLASS(PlaceholderCubemap);
GDREGISTER_CLASS(PlaceholderCubemapArray);

// These classes are part of renderer_rd
GDREGISTER_CLASS(Texture2DRD);
GDREGISTER_ABSTRACT_CLASS(TextureLayeredRD);
GDREGISTER_CLASS(Texture2DArrayRD);
GDREGISTER_CLASS(TextureCubemapRD);
GDREGISTER_CLASS(TextureCubemapArrayRD);
GDREGISTER_CLASS(Texture3DRD);

GDREGISTER_CLASS(Animation);
GDREGISTER_CLASS(AnimationLibrary);

Expand Down
2 changes: 1 addition & 1 deletion scene/resources/image_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ int ImageTexture::get_height() const {

RID ImageTexture::get_rid() const {
if (texture.is_null()) {
//we are in trouble, create something temporary
// We are in trouble, create something temporary.
texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
}
return texture;
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/portable_compressed_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ int PortableCompressedTexture2D::get_height() const {

RID PortableCompressedTexture2D::get_rid() const {
if (texture.is_null()) {
//we are in trouble, create something temporary
// We are in trouble, create something temporary.
texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
}
return texture;
Expand Down
Loading

0 comments on commit 63d6e9c

Please sign in to comment.