Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenGL: Implement 3D Texture support #80363

Merged
merged 1 commit into from
Feb 7, 2024

Conversation

alula
Copy link
Contributor

@alula alula commented Aug 7, 2023

@akien-mga
Copy link
Member

Should fix #71029 I suppose?

@alula
Copy link
Contributor Author

alula commented Aug 7, 2023

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
Copy link
Member

clayjohn commented Aug 7, 2023

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
Copy link
Contributor Author

alula commented Aug 7, 2023

done

Copy link
Member

@clayjohn clayjohn left a comment

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

drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
@alula alula force-pushed the gl3-texture3d branch 2 times, most recently from b3942da to bfd8ca8 Compare September 8, 2023 04:49
@alula
Copy link
Contributor Author

alula commented Sep 8, 2023

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
Copy link
Member

clayjohn commented Sep 8, 2023

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) {

drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
@alula alula force-pushed the gl3-texture3d branch 3 times, most recently from ad3492d to 34c1030 Compare September 19, 2023 02:10
@alula
Copy link
Contributor Author

alula commented Sep 19, 2023

That should be everything I guess?

drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.h Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.h Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
Copy link
Contributor Author

@alula alula left a comment

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
Copy link
Contributor Author

alula commented Sep 19, 2023

GitHub moment

drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
@alula alula force-pushed the gl3-texture3d branch 2 times, most recently from d85a0f3 to 5badccf Compare September 27, 2023 14:22
@alula
Copy link
Contributor Author

alula commented Oct 1, 2023

rebased

@alula
Copy link
Contributor Author

alula commented Oct 19, 2023

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

@QbieShay
Copy link
Contributor

QbieShay commented Dec 6, 2023

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
Copy link
Contributor Author

alula commented Dec 6, 2023

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

@clayjohn
Copy link
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
Copy link
Contributor Author

alula commented Jan 18, 2024

@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
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
Copy link
Contributor Author

alula commented Feb 2, 2024

rebased

Copy link
Member

@clayjohn clayjohn left a comment

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.

drivers/gles3/storage/texture_storage.cpp Show resolved Hide resolved
@clayjohn clayjohn added the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Feb 5, 2024
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/texture_storage.cpp Outdated Show resolved Hide resolved
@alula
Copy link
Contributor Author

alula commented Feb 5, 2024

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
17 checks passed
@akien-mga
Copy link
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