mirror of
https://github.com/hrydgard/ppsspp.git
synced 2024-11-23 05:19:56 +00:00
Remove the old UI texture class
This commit is contained in:
parent
7979b6378e
commit
c219793803
@ -913,13 +913,8 @@ add_library(native STATIC
|
||||
ext/native/gfx/gl_debug_log.h
|
||||
ext/native/gfx/gl_lost_manager.cpp
|
||||
ext/native/gfx/gl_lost_manager.h
|
||||
ext/native/gfx/texture.cpp
|
||||
ext/native/gfx/texture.h
|
||||
ext/native/gfx/texture_atlas.cpp
|
||||
ext/native/gfx/texture_atlas.h
|
||||
# ext/native/gfx/texture_dx11.cpp
|
||||
ext/native/gfx/texture_gen.cpp
|
||||
ext/native/gfx/texture_gen.h
|
||||
ext/native/gfx_es2/draw_buffer.cpp
|
||||
ext/native/gfx_es2/draw_buffer.h
|
||||
ext/native/gfx_es2/draw_text.cpp
|
||||
|
@ -83,9 +83,7 @@ SOURCES += $$P/ext/native/audio/*.cpp \
|
||||
$$P/ext/native/file/*.cpp \
|
||||
$$P/ext/native/gfx/gl_debug_log.cpp \
|
||||
$$P/ext/native/gfx/gl_lost_manager.cpp \
|
||||
$$P/ext/native/gfx/texture.cpp \
|
||||
$$P/ext/native/gfx/texture_atlas.cpp \
|
||||
$$P/ext/native/gfx/texture_gen.cpp \
|
||||
$$P/ext/native/gfx_es2/*.cpp \
|
||||
$$P/ext/native/gfx_es2/*.c \
|
||||
$$P/ext/native/i18n/*.cpp \
|
||||
|
@ -51,7 +51,6 @@
|
||||
#include "gfx_es2/draw_text.h"
|
||||
#include "gfx_es2/gpu_features.h"
|
||||
#include "gfx/gl_lost_manager.h"
|
||||
#include "gfx/texture.h"
|
||||
#include "i18n/i18n.h"
|
||||
#include "input/input_state.h"
|
||||
#include "math/fast/fast_math.h"
|
||||
|
@ -76,9 +76,7 @@ LOCAL_SRC_FILES :=\
|
||||
gfx_es2/vertex_format.cpp \
|
||||
gfx/gl_debug_log.cpp \
|
||||
gfx/gl_lost_manager.cpp \
|
||||
gfx/texture.cpp \
|
||||
gfx/texture_atlas.cpp \
|
||||
gfx/texture_gen.cpp \
|
||||
image/zim_load.cpp \
|
||||
image/zim_save.cpp \
|
||||
image/png_load.cpp \
|
||||
|
@ -1,9 +1,7 @@
|
||||
set(SRCS
|
||||
gl_debug_log.cpp
|
||||
gl_lost_manager.cpp
|
||||
texture.cpp
|
||||
texture_atlas.cpp
|
||||
texture_gen.cpp)
|
||||
texture_atlas.cpp)
|
||||
|
||||
set(SRCS ${SRCS})
|
||||
|
||||
|
@ -1,376 +0,0 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ext/rg_etc1/rg_etc1.h"
|
||||
#include "ext/jpge/jpgd.h"
|
||||
#include "image/png_load.h"
|
||||
#include "image/zim_load.h"
|
||||
#include "base/logging.h"
|
||||
#include "gfx/texture.h"
|
||||
#include "gfx/texture_gen.h"
|
||||
#include "gfx/gl_debug_log.h"
|
||||
#include "gfx/gl_lost_manager.h"
|
||||
#include "gfx/gl_common.h"
|
||||
#include "gfx_es2/gpu_features.h"
|
||||
|
||||
Texture::Texture() : id_(0) {
|
||||
CheckGLExtensions();
|
||||
register_gl_resource_holder(this);
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
unregister_gl_resource_holder(this);
|
||||
Destroy();
|
||||
}
|
||||
|
||||
void Texture::Destroy() {
|
||||
if (id_) {
|
||||
glDeleteTextures(1, &id_);
|
||||
id_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Texture::GLLost() {
|
||||
if (!filename_.empty()) {
|
||||
Load(filename_.c_str());
|
||||
ILOG("Reloaded lost texture %s", filename_.c_str());
|
||||
} else {
|
||||
WLOG("Texture %p cannot be restored - has no filename", this);
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
static void SetTextureParameters(int zim_flags) {
|
||||
GLenum wrap = GL_REPEAT;
|
||||
if (zim_flags & ZIM_CLAMP)
|
||||
wrap = GL_CLAMP_TO_EDGE;
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
|
||||
GL_CHECK();
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
if ((zim_flags & (ZIM_HAS_MIPS | ZIM_GEN_MIPS))) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
} else {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
GL_CHECK();
|
||||
}
|
||||
|
||||
bool Texture::Load(const char *filename) {
|
||||
// hook for generated textures
|
||||
if (!memcmp(filename, "gen:", 4)) {
|
||||
int bpp, w, h;
|
||||
bool clamp;
|
||||
uint8_t *data = generateTexture(filename, bpp, w, h, clamp);
|
||||
if (!data)
|
||||
return false;
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
if (bpp == 1) {
|
||||
if (gl_extensions.IsGLES) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
|
||||
} else {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, 1, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
} else {
|
||||
FLOG("unsupported");
|
||||
}
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
delete [] data;
|
||||
return true;
|
||||
}
|
||||
|
||||
filename_ = filename;
|
||||
|
||||
// Currently here are a bunch of project-specific workarounds.
|
||||
// They shouldn't really hurt anything else very much though.
|
||||
|
||||
size_t len = strlen(filename);
|
||||
char fn[1024];
|
||||
strncpy(fn, filename, sizeof(fn));
|
||||
fn[1023] = 0;
|
||||
bool zim = false;
|
||||
if (!strcmp("dds", &filename[len-3])) {
|
||||
strcpy(&fn[len-3], "zim");
|
||||
zim = true;
|
||||
}
|
||||
if (!strcmp("6TX", &filename[len-3]) || !strcmp("6tx", &filename[len-3])) {
|
||||
ILOG("Detected 6TX %s", filename);
|
||||
strcpy(&fn[len-3], "zim");
|
||||
zim = true;
|
||||
}
|
||||
for (int i = 0; i < (int)strlen(fn); i++) {
|
||||
if (fn[i] == '\\') fn[i] = '/';
|
||||
}
|
||||
|
||||
if (fn[0] == 'm') fn[0] = 'M';
|
||||
const char *name = fn;
|
||||
if (zim && 0==memcmp(name, "Media/textures/", strlen("Media/textures"))) name += strlen("Media/textures/");
|
||||
len = strlen(name);
|
||||
if (!strcmp("png", &name[len-3]) || !strcmp("PNG", &name[len-3])) {
|
||||
if (!LoadPNG(fn)) {
|
||||
WLOG("WARNING: Failed to load .png %s, falling back to ugly gray XOR pattern!", fn);
|
||||
LoadXOR();
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (!strcmp("zim", &name[len-3])) {
|
||||
if (LoadZIM(name)) {
|
||||
return true;
|
||||
} else {
|
||||
WLOG("WARNING: Failed to load .zim texture %s, falling back to ugly gray XOR pattern!", fn);
|
||||
LoadXOR();
|
||||
return false;
|
||||
}
|
||||
} else if (!strcmp("jpg", &name[len-3]) || !strcmp("JPG", &name[len-3]) ||
|
||||
!strcmp("jpeg", &name[len-4]) || !strcmp("JPEG", &name[len-4])) {
|
||||
if (!LoadJPEG(fn)) {
|
||||
WLOG("WARNING: Failed to load jpeg %s, falling back to ugly gray XOR pattern!", fn);
|
||||
LoadXOR();
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (!name || !strlen(name)) {
|
||||
ELOG("Failed to identify image file %s by extension", name);
|
||||
} else {
|
||||
ELOG("Cannot load a texture with an empty filename");
|
||||
}
|
||||
LoadXOR();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Texture::LoadPNG(const char *filename, bool genMips) {
|
||||
unsigned char *image_data;
|
||||
if (1 != pngLoad(filename, &width_, &height_, &image_data, false)) {
|
||||
return false;
|
||||
}
|
||||
GL_CHECK();
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(genMips ? ZIM_GEN_MIPS : ZIM_CLAMP);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, image_data);
|
||||
if (genMips) {
|
||||
if (gl_extensions.ARB_framebuffer_object) {
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
#ifndef USING_GLES2
|
||||
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
GL_CHECK();
|
||||
free(image_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Texture::LoadJPEG(const char *filename, bool genMips) {
|
||||
ILOG("Loading jpeg %s", filename);
|
||||
unsigned char *image_data;
|
||||
int actual_comps;
|
||||
image_data = jpgd::decompress_jpeg_image_from_file(filename, &width_, &height_, &actual_comps, 4);
|
||||
if (!image_data) {
|
||||
ELOG("jpeg: image data returned was 0");
|
||||
return false;
|
||||
}
|
||||
ILOG("Jpeg decoder failed to get RGB, got: %i x %i x %i", actual_comps, width_, height_);
|
||||
ILOG("First four bytes: %i %i %i %i", image_data[0], image_data[1], image_data[2], image_data[3]);
|
||||
|
||||
GL_CHECK();
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(genMips ? ZIM_GEN_MIPS : ZIM_CLAMP);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
|
||||
if (genMips) {
|
||||
if (gl_extensions.ARB_framebuffer_object) {
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
#ifndef USING_GLES2
|
||||
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
GL_CHECK();
|
||||
free(image_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Texture::LoadPNG(const uint8_t *data, size_t size, bool genMips) {
|
||||
unsigned char *image_data;
|
||||
if (1 != pngLoadPtr(data, size, &width_, &height_, &image_data, false)) {
|
||||
return false;
|
||||
}
|
||||
GL_CHECK();
|
||||
// TODO: should check for power of 2 tex size and disallow genMips when not.
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(genMips ? ZIM_GEN_MIPS : ZIM_CLAMP);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
|
||||
if (genMips) {
|
||||
if (gl_extensions.ARB_framebuffer_object) {
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
#ifndef USING_GLES2
|
||||
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
GL_CHECK();
|
||||
free(image_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Texture::LoadXOR() {
|
||||
width_ = height_ = 256;
|
||||
unsigned char *buf = new unsigned char[width_*height_*4];
|
||||
for (int y = 0; y < 256; y++) {
|
||||
for (int x = 0; x < 256; x++) {
|
||||
buf[(y*width_ + x)*4 + 0] = x^y;
|
||||
buf[(y*width_ + x)*4 + 1] = x^y;
|
||||
buf[(y*width_ + x)*4 + 2] = x^y;
|
||||
buf[(y*width_ + x)*4 + 3] = 0xFF;
|
||||
}
|
||||
}
|
||||
GL_CHECK();
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(ZIM_GEN_MIPS);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, buf);
|
||||
if(gl_extensions.ARB_framebuffer_object){
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}else{
|
||||
#ifndef USING_GLES2
|
||||
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
||||
#endif
|
||||
}
|
||||
GL_CHECK();
|
||||
delete [] buf;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#if !defined(USING_GLES2) || defined(IOS)
|
||||
|
||||
// Allocates using new[], doesn't free.
|
||||
uint8_t *ETC1ToRGBA(uint8_t *etc1, int width, int height) {
|
||||
uint8_t *rgba = new uint8_t[width * height * 4];
|
||||
memset(rgba, 0xFF, width * height * 4);
|
||||
for (int y = 0; y < height; y += 4) {
|
||||
for (int x = 0; x < width; x += 4) {
|
||||
rg_etc1::unpack_etc1_block(etc1 + ((y / 4) * width/4 + (x / 4)) * 8,
|
||||
(uint32_t *)rgba + (y * width + x), width, false);
|
||||
}
|
||||
}
|
||||
return rgba;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool Texture::LoadZIM(const char *filename) {
|
||||
uint8_t *image_data[ZIM_MAX_MIP_LEVELS];
|
||||
int width[ZIM_MAX_MIP_LEVELS];
|
||||
int height[ZIM_MAX_MIP_LEVELS];
|
||||
|
||||
int flags;
|
||||
int num_levels = ::LoadZIM(filename, &width[0], &height[0], &flags, &image_data[0]);
|
||||
if (!num_levels)
|
||||
return false;
|
||||
if (num_levels >= ZIM_MAX_MIP_LEVELS)
|
||||
return false;
|
||||
width_ = width[0];
|
||||
height_ = height[0];
|
||||
int data_type = GL_UNSIGNED_BYTE;
|
||||
int colors = GL_RGBA;
|
||||
int storage = GL_RGBA;
|
||||
bool compressed = false;
|
||||
switch (flags & ZIM_FORMAT_MASK) {
|
||||
case ZIM_RGBA8888:
|
||||
data_type = GL_UNSIGNED_BYTE;
|
||||
break;
|
||||
case ZIM_RGBA4444:
|
||||
data_type = GL_UNSIGNED_SHORT_4_4_4_4;
|
||||
break;
|
||||
case ZIM_RGB565:
|
||||
data_type = GL_UNSIGNED_SHORT_5_6_5;
|
||||
colors = GL_RGB;
|
||||
storage = GL_RGB;
|
||||
break;
|
||||
case ZIM_ETC1:
|
||||
compressed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
GL_CHECK();
|
||||
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(flags);
|
||||
|
||||
if (compressed) {
|
||||
for (int l = 0; l < num_levels; l++) {
|
||||
int data_w = width[l];
|
||||
int data_h = height[l];
|
||||
if (data_w < 4) data_w = 4;
|
||||
if (data_h < 4) data_h = 4;
|
||||
#if defined(USING_GLES2) && !defined(IOS)
|
||||
int compressed_image_bytes = data_w * data_h / 2;
|
||||
glCompressedTexImage2D(GL_TEXTURE_2D, l, GL_ETC1_RGB8_OES, width[l], height[l], 0, compressed_image_bytes, image_data[l]);
|
||||
GL_CHECK();
|
||||
#else
|
||||
// TODO: OpenGL 4.3+ accepts ETC1 so we should not have to do this anymore on those cards.
|
||||
// Also, iOS does not have support for ETC1 compressed textures so we just decompress.
|
||||
// TODO: Use PVR texture compression on iOS.
|
||||
image_data[l] = ETC1ToRGBA(image_data[l], data_w, data_h);
|
||||
glTexImage2D(GL_TEXTURE_2D, l, GL_RGBA, width[l], height[l], 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, image_data[l]);
|
||||
#endif
|
||||
}
|
||||
GL_CHECK();
|
||||
#if !defined(USING_GLES2)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_levels - 2);
|
||||
#endif
|
||||
} else {
|
||||
for (int l = 0; l < num_levels; l++) {
|
||||
glTexImage2D(GL_TEXTURE_2D, l, storage, width[l], height[l], 0,
|
||||
colors, data_type, image_data[l]);
|
||||
}
|
||||
if (num_levels == 1 && (flags & ZIM_GEN_MIPS)) {
|
||||
if(gl_extensions.ARB_framebuffer_object) {
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}else{
|
||||
#ifndef USING_GLES2
|
||||
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
SetTextureParameters(flags);
|
||||
|
||||
GL_CHECK();
|
||||
// Only free the top level, since the allocation is used for all of them.
|
||||
free(image_data[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::Bind(int stage) {
|
||||
GL_CHECK();
|
||||
if (stage != -1)
|
||||
glActiveTexture(GL_TEXTURE0 + stage);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
GL_CHECK();
|
||||
}
|
||||
|
||||
void Texture::Unbind(int stage) {
|
||||
GL_CHECK();
|
||||
if (stage != -1)
|
||||
glActiveTexture(GL_TEXTURE0 + stage);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
GL_CHECK();
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// Load and manage OpenGL textures easily. Supports ETC1 compressed texture with mipmaps
|
||||
// in the custom ZIM format.
|
||||
|
||||
// This is deprecated - start using Thin3D instead.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "gfx/gl_lost_manager.h"
|
||||
|
||||
class Texture : public GfxResourceHolder {
|
||||
public:
|
||||
Texture();
|
||||
~Texture();
|
||||
|
||||
// Deduces format from the filename.
|
||||
// If loading fails, will load a 256x256 XOR texture.
|
||||
// If filename begins with "gen:", will defer to texture_gen.cpp/h.
|
||||
// When format is known, it's fine to use LoadZIM etc directly.
|
||||
// Those will NOT auto-fall back to xor texture however!
|
||||
bool Load(const char *filename);
|
||||
void Bind(int stage = -1);
|
||||
void Destroy();
|
||||
|
||||
// PNG from memory buffer
|
||||
bool LoadPNG(const uint8_t *data, size_t size, bool genMips = true);
|
||||
bool LoadZIM(const char *filename);
|
||||
bool LoadPNG(const char *filename, bool genMips = true);
|
||||
bool LoadJPEG(const char *filename, bool genMips = true);
|
||||
|
||||
unsigned int Handle() const {
|
||||
return id_;
|
||||
}
|
||||
|
||||
virtual void GLLost();
|
||||
std::string filename() const { return filename_; }
|
||||
|
||||
static void Unbind(int stage = -1);
|
||||
|
||||
int Width() const { return width_; }
|
||||
int Height() const { return height_; }
|
||||
|
||||
private:
|
||||
bool LoadXOR(); // Loads a placeholder texture.
|
||||
|
||||
std::string filename_;
|
||||
#ifdef METRO
|
||||
ID3D11Texture2D *tex_;
|
||||
#endif
|
||||
unsigned int id_;
|
||||
int width_, height_;
|
||||
};
|
@ -1,271 +0,0 @@
|
||||
// WIP, please ignore
|
||||
#include <d3d11_1.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(USING_GLES2)
|
||||
#include "image/png_load.h"
|
||||
#include "ext/etcpack/etcdec.h"
|
||||
#endif
|
||||
|
||||
#include "image/zim_load.h"
|
||||
#include "base/logging.h"
|
||||
#include "gfx/texture.h"
|
||||
#include "gfx/texture_gen.h"
|
||||
#include "gfx/gl_debug_log.h"
|
||||
#include "gfx/gl_lost_manager.h"
|
||||
|
||||
Texture::Texture() : tex_(0) {
|
||||
register_gl_resource_holder(this);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Texture::Destroy() {
|
||||
if (tex_) {
|
||||
tex_->Release();
|
||||
tex_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Texture::GLLost() {
|
||||
ILOG("Reloading lost texture %s", filename_.c_str());
|
||||
Load(filename_.c_str());
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
unregister_gl_resource_holder(this);
|
||||
Destroy();
|
||||
}
|
||||
|
||||
static void SetTextureParameters(int zim_flags) {
|
||||
/*
|
||||
GLenum wrap = GL_REPEAT;
|
||||
if (zim_flags & ZIM_CLAMP) wrap = GL_CLAMP_TO_EDGE;
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
|
||||
GL_CHECK();
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
if ((zim_flags & (ZIM_HAS_MIPS | ZIM_GEN_MIPS))) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
} else {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
GL_CHECK();*/
|
||||
}
|
||||
|
||||
bool Texture::Load(const char *filename) {
|
||||
// hook for generated textures
|
||||
if (!memcmp(filename, "gen:", 4)) {
|
||||
// TODO
|
||||
// return false;
|
||||
tex_ = (LPVOID)generateTexture(filename);
|
||||
if (tex_) {
|
||||
this->filename_ = filename;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
filename_ = filename;
|
||||
// Currently contains many Rollerball-specific workarounds.
|
||||
// They shouldn't really hurt anything else very much though.
|
||||
int len = strlen(filename);
|
||||
char fn[256];
|
||||
strcpy(fn, filename);
|
||||
bool zim = false;
|
||||
if (!strcmp("dds", &filename[len-3])) {
|
||||
strcpy(&fn[len-3], "zim");
|
||||
zim = true;
|
||||
}
|
||||
if (!strcmp("6TX", &filename[len-3]) || !strcmp("6tx", &filename[len-3])) {
|
||||
ILOG("Detected 6TX %s", filename);
|
||||
strcpy(&fn[len-3], "zim");
|
||||
zim = true;
|
||||
}
|
||||
for (int i = 0; i < (int)strlen(fn); i++) {
|
||||
if (fn[i] == '\\') fn[i] = '/';
|
||||
}
|
||||
|
||||
if (fn[0] == 'm') fn[0] = 'M';
|
||||
const char *name = fn;
|
||||
if (zim && 0 == memcmp(name, "Media/textures/", strlen("Media/textures"))) name += strlen("Media/textures/");
|
||||
len = strlen(name);
|
||||
#if !defined(USING_GLES2)
|
||||
if (!strcmp("png", &name[len-3]) ||
|
||||
!strcmp("PNG", &name[len-3])) {
|
||||
if (!LoadPNG(fn)) {
|
||||
LoadXOR();
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
if (!strcmp("zim", &name[len-3])) {
|
||||
if (!LoadZIM(name)) {
|
||||
LoadXOR();
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
LoadXOR();
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifndef METRO
|
||||
#if !defined(USING_GLES2)
|
||||
bool Texture::LoadPNG(const char *filename) {
|
||||
unsigned char *image_data;
|
||||
if (1 != pngLoad(filename, &width_, &height_, &image_data, false)) {
|
||||
return false;
|
||||
}
|
||||
GL_CHECK();
|
||||
glGenTextures(1, &id_);
|
||||
glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(ZIM_GEN_MIPS);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, image_data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
GL_CHECK();
|
||||
free(image_data);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bool Texture::LoadXOR() {
|
||||
width_ = height_ = 256;
|
||||
unsigned char *buf = new unsigned char[width_*height_*4];
|
||||
for (int y = 0; y < 256; y++) {
|
||||
for (int x = 0; x < 256; x++) {
|
||||
buf[(y*width_ + x)*4 + 0] = x^y;
|
||||
buf[(y*width_ + x)*4 + 1] = x^y;
|
||||
buf[(y*width_ + x)*4 + 2] = x^y;
|
||||
buf[(y*width_ + x)*4 + 3] = 0xFF;
|
||||
}
|
||||
}
|
||||
GL_CHECK();
|
||||
ID3D11Device *ctx;
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
desc.Width = width_;
|
||||
desc.Height = height_;
|
||||
desc.MipLevels = 0;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.CPUAccessFlags = 0;
|
||||
desc.MiscFlags = 0;
|
||||
if (FAILED(ctx->CreateTexture2D(&desc, 0, &tex_))) {
|
||||
FLOG("Failed creating XOR texture");
|
||||
}
|
||||
SetTextureParameters(ZIM_GEN_MIPS);
|
||||
GL_CHECK();
|
||||
delete [] buf;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#if !defined(USING_GLES2)
|
||||
|
||||
// Allocates using new[], doesn't free.
|
||||
uint8_t *ETC1ToRGBA(uint8_t *etc1, int width, int height) {
|
||||
uint8_t *rgba = new uint8_t[width * height * 4];
|
||||
memset(rgba, 0xFF, width * height * 4);
|
||||
for (int y = 0; y < height; y += 4) {
|
||||
for (int x = 0; x < width; x += 4) {
|
||||
DecompressBlock(etc1 + ((y / 4) * width/4 + (x / 4)) * 8,
|
||||
rgba + (y * width + x) * 4, width, 255);
|
||||
}
|
||||
}
|
||||
return rgba;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool Texture::LoadZIM(const char *filename) {
|
||||
uint8_t *image_data[ZIM_MAX_MIP_LEVELS];
|
||||
int width[ZIM_MAX_MIP_LEVELS];
|
||||
int height[ZIM_MAX_MIP_LEVELS];
|
||||
|
||||
int flags;
|
||||
int num_levels = ::LoadZIM(filename, &width[0], &height[0], &flags, &image_data[0]);
|
||||
if (!num_levels)
|
||||
return false;
|
||||
width_ = width[0];
|
||||
height_ = height[0];
|
||||
int data_type = GL_UNSIGNED_BYTE;
|
||||
int colors = GL_RGBA;
|
||||
int storage = GL_RGBA;
|
||||
bool compressed = false;
|
||||
switch (flags & ZIM_FORMAT_MASK) {
|
||||
case ZIM_RGBA8888:
|
||||
data_type = GL_UNSIGNED_BYTE;
|
||||
break;
|
||||
case ZIM_RGBA4444:
|
||||
data_type = DXGI_FORMAT_B4G4R4A4_UNORM;
|
||||
break;
|
||||
case ZIM_RGB565:
|
||||
data_type = DXGI_FORMAT_B5G6R5_UNORM;
|
||||
colors = GL_RGB;
|
||||
storage = GL_RGB;
|
||||
break;
|
||||
case ZIM_ETC1:
|
||||
compressed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
GL_CHECK();
|
||||
//glGenTextures(1, &id_);
|
||||
//glBindTexture(GL_TEXTURE_2D, id_);
|
||||
SetTextureParameters(flags);
|
||||
|
||||
if (compressed) {
|
||||
for (int l = 0; l < num_levels; l++) {
|
||||
int data_w = width[l];
|
||||
int data_h = height[l];
|
||||
if (data_w < 4) data_w = 4;
|
||||
if (data_h < 4) data_h = 4;
|
||||
#if defined(USING_GLES2)
|
||||
int compressed_image_bytes = data_w * data_h / 2;
|
||||
glCompressedTexImage2D(GL_TEXTURE_2D, l, GL_ETC1_RGB8_OES, width[l], height[l], 0, compressed_image_bytes, image_data[l]);
|
||||
GL_CHECK();
|
||||
#else
|
||||
//image_data[l] = ETC1ToRGBA(image_data[l], data_w, data_h);
|
||||
//glTexImage2D(GL_TEXTURE_2D, l, GL_RGBA, width[l], height[l], 0,
|
||||
// GL_RGBA, GL_UNSIGNED_BYTE, image_data[l]);
|
||||
#endif
|
||||
}
|
||||
GL_CHECK();
|
||||
#if !defined(USING_GLES2)
|
||||
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, num_levels - 2);
|
||||
#endif
|
||||
} else {
|
||||
for (int l = 0; l < num_levels; l++) {
|
||||
//glTexImage2D(GL_TEXTURE_2D, l, storage, width[l], height[l], 0,
|
||||
// colors, data_type, image_data[l]);
|
||||
}
|
||||
if (num_levels == 1 && (flags & ZIM_GEN_MIPS)) {
|
||||
//glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
}
|
||||
SetTextureParameters(flags);
|
||||
|
||||
GL_CHECK();
|
||||
// Only free the top level, since the allocation is used for all of them.
|
||||
delete [] image_data[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::Bind(int stage) {
|
||||
GL_CHECK();
|
||||
//if (stage != -1)
|
||||
// glActiveTexture(GL_TEXTURE0 + stage);
|
||||
// glBindTexture(GL_TEXTURE_2D, id_);
|
||||
GL_CHECK();
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
// Minimal procedural texture generator to generate some usual textures like circles and vignette textures.
|
||||
// "Gen" textures have filenames like this: "gen:256:256:vignette:0.1"
|
||||
//
|
||||
// These must be VERY VERY fast to not slow down loading. Could multicore this in the
|
||||
// future.
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "gfx/texture.h"
|
||||
|
||||
|
||||
uint8_t *generateTexture(const char *filename, int &bpp, int &w, int &h, bool &clamp) {
|
||||
char name_and_params[256];
|
||||
// security check :)
|
||||
if (strlen(filename) > 200)
|
||||
return 0;
|
||||
sscanf(filename, "gen:%i:%i:%s", &w, &h, name_and_params);
|
||||
|
||||
uint8_t *data;
|
||||
if (!strcmp(name_and_params, "vignette")) {
|
||||
bpp = 1;
|
||||
data = new uint8_t[w*h];
|
||||
for (int y = 0; y < h; ++y) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
float dx = (float)(x - w/2) / (w/2);
|
||||
float dy = (float)(y - h/2) / (h/2);
|
||||
float dist = sqrtf(dx * dx + dy * dy);
|
||||
dist /= 1.414f;
|
||||
float val = 1.0 - powf(dist, 1.4f);
|
||||
data[y*w + x] = val * 255;
|
||||
}
|
||||
}
|
||||
} else if (!strcmp(name_and_params, "circle")) {
|
||||
bpp = 1;
|
||||
// TODO
|
||||
data = new uint8_t[w*h];
|
||||
for (int y = 0; y < h; ++y) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
float dx = (float)(x - w/2) / (w/2);
|
||||
float dy = (float)(y - h/2) / (h/2);
|
||||
float dist = sqrtf(dx * dx + dy * dy);
|
||||
dist /= 1.414f;
|
||||
float val = 1.0 - powf(dist, 1.4f);
|
||||
data[y*w + x] = val * 255;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data = NULL;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
// Minimal procedural texture generator to generate some useful but unnecessary-to-store textures like circles.
|
||||
// "Gen" textures have filenames like this: "gen:256:256:4444:vignette:0.1"
|
||||
|
||||
#include "gfx/texture.h"
|
||||
|
||||
uint8_t *generateTexture(const char *filename, int &bpp, int &w, int &h, bool &clamp);
|
@ -231,8 +231,6 @@
|
||||
<ClInclude Include="gfx\gl_common.h" />
|
||||
<ClInclude Include="gfx\gl_debug_log.h" />
|
||||
<ClInclude Include="gfx\gl_lost_manager.h" />
|
||||
<ClInclude Include="gfx\texture_gen.h" />
|
||||
<ClInclude Include="gfx\texture.h" />
|
||||
<ClInclude Include="gfx\texture_atlas.h" />
|
||||
<ClInclude Include="gfx_es2\draw_buffer.h" />
|
||||
<ClInclude Include="gfx_es2\draw_text.h" />
|
||||
@ -689,9 +687,7 @@
|
||||
<ClCompile Include="file\zip_read.cpp" />
|
||||
<ClCompile Include="gfx\gl_debug_log.cpp" />
|
||||
<ClCompile Include="gfx\gl_lost_manager.cpp" />
|
||||
<ClCompile Include="gfx\texture.cpp" />
|
||||
<ClCompile Include="gfx\texture_atlas.cpp" />
|
||||
<ClCompile Include="gfx\texture_gen.cpp" />
|
||||
<ClCompile Include="gfx_es2\draw_buffer.cpp" />
|
||||
<ClCompile Include="gfx_es2\draw_text.cpp" />
|
||||
<ClCompile Include="gfx_es2\gl3stub.c">
|
||||
|
@ -90,9 +90,6 @@
|
||||
<ClInclude Include="base\timeutil.h">
|
||||
<Filter>base</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="gfx\texture.h">
|
||||
<Filter>gfx</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="input\gesture_detector.h">
|
||||
<Filter>input</Filter>
|
||||
</ClInclude>
|
||||
@ -105,9 +102,6 @@
|
||||
<ClInclude Include="base\colorutil.h">
|
||||
<Filter>base</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="gfx\texture_gen.h">
|
||||
<Filter>gfx</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="json\json_writer.h">
|
||||
<Filter>json</Filter>
|
||||
</ClInclude>
|
||||
@ -371,9 +365,6 @@
|
||||
<ClCompile Include="base\timeutil.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="gfx\texture.cpp">
|
||||
<Filter>gfx</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="input\gesture_detector.cpp">
|
||||
<Filter>input</Filter>
|
||||
</ClCompile>
|
||||
@ -383,9 +374,6 @@
|
||||
<ClCompile Include="base\colorutil.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="gfx\texture_gen.cpp">
|
||||
<Filter>gfx</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="json\json_writer.cpp">
|
||||
<Filter>json</Filter>
|
||||
</ClCompile>
|
||||
|
@ -8,7 +8,6 @@
|
||||
#include "base/colorutil.h"
|
||||
#include "ui/ui.h"
|
||||
#include "ui/ui_context.h"
|
||||
#include "gfx/texture.h"
|
||||
#include "gfx/texture_atlas.h"
|
||||
#include "gfx_es2/draw_buffer.h"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user