Skip to content

OpenGL: Implement 3D Texture support#80363

Merged
akien-mga merged 1 commit into
godotengine:masterfrom
alula:gl3-texture3d
Feb 7, 2024
Merged

OpenGL: Implement 3D Texture support#80363
akien-mga merged 1 commit into
godotengine:masterfrom
alula:gl3-texture3d

Conversation

@alula

@alula alula commented Aug 7, 2023

Copy link
Copy Markdown
Contributor

@alula alula requested a review from a team as a code owner August 7, 2023 08:42
@akien-mga akien-mga added this to the 3.x milestone Aug 7, 2023
@akien-mga

Copy link
Copy Markdown
Member

Should fix #71029 I suppose?

@alula

alula commented Aug 7, 2023

Copy link
Copy Markdown
Contributor Author

This is for 4.x compatibility renderer, so 3.x milestone sounds like a mistake...

@akien-mga akien-mga modified the milestones: 3.x, 4.2 Aug 7, 2023
@clayjohn

clayjohn commented Aug 7, 2023

Copy link
Copy Markdown
Member

I am getting the following error just opening up the project manager with this PR:

ERROR: Image has included mipmaps
   at: texture_3d_initialize (drivers/gles3/storage/texture_storage.cpp:816)
ERROR: Image has included mipmaps
   at: texture_3d_initialize (drivers/gles3/storage/texture_storage.cpp:816)

I traced it back to our default texture generation which reuses images that already have mipmaps to create the default images. The following diff resolves the errors:

diff --git a/drivers/gles3/storage/texture_storage.cpp b/drivers/gles3/storage/texture_storage.cpp
index 268043f99f..ef16a52b3a 100644
--- a/drivers/gles3/storage/texture_storage.cpp
+++ b/drivers/gles3/storage/texture_storage.cpp
@@ -77,19 +77,24 @@ TextureStorage::TextureStorage() {
 			default_gl_textures[DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE] = texture_allocate();
 			texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE], images, RS::TEXTURE_LAYERED_2D_ARRAY);
 
-			for (int i = 0; i < 3; i++) {
+			for (int i = 0; i < 5; i++) {
 				images.push_back(image);
 			}
 
-			default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE] = texture_allocate();
-			texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE], image->get_format(), 4, 4, 4, false, images);
+			default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE] = texture_allocate();
+			texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE], images, RS::TEXTURE_LAYERED_CUBEMAP);
+		}
+
+		{
+			Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
+			image->fill(Color(1, 1, 1, 1));
 
-			for (int i = 0; i < 2; i++) {
+			Vector<Ref<Image>> images;
+			for (int i = 0; i < 4; i++) {
 				images.push_back(image);
 			}
-
-			default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE] = texture_allocate();
-			texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE], images, RS::TEXTURE_LAYERED_CUBEMAP);
+			default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE] = texture_allocate();
+			texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE], image->get_format(), 4, 4, 4, false, images);
 		}
 
 		{ // black
@@ -101,19 +106,23 @@ TextureStorage::TextureStorage() {
 			texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_BLACK], image);
 
 			Vector<Ref<Image>> images;
-
-			for (int i = 0; i < 4; i++) {
+			for (int i = 0; i < 6; i++) {
 				images.push_back(image);
 			}
+			default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK] = texture_allocate();
+			texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK], images, RS::TEXTURE_LAYERED_CUBEMAP);
+		}
 
-			default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK] = texture_allocate();
-			texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK], image->get_format(), 4, 4, 4, false, images);
+		{
+			Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
+			image->fill(Color(0, 0, 0, 1));
 
-			for (int i = 0; i < 2; i++) {
+			Vector<Ref<Image>> images;
+			for (int i = 0; i < 4; i++) {
 				images.push_back(image);
 			}
-			default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK] = texture_allocate();
-			texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK], images, RS::TEXTURE_LAYERED_CUBEMAP);
+			default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK] = texture_allocate();
+			texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK], image->get_format(), 4, 4, 4, false, images);
 		}
 
 		{ // transparent black
@@ -813,6 +822,7 @@ void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format p_format

@alula

alula commented Aug 7, 2023

Copy link
Copy Markdown
Contributor Author

done

@clayjohn clayjohn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks mostly good. I left a comment on the validation code as it will need some changes.

Another thing to consider is implementing texture_3d_get() without it, NoiseTexture3Ds crash on creation. Please let me know if you would like some help with implementing texture_3d_get() as it is a little more complex

Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
@alula alula force-pushed the gl3-texture3d branch 2 times, most recently from b3942da to bfd8ca8 Compare September 8, 2023 04:49
@alula

alula commented Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

NoiseTexture3Ds seem to work fine now and implementing texture_2d_layer_get() and texture_3d_get() is straightforward on OpenGL due to glGetTexImage() availability but a bit tricky on GLES.

For me it looks like it would need a variant of the copy.glsl shader with sampler3D, what would be your preferred way to implement that?

@clayjohn

clayjohn commented Sep 8, 2023

Copy link
Copy Markdown
Member

I would do something similar to how it was done in 3.x

if (texture->type == VS::TEXTURE_TYPE_2D_ARRAY || texture->type == VS::TEXTURE_TYPE_3D) {

Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
@alula alula force-pushed the gl3-texture3d branch 3 times, most recently from ad3492d to 34c1030 Compare September 19, 2023 02:10
@alula

alula commented Sep 19, 2023

Copy link
Copy Markdown
Contributor Author

That should be everything I guess?

Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.h Outdated
Comment thread drivers/gles3/storage/texture_storage.h Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated

@alula alula left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've applied the code review changes in separate commit, but it's not showing up here for me for some reason?

@alula

alula commented Sep 19, 2023

Copy link
Copy Markdown
Contributor Author

GitHub moment

Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
@alula alula force-pushed the gl3-texture3d branch 2 times, most recently from d85a0f3 to 5badccf Compare September 27, 2023 14:22
@alula

alula commented Oct 1, 2023

Copy link
Copy Markdown
Contributor Author

rebased

@alula

alula commented Oct 19, 2023

Copy link
Copy Markdown
Contributor Author

Fixed an issue I found while testing my project on Web target, any updates on this PR by the way?

Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
@QbieShay

QbieShay commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

Hey 👋

This has been postponed to 4.3 at the time of 4.2, are you still available to update this PR @alula ?

Please let us know so we can organise and align on the implementation ^^

@alula

alula commented Dec 6, 2023

Copy link
Copy Markdown
Contributor Author

Oh yes, I'm still around, I'll update it soon!

@clayjohn

Copy link
Copy Markdown
Member

Hi! This has become a blocker for other work that needs to get finished on the compatibility backend. So I'm going to make the necessary changes and push directly to this PR

@alula

alula commented Jan 18, 2024

Copy link
Copy Markdown
Contributor Author

@clayjohn alright, I've been busy with irl stuff and went on trip after my last comment and totally forgot about finishing this PR, oops... 😅

@clayjohn

Copy link
Copy Markdown
Member

@clayjohn alright, I've been busy with irl stuff and went on trip after my last comment and totally forgot about finishing this PR, oops... 😅

No worries!

@alula

alula commented Feb 2, 2024

Copy link
Copy Markdown
Contributor Author

rebased

@clayjohn clayjohn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hey, thanks for rebasing and updating based on my last comment.

Right after commenting earlier I realized we could get by a little longer without 3D textures. So thank you for taking the initiative back and finishing this PR.

It looks great to me overall. The final step before merging it is to "squash" all the commits into one commit. We describe how to that here: https://docs.godotengine.org/en/latest/contributing/workflow/pr_workflow.html#the-interactive-rebase

We like to have only one commit per PR in order to keep our git history clean and easy to navigate. Please let me know if you run into any trouble or want me to finish it off for you. I feel bad saying I would take over and then leaving it to you to finish again.

Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
@clayjohn clayjohn added the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Feb 5, 2024
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
Comment thread drivers/gles3/storage/texture_storage.cpp Outdated
@alula

alula commented Feb 5, 2024

Copy link
Copy Markdown
Contributor Author

done

Hey, thanks for rebasing and updating based on my last comment.

Right after commenting earlier I realized we could get by a little longer without 3D textures. So thank you for taking the initiative back and finishing this PR.

It looks great to me overall. The final step before merging it is to "squash" all the commits into one commit. We describe how to that here: https://docs.godotengine.org/en/latest/contributing/workflow/pr_workflow.html#the-interactive-rebase

We like to have only one commit per PR in order to keep our git history clean and easy to navigate. Please let me know if you run into any trouble or want me to finish it off for you. I feel bad saying I would take over and then leaving it to you to finish again.

@akien-mga akien-mga merged commit 40eb988 into godotengine:master Feb 7, 2024
@akien-mga

Copy link
Copy Markdown
Member

Thanks!

@clayjohn clayjohn removed the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenGL: TextureArrays and Texture3D samplers not implemented yet

5 participants