mirror of https://github.com/auygun/kaliber.git
Move Image::Format enum to renderer_types.h
And make it enum class
This commit is contained in:
parent
b86a27531b
commit
0da56dd552
|
@ -95,13 +95,14 @@ void Image::Copy(const Image& other) {
|
|||
}
|
||||
|
||||
bool Image::CreateMip(const Image& other) {
|
||||
if (other.width_ <= 1 || other.height_ <= 1 || other.GetFormat() != kRGBA32)
|
||||
if (other.width_ <= 1 || other.height_ <= 1 ||
|
||||
other.GetFormat() != ImageFormat::kRGBA32)
|
||||
return false;
|
||||
|
||||
// Reduce the dimensions.
|
||||
width_ = std::max(other.width_ >> 1, 1);
|
||||
height_ = std::max(other.height_ >> 1, 1);
|
||||
format_ = kRGBA32;
|
||||
format_ = ImageFormat::kRGBA32;
|
||||
buffer_.reset((uint8_t*)AlignedAlloc(GetSize(), 16));
|
||||
|
||||
// If the width isn't perfectly divisable with two, then we end up skewing
|
||||
|
@ -214,21 +215,12 @@ bool Image::Load(const std::string& file_name) {
|
|||
return !!buffer_;
|
||||
}
|
||||
|
||||
bool Image::IsCompressed() const {
|
||||
return IsCompressedFormat(format_);
|
||||
}
|
||||
|
||||
size_t Image::GetSize() const {
|
||||
switch (format_) {
|
||||
case kRGBA32:
|
||||
return width_ * height_ * 4;
|
||||
case kDXT1:
|
||||
case kATC:
|
||||
return ((width_ + 3) / 4) * ((height_ + 3) / 4) * 8;
|
||||
case kDXT5:
|
||||
case kATCIA:
|
||||
return ((width_ + 3) / 4) * ((height_ + 3) / 4) * 16;
|
||||
case kETC1:
|
||||
return (width_ * height_ * 4) / 8;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return GetImageSize(width_, height_, format_);
|
||||
}
|
||||
|
||||
void Image::ConvertToPow2() {
|
||||
|
@ -275,25 +267,25 @@ bool Image::Compress() {
|
|||
|
||||
switch (tc->format()) {
|
||||
case TextureCompressor::kFormatATC:
|
||||
format_ = kATC;
|
||||
format_ = ImageFormat::kATC;
|
||||
break;
|
||||
case TextureCompressor::kFormatATCIA:
|
||||
format_ = kATCIA;
|
||||
format_ = ImageFormat::kATCIA;
|
||||
break;
|
||||
case TextureCompressor::kFormatDXT1:
|
||||
format_ = kDXT1;
|
||||
format_ = ImageFormat::kDXT1;
|
||||
break;
|
||||
case TextureCompressor::kFormatDXT5:
|
||||
format_ = kDXT5;
|
||||
format_ = ImageFormat::kDXT5;
|
||||
break;
|
||||
case TextureCompressor::kFormatETC1:
|
||||
format_ = kETC1;
|
||||
format_ = ImageFormat::kETC1;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG(0) << "Compressing image. Format: " << format_;
|
||||
LOG(0) << "Compressing image. Format: " << ImageFormatToString(format_);
|
||||
|
||||
unsigned compressedSize = GetSize();
|
||||
uint8_t* compressedBuffer =
|
||||
|
|
|
@ -6,13 +6,12 @@
|
|||
|
||||
#include "base/mem.h"
|
||||
#include "base/vecmath.h"
|
||||
#include "engine/renderer/renderer_types.h"
|
||||
|
||||
namespace eng {
|
||||
|
||||
class Image {
|
||||
public:
|
||||
enum Format { kRGBA32, kDXT1, kDXT5, kETC1, kATC, kATCIA };
|
||||
|
||||
Image();
|
||||
Image(const Image& other);
|
||||
~Image();
|
||||
|
@ -31,8 +30,8 @@ class Image {
|
|||
int GetWidth() const { return width_; }
|
||||
int GetHeight() const { return height_; }
|
||||
|
||||
Format GetFormat() const { return format_; }
|
||||
bool IsCompressed() const { return format_ > kRGBA32; }
|
||||
ImageFormat GetFormat() const { return format_; }
|
||||
bool IsCompressed() const;
|
||||
|
||||
size_t GetSize() const;
|
||||
|
||||
|
@ -51,7 +50,7 @@ class Image {
|
|||
base::AlignedMemPtr<uint8_t[]> buffer_;
|
||||
int width_ = 0;
|
||||
int height_ = 0;
|
||||
Format format_ = kRGBA32;
|
||||
ImageFormat format_ = ImageFormat::kRGBA32;
|
||||
};
|
||||
|
||||
} // namespace eng
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
using namespace base;
|
||||
|
||||
namespace eng {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr GLenum kGlPrimitive[eng::kPrimitive_Max] = {GL_TRIANGLES,
|
||||
|
@ -31,8 +33,6 @@ const std::string kAttributeNames[eng::kAttribType_Max] = {
|
|||
|
||||
} // namespace
|
||||
|
||||
namespace eng {
|
||||
|
||||
RendererOpenGL::RendererOpenGL(base::Closure context_lost_cb)
|
||||
: Renderer(context_lost_cb) {}
|
||||
|
||||
|
@ -259,25 +259,26 @@ void RendererOpenGL::UpdateTexture(uint64_t resource_id,
|
|||
if (image->IsCompressed()) {
|
||||
GLenum format = 0;
|
||||
switch (image->GetFormat()) {
|
||||
case Image::kDXT1:
|
||||
case ImageFormat::kDXT1:
|
||||
format = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||
break;
|
||||
case Image::kDXT5:
|
||||
case ImageFormat::kDXT5:
|
||||
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
break;
|
||||
case Image::kETC1:
|
||||
case ImageFormat::kETC1:
|
||||
format = GL_ETC1_RGB8_OES;
|
||||
break;
|
||||
#if defined(__ANDROID__)
|
||||
case Image::kATC:
|
||||
case ImageFormat::kATC:
|
||||
format = GL_ATC_RGB_AMD;
|
||||
break;
|
||||
case Image::kATCIA:
|
||||
case ImageFormat::kATCIA:
|
||||
format = GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
NOTREACHED() << "- Unhandled texure format: " << image->GetFormat();
|
||||
NOTREACHED() << "- Unhandled texure format: "
|
||||
<< ImageFormatToString(image->GetFormat());
|
||||
}
|
||||
|
||||
glCompressedTexImage2D(GL_TEXTURE_2D, 0, format, image->GetWidth(),
|
||||
|
|
|
@ -14,6 +14,60 @@ const char kLayoutDelimiter[] = ";/ \t";
|
|||
|
||||
namespace eng {
|
||||
|
||||
const char* ImageFormatToString(ImageFormat format) {
|
||||
switch (format) {
|
||||
case ImageFormat::kRGBA32:
|
||||
return "RGBA32";
|
||||
case ImageFormat::kDXT1:
|
||||
return "DXT1";
|
||||
case ImageFormat::kDXT5:
|
||||
return "DXT5";
|
||||
case ImageFormat::kETC1:
|
||||
return "ETC1";
|
||||
case ImageFormat::kATC:
|
||||
return "ATC";
|
||||
case ImageFormat::kATCIA:
|
||||
return "ATCIA";
|
||||
default:
|
||||
NOTREACHED() << "Unknown image format: " << static_cast<int>(format);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsCompressedFormat(ImageFormat format) {
|
||||
switch (format) {
|
||||
case ImageFormat::kRGBA32:
|
||||
return false;
|
||||
case ImageFormat::kDXT1:
|
||||
case ImageFormat::kDXT5:
|
||||
case ImageFormat::kETC1:
|
||||
case ImageFormat::kATC:
|
||||
case ImageFormat::kATCIA:
|
||||
return true;
|
||||
default:
|
||||
NOTREACHED() << "Unknown image format: " << static_cast<int>(format);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
size_t GetImageSize(int width, int height, ImageFormat format) {
|
||||
switch (format) {
|
||||
case ImageFormat::kRGBA32:
|
||||
return width * height * 4;
|
||||
case ImageFormat::kDXT1:
|
||||
case ImageFormat::kATC:
|
||||
return ((width + 3) / 4) * ((height + 3) / 4) * 8;
|
||||
case ImageFormat::kDXT5:
|
||||
case ImageFormat::kATCIA:
|
||||
return ((width + 3) / 4) * ((height + 3) / 4) * 16;
|
||||
case ImageFormat::kETC1:
|
||||
return (width * height * 4) / 8;
|
||||
default:
|
||||
NOTREACHED() << "Unknown image format: " << static_cast<int>(format);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t GetVertexSize(const VertexDescription& vertex_description) {
|
||||
size_t size = 0;
|
||||
for (auto& attr : vertex_description) {
|
||||
|
@ -22,7 +76,7 @@ size_t GetVertexSize(const VertexDescription& vertex_description) {
|
|||
return size;
|
||||
}
|
||||
|
||||
size_t GetIndexSize(DataType index_description) {
|
||||
size_t GetIndexSize(DataType index_description) {
|
||||
switch (index_description) {
|
||||
case kDataType_Byte:
|
||||
return sizeof(char);
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace eng {
|
||||
|
||||
enum class ImageFormat { kRGBA32, kDXT1, kDXT5, kETC1, kATC, kATCIA };
|
||||
|
||||
enum Primitive {
|
||||
kPrimitive_Invalid = -1,
|
||||
kPrimitive_Triangles,
|
||||
|
@ -41,7 +43,14 @@ using DataTypeSize = size_t;
|
|||
using VertexDescription =
|
||||
std::vector<std::tuple<AttribType, DataType, ElementCount, DataTypeSize>>;
|
||||
|
||||
const char* ImageFormatToString(ImageFormat format);
|
||||
|
||||
bool IsCompressedFormat(ImageFormat format);
|
||||
|
||||
size_t GetImageSize(int width, int height, ImageFormat format);
|
||||
|
||||
size_t GetVertexSize(const VertexDescription& vertex_description);
|
||||
|
||||
size_t GetIndexSize(DataType index_description);
|
||||
|
||||
bool ParseVertexDescription(const std::string& vd_str, VertexDescription& out);
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
using namespace base;
|
||||
|
||||
namespace eng {
|
||||
|
||||
namespace {
|
||||
|
||||
// Synchronized with glslang/StandAlone/ResourceLimits.cpp which was removed
|
||||
|
@ -148,12 +150,12 @@ using VertexInputDescription =
|
|||
std::pair<std::vector<VkVertexInputBindingDescription>,
|
||||
std::vector<VkVertexInputAttributeDescription>>;
|
||||
|
||||
constexpr VkPrimitiveTopology kVkPrimitiveType[eng::kPrimitive_Max] = {
|
||||
constexpr VkPrimitiveTopology kVkPrimitiveType[kPrimitive_Max] = {
|
||||
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
|
||||
};
|
||||
|
||||
constexpr VkFormat kVkDataType[eng::kDataType_Max][4] = {
|
||||
constexpr VkFormat kVkDataType[kDataType_Max][4] = {
|
||||
{
|
||||
VK_FORMAT_R8_UNORM,
|
||||
VK_FORMAT_R8G8_UNORM,
|
||||
|
@ -277,8 +279,7 @@ std::vector<uint8_t> CompileGlsl(EShLanguage stage,
|
|||
return ret;
|
||||
}
|
||||
|
||||
VertexInputDescription GetVertexInputDescription(
|
||||
const eng::VertexDescription& vd) {
|
||||
VertexInputDescription GetVertexInputDescription(const VertexDescription& vd) {
|
||||
unsigned vertex_offset = 0;
|
||||
unsigned location = 0;
|
||||
|
||||
|
@ -305,13 +306,13 @@ VertexInputDescription GetVertexInputDescription(
|
|||
return std::make_pair(std::move(bindings), std::move(attributes));
|
||||
}
|
||||
|
||||
VkIndexType GetIndexType(eng::DataType data_type) {
|
||||
VkIndexType GetIndexType(DataType data_type) {
|
||||
switch (data_type) {
|
||||
case eng::kDataType_Invalid:
|
||||
case kDataType_Invalid:
|
||||
return VK_INDEX_TYPE_NONE_KHR;
|
||||
case eng::kDataType_UInt:
|
||||
case kDataType_UInt:
|
||||
return VK_INDEX_TYPE_UINT32;
|
||||
case eng::kDataType_UShort:
|
||||
case kDataType_UShort:
|
||||
return VK_INDEX_TYPE_UINT16;
|
||||
default:
|
||||
break;
|
||||
|
@ -320,21 +321,21 @@ VkIndexType GetIndexType(eng::DataType data_type) {
|
|||
return VK_INDEX_TYPE_NONE_KHR;
|
||||
}
|
||||
|
||||
VkFormat GetImageFormat(eng::Image::Format format) {
|
||||
VkFormat GetImageFormat(ImageFormat format) {
|
||||
switch (format) {
|
||||
case eng::Image::kRGBA32:
|
||||
case ImageFormat::kRGBA32:
|
||||
return VK_FORMAT_R8G8B8A8_UNORM;
|
||||
case eng::Image::kDXT1:
|
||||
case ImageFormat::kDXT1:
|
||||
return VK_FORMAT_BC1_RGB_UNORM_BLOCK;
|
||||
case eng::Image::kDXT5:
|
||||
case ImageFormat::kDXT5:
|
||||
return VK_FORMAT_BC3_UNORM_BLOCK;
|
||||
case eng::Image::kETC1:
|
||||
case eng::Image::kATC:
|
||||
case eng::Image::kATCIA:
|
||||
case ImageFormat::kETC1:
|
||||
case ImageFormat::kATC:
|
||||
case ImageFormat::kATCIA:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
NOTREACHED() << "Invalid format: " << format;
|
||||
NOTREACHED() << "Invalid format: " << ImageFormatToString(format);
|
||||
return VK_FORMAT_R8G8B8A8_UNORM;
|
||||
}
|
||||
|
||||
|
@ -371,8 +372,6 @@ std::pair<int, int> GetNumBlocksForImageFormat(VkFormat format,
|
|||
|
||||
} // namespace
|
||||
|
||||
namespace eng {
|
||||
|
||||
RendererVulkan::RendererVulkan(base::Closure context_lost_cb)
|
||||
: Renderer(context_lost_cb) {}
|
||||
|
||||
|
|
Loading…
Reference in New Issue