Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agdk/agdktunnel/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ set(SIMPLE_RENDERER_SRCS
${SIMPLE_RENDERER_DIR}/renderer_resources.cpp
${SIMPLE_RENDERER_DIR}/renderer_shader_program_gles.cpp
${SIMPLE_RENDERER_DIR}/renderer_shader_program_vk.cpp
${SIMPLE_RENDERER_DIR}/renderer_texture.cpp
${SIMPLE_RENDERER_DIR}/renderer_texture_gles.cpp
${SIMPLE_RENDERER_DIR}/renderer_texture_vk.cpp
${SIMPLE_RENDERER_DIR}/renderer_uniform_buffer.cpp
Expand Down
136 changes: 136 additions & 0 deletions agdk/common/simple_renderer/renderer_texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "renderer_texture.h"
#include <algorithm>
#include "renderer_debug.h"

namespace simple_renderer {

// ASTC block dimensions, indexed from kTextureCompression_ASTC_LDR_4x4
static constexpr uint32_t kASTCBlockSizes[][2] = {
{4, 4}, {5, 4}, {5, 5}, {6, 5}, {6, 6}, {8, 5}, {8, 6}, {8, 8},
{10, 5}, {10, 6}, {10, 8}, {10, 10}, {12, 10}, {12, 12}
};

static bool IsASTC(const Texture::TextureCompressionType compression) {
return compression >= Texture::kTextureCompression_ASTC_LDR_4x4 &&
compression <= Texture::kTextureCompression_ASTC_LDR_12x12;
}

static bool IsETC2(const Texture::TextureCompressionType compression) {
return compression >= Texture::kTextureCompression_ETC2_RGB8 &&
compression <= Texture::kTextureCompression_ETC2_SRGB8_ALPHA8_EAC;
}

// Sizes are computed in 64 bits: width + block - 1 can overflow 32 bits, and so can the
// byte count (size_t is 32 bits on 32-bit ABIs)
uint64_t Texture::GetRequiredMipSize(const TextureFormat format,
const TextureCompressionType compression,
const uint32_t width, const uint32_t height) {
const uint64_t w = width;
const uint64_t h = height;
switch (format) {
case kTextureFormat_RGBA_8888:
return w * h * 4;
case kTextureFormat_RGB_888:
return w * h * 3;
case kTextureFormat_ASTC: {
if (!IsASTC(compression)) {
return 0;
}
// Every ASTC block is 16 bytes, partial blocks at the edges are whole blocks
const uint32_t* block = kASTCBlockSizes[compression - kTextureCompression_ASTC_LDR_4x4];
return ((w + block[0] - 1) / block[0]) * ((h + block[1] - 1) / block[1]) * 16;
}
case kTextureFormat_ETC2: {
if (!IsETC2(compression)) {
return 0;
}
// 4x4 blocks: 16 bytes with EAC alpha, 8 bytes otherwise
const uint64_t block_bytes =
(compression == kTextureCompression_ETC2_RGBA8_EAC ||
compression == kTextureCompression_ETC2_SRGB8_ALPHA8_EAC) ? 16 : 8;
return ((w + 3) / 4) * ((h + 3) / 4) * block_bytes;
}
default:
return 0;
}
}

bool Texture::ValidateCreationParams(const TextureCreationParams& params) {
if (params.format >= kTextureFormat_Count ||
params.compression_type >= kTextureCompression_Count ||
params.min_filter >= kMinFilter_Count || params.mag_filter >= kMagFilter_Count ||
params.wrap_s >= kWrapS_Count || params.wrap_t >= kWrapT_Count) {
RENDERER_ERROR("Texture: invalid format/compression/filter/wrap enum");
return false;
}
if (params.base_width == 0 || params.base_height == 0 || params.mip_count == 0 ||
params.texture_data == nullptr || params.texture_sizes == nullptr) {
RENDERER_ERROR("Texture: missing size or data (%ux%u, %u mips)", params.base_width,
params.base_height, params.mip_count);
return false;
}
if (params.mip_count > kMaxMipCount) {
RENDERER_ERROR("Texture: %u mips, the maximum is %u", params.mip_count, kMaxMipCount);
return false;
}
if ((params.format == kTextureFormat_RGBA_8888 || params.format == kTextureFormat_RGB_888) &&
params.compression_type != kTextureCompression_None) {
RENDERER_ERROR("Texture: uncompressed texture with compression %u",
params.compression_type);
return false;
}
if (params.format == kTextureFormat_ASTC && !IsASTC(params.compression_type)) {
RENDERER_ERROR("Texture: ASTC texture with non-ASTC compression %u",
params.compression_type);
return false;
}
if (params.format == kTextureFormat_ETC2 && !IsETC2(params.compression_type)) {
RENDERER_ERROR("Texture: ETC2 texture with non-ETC2 compression %u",
params.compression_type);
return false;
}
// A full mip chain ends at 1x1
uint32_t full_chain = 1;
for (uint32_t size = std::max(params.base_width, params.base_height); size > 1; size >>= 1) {
++full_chain;
}
if (params.mip_count > full_chain) {
RENDERER_ERROR("Texture: %u mips for %ux%u, a full chain is %u", params.mip_count,
params.base_width, params.base_height, full_chain);
return false;
}
// Uploads read the required byte count for each level, whatever size is passed in, so
// make sure that much data is there
uint32_t width = params.base_width;
uint32_t height = params.base_height;
for (uint32_t level = 0; level < params.mip_count; ++level) {
const uint64_t required = GetRequiredMipSize(params.format, params.compression_type,
width, height);
if (params.texture_sizes[level] < required) {
RENDERER_ERROR("Texture: mip %u has %u bytes, needs %llu", level,
params.texture_sizes[level], static_cast<unsigned long long>(required));
return false;
}
width = (width > 1) ? (width >> 1) : 1;
height = (height > 1) ? (height >> 1) : 1;
}
return true;
}

} // namespace simple_renderer
41 changes: 32 additions & 9 deletions agdk/common/simple_renderer/renderer_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,42 @@ class Texture {
*/
virtual ~Texture() {}

/**
* @brief Check that texture creation parameters are usable: enums in range, a
* compression type that matches the format (`kTextureCompression_None` for uncompressed
* formats), nonzero dimensions, between 1 and `kMaxMipCount` mip levels and no more than
* a full mip chain, and enough data for each level. Logs the problem if not.
* @param params The creation parameters
* @return true if the parameters are valid
*/
static bool ValidateCreationParams(const TextureCreationParams& params);

/**
* @brief Get the number of bytes of texel data a mip level of the given size needs.
* @return The required byte count, or 0 for an invalid format/compression combination.
*/
static uint64_t GetRequiredMipSize(TextureFormat format, TextureCompressionType compression,
uint32_t width, uint32_t height);

protected:
/** @brief Maximum number of mip levels a texture can have */
static constexpr uint32_t kMaxMipCount = 16;

/**
* @brief Whether the creation parameters passed validation. If not, the texture has no
* mip levels and subclasses must not create any API objects.
*/
bool HasValidCreationParams() const { return valid_creation_params_; }

Texture(const TextureCreationParams& params)
: texture_format_(params.format),
texture_compression_type_(params.compression_type),
texture_base_width_(params.base_width),
texture_base_height_(params.base_height),
texture_debug_name_("noname)") {
texture_mip_count_ = params.mip_count;
if (params.mip_count > kMaxMipCount) {
texture_mip_count_ = kMaxMipCount;
}
if (params.texture_sizes == nullptr) {
texture_mip_count_ = 0;
}
valid_creation_params_ = ValidateCreationParams(params);
// An invalid texture reports no mip levels (and no level sizes)
texture_mip_count_ = valid_creation_params_ ? params.mip_count : 0;
uint32_t i = 0;
while (i < texture_mip_count_) {
texture_sizes_[i] = params.texture_sizes[i];
Expand All @@ -274,19 +296,20 @@ class Texture {
Texture()
: texture_format_(kTextureFormat_Count),
texture_compression_type_(kTextureCompression_Count),
texture_mip_count_(0) {
texture_mip_count_(0),
valid_creation_params_(false) {
for (uint32_t i = 0; i < kMaxMipCount; ++i) {
texture_sizes_[i] = 0;
}
}

static constexpr uint32_t kMaxMipCount = 16;
TextureFormat texture_format_;
TextureCompressionType texture_compression_type_;
uint32_t texture_base_width_;
uint32_t texture_base_height_;
uint32_t texture_mip_count_;
size_t texture_sizes_[kMaxMipCount];
bool valid_creation_params_;

std::string texture_debug_name_;
};
Expand Down
57 changes: 3 additions & 54 deletions agdk/common/simple_renderer/renderer_texture_gles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,68 +84,17 @@ static constexpr GLint kGLWrapT[Texture::kWrapT_Count] = {
GL_MIRRORED_REPEAT
};

// Returns true if the creation parameters are usable: enums in range, the compression type
// matches a compressed format, and every mip level has data of a plausible size
static bool ValidateTextureParams(const Texture::TextureCreationParams& params,
const uint32_t mip_count) {
if (params.format >= Texture::kTextureFormat_Count ||
params.min_filter >= Texture::kMinFilter_Count ||
params.mag_filter >= Texture::kMagFilter_Count ||
params.wrap_s >= Texture::kWrapS_Count || params.wrap_t >= Texture::kWrapT_Count) {
RENDERER_ERROR("TextureGLES: invalid format/filter/wrap enum");
return false;
}
if (params.base_width == 0 || params.base_height == 0 || mip_count == 0 ||
params.texture_data == nullptr || params.texture_sizes == nullptr) {
RENDERER_ERROR("TextureGLES: missing size or data (%ux%u, %u mips)", params.base_width,
params.base_height, params.mip_count);
return false;
}
if (params.format == Texture::kTextureFormat_ASTC &&
!(params.compression_type >= Texture::kTextureCompression_ASTC_LDR_4x4 &&
params.compression_type <= Texture::kTextureCompression_ASTC_LDR_12x12)) {
RENDERER_ERROR("TextureGLES: ASTC texture with non-ASTC compression %u",
params.compression_type);
return false;
}
if (params.format == Texture::kTextureFormat_ETC2 &&
!(params.compression_type >= Texture::kTextureCompression_ETC2_RGB8 &&
params.compression_type <= Texture::kTextureCompression_ETC2_SRGB8_ALPHA8_EAC)) {
RENDERER_ERROR("TextureGLES: ETC2 texture with non-ETC2 compression %u",
params.compression_type);
return false;
}
// glTexImage2D reads width * height * bytes per pixel for each uncompressed level,
// regardless of the size passed in, so make sure that much data is there
if (params.format == Texture::kTextureFormat_RGBA_8888 ||
params.format == Texture::kTextureFormat_RGB_888) {
const size_t bytes_per_pixel = (params.format == Texture::kTextureFormat_RGBA_8888) ? 4 : 3;
size_t width = params.base_width;
size_t height = params.base_height;
for (uint32_t level = 0; level < mip_count; ++level) {
if (params.texture_sizes[level] < width * height * bytes_per_pixel) {
RENDERER_ERROR("TextureGLES: mip %u has %u bytes, needs %zu", level,
params.texture_sizes[level], width * height * bytes_per_pixel);
return false;
}
width = (width > 1) ? (width >> 1) : 1;
height = (height > 1) ? (height >> 1) : 1;
}
}
return true;
}

TextureGLES::TextureGLES(const Texture::TextureCreationParams& params)
: Texture(params) {
texture_object_ = 0;
// Use the base class mip count, which is clamped to the supported maximum
const uint32_t mip_count = GetMipCount();
const bool valid_params = ValidateTextureParams(params, mip_count);
// The base class validated the parameters; an invalid texture has no mip levels
const bool valid_params = HasValidCreationParams();
RENDERER_ASSERT(valid_params);
if (!valid_params) {
// Leave texture_object_ as 0; binding it unbinds any texture
return;
}
const uint32_t mip_count = GetMipCount();

// Don't disturb the caller's texture binding or pixel unpack state
GLint previous_texture = 0;
Expand Down
Loading