Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix creating VRAM-compressed 3D textures with mipmaps

BlueCube33106eb8a2b2acc1
diff --git a/core/io/image.cpp b/core/io/image.cpp
index b0797e96a7c..569e719979f 100644
--- a/core/io/image.cpp
+++ b/core/io/image.cpp
@@ -425,6 +425,9 @@ Image::Image3DValidateError Image::validate_3d_image(Image::Format p_format, int
int d = p_depth;
int arr_ofs = 0;
+ // We use the format block size to ensure block-compressed images can be used as mipmaps.
+ // This is a hacky solution that should be handled better in the future.
+ int block = get_format_block_size(p_format);
while (true) {
for (int i = 0; i < d; i++) {
@@ -438,7 +441,7 @@ Image::Image3DValidateError Image::validate_3d_image(Image::Format p_format, int
if (p_images[idx]->get_format() != p_format) {
return VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH;
}
- if (p_images[idx]->get_width() != w || p_images[idx]->get_height() != h) {
+ if (MAX(p_images[idx]->get_width(), block) != MAX(w, block) || MAX(p_images[idx]->get_height(), block) != MAX(h, block)) {
return VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH;
}
if (p_images[idx]->has_mipmaps()) {
diff --git a/modules/etcpak/image_compress_etcpak.cpp b/modules/etcpak/image_compress_etcpak.cpp
index d7c7382916a..7c48e9622a5 100644
--- a/modules/etcpak/image_compress_etcpak.cpp
+++ b/modules/etcpak/image_compress_etcpak.cpp
@@ -190,11 +190,6 @@ void _compress_etcpak(EtcpakType p_compress_type, Image *r_img) {
r_img->resize(width, height, Image::INTERPOLATE_NEAREST);
}
- // Multiple-of-4 should be guaranteed by above.
- // However, power-of-two 3d textures will create Nx2 and Nx1 mipmap levels,
- // which are individually compressed Image objects that violate the above rule.
- // Hence, we allow Nx1 and Nx2 images through without forcing to multiple-of-4.
-
// Create the buffer for compressed image data.
Vector<uint8_t> dest_data;
dest_data.resize(Image::get_image_data_size(width, height, target_format, has_mipmaps));
diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp
index f9753a6e097..da9500f386b 100644
--- a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp
+++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp
@@ -1180,7 +1180,7 @@ void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format p_format
TextureToRDFormat ret_format;
Image::Format validated_format = Image::FORMAT_MAX;
Vector<uint8_t> all_data;
- uint32_t mipmap_count = 0;
+ uint32_t mipmap_count = 1;
Vector<Texture::BufferSlice3D> slices;
{
Vector<Ref<Image>> images;
@@ -1199,7 +1199,6 @@ void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format p_format
all_data.resize(all_data_size); //consolidate all data here
uint32_t offset = 0;
- Size2i prev_size;
for (int i = 0; i < p_data.size(); i++) {
uint32_t s = images[i]->get_data().size();
@@ -1215,10 +1214,21 @@ void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format p_format
offset += s;
Size2i img_size(images[i]->get_width(), images[i]->get_height());
- if (img_size != prev_size) {
+ }
+
+ // Calculate the mipmap count again.
+ if (p_mipmaps) {
+ int mmw = p_width;
+ int mmh = p_height;
+ int mmd = p_depth;
+
+ while (mmw > 1 || mmh > 1 || mmd > 1) {
mipmap_count++;
+
+ mmw = MAX(1, mmw >> 1);
+ mmh = MAX(1, mmh >> 1);
+ mmd = MAX(1, mmd >> 1);
}
- prev_size = img_size;
}
}