2018-12-30 18:44:18 +00:00
|
|
|
#include <algorithm>
|
2018-05-15 13:22:26 +00:00
|
|
|
#include "glcache.h"
|
2013-12-19 17:10:14 +00:00
|
|
|
#include "rend/TexCache.h"
|
|
|
|
#include "hw/pvr/pvr_mem.h"
|
2018-05-09 10:28:38 +00:00
|
|
|
#include "hw/mem/_vmem.h"
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-09-04 16:40:18 +00:00
|
|
|
#include <png.h>
|
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
/*
|
|
|
|
Textures
|
|
|
|
|
2013-12-24 00:56:44 +00:00
|
|
|
Textures are converted to native OpenGL textures
|
2013-12-19 17:10:14 +00:00
|
|
|
The mapping is done with tcw:tsp -> GL texture. That includes stuff like
|
|
|
|
filtering/ texture repeat
|
|
|
|
|
|
|
|
To save space native formats are used for 1555/565/4444 (only bit shuffling is done)
|
2018-07-09 12:59:53 +00:00
|
|
|
YUV is converted to 8888
|
|
|
|
PALs are decoded to their unpaletted format (5551/565/4444/8888 depending on palette type)
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
Mipmaps
|
|
|
|
not supported for now
|
|
|
|
|
2013-12-24 00:56:44 +00:00
|
|
|
Compression
|
2013-12-19 17:10:14 +00:00
|
|
|
look into it, but afaik PVRC is not realtime doable
|
|
|
|
*/
|
|
|
|
|
2015-12-13 22:58:24 +00:00
|
|
|
#if FEAT_HAS_SOFTREND
|
|
|
|
#include <xmmintrin.h>
|
|
|
|
#endif
|
2015-12-03 23:53:42 +00:00
|
|
|
|
2015-08-07 14:03:20 +00:00
|
|
|
extern u32 decoded_colors[3][65536];
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-05-11 13:29:24 +00:00
|
|
|
static void dumpRtTexture(u32 name, u32 w, u32 h) {
|
|
|
|
char sname[256];
|
|
|
|
sprintf(sname, "texdump/%x-%d.png", name, FrameCount);
|
|
|
|
FILE *fp = fopen(sname, "wb");
|
|
|
|
if (fp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
|
|
|
|
png_bytepp rows = (png_bytepp)malloc(h * sizeof(png_bytep));
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
rows[y] = (png_bytep)malloc(w * 4); // 32-bit per pixel
|
|
|
|
glReadPixels(0, y, w, 1, GL_RGBA, GL_UNSIGNED_BYTE, rows[y]);
|
|
|
|
}
|
|
|
|
|
|
|
|
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
|
|
|
png_init_io(png_ptr, fp);
|
|
|
|
|
|
|
|
|
|
|
|
/* write header */
|
|
|
|
png_set_IHDR(png_ptr, info_ptr, w, h,
|
|
|
|
8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
|
|
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
|
|
|
|
/* write bytes */
|
|
|
|
png_write_image(png_ptr, rows);
|
|
|
|
|
|
|
|
/* end write */
|
|
|
|
png_write_end(png_ptr, NULL);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
free(rows[y]);
|
|
|
|
free(rows);
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:22:18 +00:00
|
|
|
void TextureCacheData::UploadToGPU(int width, int height, u8 *temp_tex_buffer)
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
2019-10-04 10:22:18 +00:00
|
|
|
if (texID != 0)
|
2018-09-06 10:11:55 +00:00
|
|
|
{
|
2018-12-30 17:42:55 +00:00
|
|
|
//upload to OpenGL !
|
2019-10-04 10:22:18 +00:00
|
|
|
glcache.BindTexture(GL_TEXTURE_2D, texID);
|
|
|
|
GLuint comps = GL_RGBA;
|
|
|
|
GLuint gltype;
|
|
|
|
switch (tex_type)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2019-10-04 10:22:18 +00:00
|
|
|
case TextureType::_5551:
|
|
|
|
gltype = GL_UNSIGNED_SHORT_5_5_5_1;
|
|
|
|
break;
|
|
|
|
case TextureType::_565:
|
|
|
|
gltype = GL_UNSIGNED_SHORT_5_6_5;
|
|
|
|
comps = GL_RGB;
|
|
|
|
break;
|
|
|
|
case TextureType::_4444:
|
|
|
|
gltype = GL_UNSIGNED_SHORT_4_4_4_4;
|
|
|
|
break;
|
|
|
|
case TextureType::_8888:
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
2019-10-04 10:22:18 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0,comps, width, height, 0, comps, gltype, temp_tex_buffer);
|
|
|
|
if (tcw.MipMapped && settings.rend.UseMipmaps)
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
2018-12-30 17:42:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if FEAT_HAS_SOFTREND
|
2019-10-04 10:22:18 +00:00
|
|
|
/*
|
|
|
|
if (tex_type == TextureType::_565)
|
2018-12-30 17:42:55 +00:00
|
|
|
tex_type = 0;
|
2019-10-04 10:22:18 +00:00
|
|
|
else if (tex_type == TextureType::_5551)
|
2018-12-30 17:42:55 +00:00
|
|
|
tex_type = 1;
|
2019-10-04 10:22:18 +00:00
|
|
|
else if (tex_type == TextureType::_4444)
|
2018-12-30 17:42:55 +00:00
|
|
|
tex_type = 2;
|
2019-10-04 10:22:18 +00:00
|
|
|
*/
|
2018-12-30 17:42:55 +00:00
|
|
|
u16 *tex_data = (u16 *)temp_tex_buffer;
|
|
|
|
if (pData) {
|
|
|
|
_mm_free(pData);
|
|
|
|
}
|
|
|
|
|
|
|
|
pData = (u16*)_mm_malloc(w * h * 16, 16);
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
u32* data = (u32*)&pData[(x + y*w) * 8];
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-12-30 17:42:55 +00:00
|
|
|
data[0] = decoded_colors[tex_type][tex_data[(x + 1) % w + (y + 1) % h * w]];
|
|
|
|
data[1] = decoded_colors[tex_type][tex_data[(x + 0) % w + (y + 1) % h * w]];
|
|
|
|
data[2] = decoded_colors[tex_type][tex_data[(x + 1) % w + (y + 0) % h * w]];
|
|
|
|
data[3] = decoded_colors[tex_type][tex_data[(x + 0) % w + (y + 0) % h * w]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
die("Soft rend disabled, invalid code path");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-12-30 18:19:27 +00:00
|
|
|
bool TextureCacheData::Delete()
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
2019-10-04 10:22:18 +00:00
|
|
|
if (!BaseTextureCacheData::Delete())
|
2018-12-30 18:19:27 +00:00
|
|
|
return false;
|
2019-10-04 10:22:18 +00:00
|
|
|
|
2018-12-30 17:42:55 +00:00
|
|
|
if (pData) {
|
|
|
|
#if FEAT_HAS_SOFTREND
|
|
|
|
_mm_free(pData);
|
|
|
|
pData = 0;
|
|
|
|
#else
|
|
|
|
die("softrend disabled, invalid codepath");
|
|
|
|
#endif
|
|
|
|
}
|
2015-12-03 23:53:42 +00:00
|
|
|
|
2018-12-30 17:42:55 +00:00
|
|
|
if (texID) {
|
|
|
|
glcache.DeleteTextures(1, &texID);
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
2018-12-30 18:19:27 +00:00
|
|
|
|
|
|
|
return true;
|
2018-12-30 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
void BindRTT(u32 addy, u32 fbw, u32 fbh, u32 channels, u32 fmt)
|
|
|
|
{
|
2019-02-18 23:49:24 +00:00
|
|
|
if (gl.rtt.fbo) glDeleteFramebuffers(1,&gl.rtt.fbo);
|
|
|
|
if (gl.rtt.tex) glcache.DeleteTextures(1,&gl.rtt.tex);
|
|
|
|
if (gl.rtt.depthb) glDeleteRenderbuffers(1,&gl.rtt.depthb);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-02-18 23:49:24 +00:00
|
|
|
gl.rtt.TexAddr=addy>>3;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-06-27 20:41:16 +00:00
|
|
|
// Find the smallest power of two texture that fits the viewport
|
2018-05-08 16:47:00 +00:00
|
|
|
int fbh2 = 2;
|
|
|
|
while (fbh2 < fbh)
|
|
|
|
fbh2 *= 2;
|
2018-05-11 13:29:24 +00:00
|
|
|
int fbw2 = 2;
|
|
|
|
while (fbw2 < fbw)
|
|
|
|
fbw2 *= 2;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-07-16 14:28:48 +00:00
|
|
|
if (settings.rend.RenderToTextureUpscale > 1 && !settings.rend.RenderToTextureBuffer)
|
|
|
|
{
|
|
|
|
fbw *= settings.rend.RenderToTextureUpscale;
|
|
|
|
fbh *= settings.rend.RenderToTextureUpscale;
|
|
|
|
fbw2 *= settings.rend.RenderToTextureUpscale;
|
|
|
|
fbh2 *= settings.rend.RenderToTextureUpscale;
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
// Get the currently bound frame buffer object. On most platforms this just gives 0.
|
|
|
|
//glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_i32OriginalFbo);
|
|
|
|
|
|
|
|
// Generate and bind a render buffer which will become a depth buffer shared between our two FBOs
|
2019-02-18 23:49:24 +00:00
|
|
|
glGenRenderbuffers(1, &gl.rtt.depthb);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, gl.rtt.depthb);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Currently it is unknown to GL that we want our new render buffer to be a depth buffer.
|
|
|
|
glRenderbufferStorage will fix this and in this case will allocate a depth buffer
|
|
|
|
m_i32TexSize by m_i32TexSize.
|
|
|
|
*/
|
|
|
|
|
2019-02-19 20:40:21 +00:00
|
|
|
if (gl.is_gles)
|
|
|
|
{
|
|
|
|
#if defined(GL_DEPTH24_STENCIL8_OES) && defined(GL_DEPTH_COMPONENT24_OES)
|
|
|
|
if (gl.GL_OES_packed_depth_stencil_supported)
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, fbw2, fbh2);
|
|
|
|
else if (gl.GL_OES_depth24_supported)
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, fbw2, fbh2);
|
|
|
|
else
|
2014-01-21 22:12:04 +00:00
|
|
|
#endif
|
2019-02-19 20:40:21 +00:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, fbw2, fbh2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, fbw2, fbh2);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
// Create a texture for rendering to
|
2019-02-18 23:49:24 +00:00
|
|
|
gl.rtt.tex = glcache.GenTexture();
|
|
|
|
glcache.BindTexture(GL_TEXTURE_2D, gl.rtt.tex);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-05-11 13:29:24 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, channels, fbw2, fbh2, 0, channels, fmt, 0);
|
2019-02-18 16:42:07 +00:00
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2013-12-19 17:10:14 +00:00
|
|
|
// Create the object that will allow us to render to the aforementioned texture
|
2019-02-18 23:49:24 +00:00
|
|
|
glGenFramebuffers(1, &gl.rtt.fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gl.rtt.fbo);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
// Attach the texture to the FBO
|
2019-02-18 23:49:24 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl.rtt.tex, 0);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
// Attach the depth buffer we created earlier to our FBO.
|
2019-02-18 23:49:24 +00:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gl.rtt.depthb);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-02-19 20:40:21 +00:00
|
|
|
if (!gl.is_gles || gl.GL_OES_packed_depth_stencil_supported)
|
2019-02-18 23:49:24 +00:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gl.rtt.depthb);
|
2019-02-18 16:42:07 +00:00
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
// Check that our FBO creation was successful
|
|
|
|
GLuint uStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
|
|
|
|
verify(uStatus == GL_FRAMEBUFFER_COMPLETE);
|
2018-05-08 16:47:00 +00:00
|
|
|
|
|
|
|
glViewport(0, 0, fbw, fbh); // TODO CLIP_X/Y min?
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:47:00 +00:00
|
|
|
void ReadRTTBuffer() {
|
|
|
|
u32 w = pvrrc.fb_X_CLIP.max - pvrrc.fb_X_CLIP.min + 1;
|
|
|
|
u32 h = pvrrc.fb_Y_CLIP.max - pvrrc.fb_Y_CLIP.min + 1;
|
|
|
|
|
2018-05-11 17:33:17 +00:00
|
|
|
u32 size = w * h * 2;
|
2018-05-08 16:47:00 +00:00
|
|
|
|
2018-05-11 17:33:17 +00:00
|
|
|
const u8 fb_packmode = FB_W_CTRL.fb_packmode;
|
2018-05-09 10:28:38 +00:00
|
|
|
|
2018-05-22 13:34:33 +00:00
|
|
|
if (settings.rend.RenderToTextureBuffer)
|
2018-05-11 17:33:17 +00:00
|
|
|
{
|
2019-02-18 23:49:24 +00:00
|
|
|
u32 tex_addr = gl.rtt.TexAddr << 3;
|
2018-05-09 10:28:38 +00:00
|
|
|
|
2019-10-04 10:22:18 +00:00
|
|
|
// Remove all vram locks before calling glReadPixels
|
2018-05-11 17:33:17 +00:00
|
|
|
// (deadlock on rpi)
|
2019-10-04 10:22:18 +00:00
|
|
|
u32 page_tex_addr = tex_addr & PAGE_MASK;
|
|
|
|
u32 page_size = size + tex_addr - page_tex_addr;
|
|
|
|
page_size = ((page_size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
|
|
|
|
for (u32 page = page_tex_addr; page < page_tex_addr + page_size; page += PAGE_SIZE)
|
|
|
|
VramLockedWriteOffset(page);
|
2018-05-09 10:28:38 +00:00
|
|
|
|
2018-05-11 17:33:17 +00:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
2018-06-25 13:48:57 +00:00
|
|
|
u16 *dst = (u16 *)&vram[tex_addr];
|
2018-05-09 10:28:38 +00:00
|
|
|
|
2018-05-11 17:33:17 +00:00
|
|
|
GLint color_fmt, color_type;
|
|
|
|
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &color_fmt);
|
|
|
|
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &color_type);
|
2018-05-11 13:29:24 +00:00
|
|
|
|
2019-10-09 19:14:31 +00:00
|
|
|
if (fb_packmode == 1 && stride == w * 2 && color_fmt == GL_RGB && color_type == GL_UNSIGNED_SHORT_5_6_5)
|
|
|
|
{
|
2018-05-11 17:33:17 +00:00
|
|
|
// Can be read directly into vram
|
|
|
|
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, dst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-01 17:43:01 +00:00
|
|
|
PixelBuffer<u32> tmp_buf;
|
|
|
|
tmp_buf.init(w, h);
|
|
|
|
|
2018-05-11 17:33:17 +00:00
|
|
|
const u16 kval_bit = (FB_W_CTRL.fb_kval & 0x80) << 8;
|
|
|
|
const u8 fb_alpha_threshold = FB_W_CTRL.fb_alpha_threshold;
|
|
|
|
|
2018-08-01 17:43:01 +00:00
|
|
|
u8 *p = (u8 *)tmp_buf.data();
|
|
|
|
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
|
|
|
|
|
2019-10-09 19:14:31 +00:00
|
|
|
WriteTextureToVRam(w, h, p, dst);
|
2018-05-09 10:28:38 +00:00
|
|
|
}
|
2018-05-11 17:33:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-25 13:48:57 +00:00
|
|
|
//memset(&vram[fb_rtt.TexAddr << 3], '\0', size);
|
2018-05-11 17:33:17 +00:00
|
|
|
}
|
2018-05-09 10:28:38 +00:00
|
|
|
|
2018-05-11 13:29:24 +00:00
|
|
|
//dumpRtTexture(fb_rtt.TexAddr, w, h);
|
2019-02-03 16:53:02 +00:00
|
|
|
|
|
|
|
if (w > 1024 || h > 1024 || settings.rend.RenderToTextureBuffer) {
|
2019-02-18 23:49:24 +00:00
|
|
|
glcache.DeleteTextures(1, &gl.rtt.tex);
|
2018-05-11 13:29:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-24 09:22:56 +00:00
|
|
|
// TexAddr : fb_rtt.TexAddr, Reserved : 0, StrideSel : 0, ScanOrder : 1
|
2019-02-18 23:49:24 +00:00
|
|
|
TCW tcw = { { gl.rtt.TexAddr, 0, 0, 1 } };
|
2018-05-11 17:33:17 +00:00
|
|
|
switch (fb_packmode) {
|
2018-05-11 13:29:24 +00:00
|
|
|
case 0:
|
|
|
|
case 3:
|
2018-08-01 17:43:01 +00:00
|
|
|
tcw.PixelFmt = Pixel1555;
|
2018-05-11 13:29:24 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2018-08-01 17:43:01 +00:00
|
|
|
tcw.PixelFmt = Pixel565;
|
2018-05-11 13:29:24 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-08-01 17:43:01 +00:00
|
|
|
tcw.PixelFmt = Pixel4444;
|
2018-05-11 13:29:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
TSP tsp = { 0 };
|
|
|
|
for (tsp.TexU = 0; tsp.TexU <= 7 && (8 << tsp.TexU) < w; tsp.TexU++);
|
|
|
|
for (tsp.TexV = 0; tsp.TexV <= 7 && (8 << tsp.TexV) < h; tsp.TexV++);
|
|
|
|
|
2019-10-04 11:33:08 +00:00
|
|
|
TextureCacheData *texture_data = static_cast<TextureCacheData*>(getTextureCacheData(tsp, tcw, [](){ return (BaseTextureCacheData *)new TextureCacheData(); }));
|
2018-05-11 13:29:24 +00:00
|
|
|
if (texture_data->texID != 0)
|
2018-05-15 13:22:26 +00:00
|
|
|
glcache.DeleteTextures(1, &texture_data->texID);
|
2019-02-03 16:53:02 +00:00
|
|
|
else
|
2019-10-04 10:22:18 +00:00
|
|
|
texture_data->Create();
|
2019-02-18 23:49:24 +00:00
|
|
|
texture_data->texID = gl.rtt.tex;
|
2018-05-11 13:29:24 +00:00
|
|
|
texture_data->dirty = 0;
|
2019-02-03 16:53:02 +00:00
|
|
|
if (texture_data->lock_block == NULL)
|
|
|
|
texture_data->lock_block = libCore_vramlock_Lock(texture_data->sa_tex, texture_data->sa + texture_data->size - 1, texture_data);
|
2018-05-11 13:29:24 +00:00
|
|
|
}
|
2019-02-18 23:49:24 +00:00
|
|
|
gl.rtt.tex = 0;
|
2018-05-11 13:29:24 +00:00
|
|
|
|
2019-02-18 23:49:24 +00:00
|
|
|
if (gl.rtt.fbo) { glDeleteFramebuffers(1,&gl.rtt.fbo); gl.rtt.fbo = 0; }
|
|
|
|
if (gl.rtt.depthb) { glDeleteRenderbuffers(1,&gl.rtt.depthb); gl.rtt.depthb = 0; }
|
2018-05-08 16:47:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-11 13:29:24 +00:00
|
|
|
static int TexCacheLookups;
|
|
|
|
static int TexCacheHits;
|
|
|
|
static float LastTexCacheStats;
|
|
|
|
|
2019-10-04 11:33:08 +00:00
|
|
|
u64 gl_GetTexture(TSP tsp, TCW tcw)
|
2018-05-11 13:29:24 +00:00
|
|
|
{
|
|
|
|
TexCacheLookups++;
|
|
|
|
|
|
|
|
//lookup texture
|
2019-10-04 11:33:08 +00:00
|
|
|
TextureCacheData* tf = static_cast<TextureCacheData*>(getTextureCacheData(tsp, tcw, [](){ return (BaseTextureCacheData *)new TextureCacheData(); }));
|
2018-05-11 13:29:24 +00:00
|
|
|
|
|
|
|
if (tf->texID == 0)
|
2019-10-04 10:22:18 +00:00
|
|
|
{
|
|
|
|
tf->Create();
|
|
|
|
tf->texID = glcache.GenTexture();
|
|
|
|
}
|
2018-05-11 13:29:24 +00:00
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
//update if needed
|
|
|
|
if (tf->NeedsUpdate())
|
|
|
|
tf->Update();
|
2018-05-11 13:29:24 +00:00
|
|
|
else
|
2018-12-30 17:42:55 +00:00
|
|
|
{
|
|
|
|
tf->CheckCustomTexture();
|
2018-05-11 13:29:24 +00:00
|
|
|
TexCacheHits++;
|
2018-12-30 17:42:55 +00:00
|
|
|
}
|
2018-05-11 13:29:24 +00:00
|
|
|
|
|
|
|
// if (os_GetSeconds() - LastTexCacheStats >= 2.0)
|
|
|
|
// {
|
|
|
|
// LastTexCacheStats = os_GetSeconds();
|
|
|
|
// printf("Texture cache efficiency: %.2f%% cache size %ld\n", (float)TexCacheHits / TexCacheLookups * 100, TexCache.size());
|
|
|
|
// TexCacheLookups = 0;
|
|
|
|
// TexCacheHits = 0;
|
|
|
|
// }
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
//update state for opts/stuff
|
|
|
|
tf->Lookups++;
|
|
|
|
|
|
|
|
//return gl texture
|
|
|
|
return tf->texID;
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:02:34 +00:00
|
|
|
|
|
|
|
text_info raw_GetTexture(TSP tsp, TCW tcw)
|
|
|
|
{
|
|
|
|
text_info rv = { 0 };
|
|
|
|
|
|
|
|
//lookup texture
|
2019-10-04 11:33:08 +00:00
|
|
|
TextureCacheData* tf = static_cast<TextureCacheData*>(getTextureCacheData(tsp, tcw, [](){ return (BaseTextureCacheData *)new TextureCacheData(); }));
|
2015-08-05 15:02:34 +00:00
|
|
|
|
2019-10-04 11:33:08 +00:00
|
|
|
if (tf->pData == nullptr)
|
2019-10-04 10:22:18 +00:00
|
|
|
tf->Create();
|
2015-08-05 15:02:34 +00:00
|
|
|
|
|
|
|
//update if needed
|
|
|
|
if (tf->NeedsUpdate())
|
|
|
|
tf->Update();
|
|
|
|
|
|
|
|
//update state for opts/stuff
|
|
|
|
tf->Lookups++;
|
|
|
|
|
|
|
|
//return gl texture
|
|
|
|
rv.height = tf->h;
|
|
|
|
rv.width = tf->w;
|
|
|
|
rv.pdata = tf->pData;
|
2019-10-04 10:22:18 +00:00
|
|
|
rv.textype = (u32)tf->tex_type;
|
2015-08-05 15:02:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-08 01:29:14 +00:00
|
|
|
void DoCleanup() {
|
|
|
|
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
void rend_text_invl(vram_block* bl)
|
|
|
|
{
|
|
|
|
TextureCacheData* tcd = (TextureCacheData*)bl->userdata;
|
2014-05-08 01:29:14 +00:00
|
|
|
tcd->dirty=FrameCount;
|
2013-12-19 17:10:14 +00:00
|
|
|
tcd->lock_block=0;
|
|
|
|
|
|
|
|
libCore_vramlock_Unlock_block_wb(bl);
|
2015-08-11 13:58:45 +00:00
|
|
|
}
|
2018-08-26 14:58:10 +00:00
|
|
|
|
|
|
|
GLuint fbTextureId;
|
|
|
|
|
|
|
|
void RenderFramebuffer()
|
|
|
|
{
|
|
|
|
if (FB_R_SIZE.fb_x_size == 0 || FB_R_SIZE.fb_y_size == 0)
|
|
|
|
return;
|
|
|
|
|
2019-10-05 14:06:07 +00:00
|
|
|
PixelBuffer<u32> pb;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
ReadFramebuffer(pb, width, height);
|
2018-08-26 14:58:10 +00:00
|
|
|
|
|
|
|
if (fbTextureId == 0)
|
|
|
|
fbTextureId = glcache.GenTexture();
|
|
|
|
|
|
|
|
glcache.BindTexture(GL_TEXTURE_2D, fbTextureId);
|
|
|
|
|
|
|
|
//set texture repeat mode
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pb.data());
|
|
|
|
}
|
|
|
|
|
2019-02-19 10:36:59 +00:00
|
|
|
GLuint init_output_framebuffer(int width, int height)
|
2019-02-18 23:49:24 +00:00
|
|
|
{
|
|
|
|
if (width != gl.ofbo.width || height != gl.ofbo.height)
|
|
|
|
{
|
2019-04-07 22:21:06 +00:00
|
|
|
free_output_framebuffer();
|
2019-02-18 23:49:24 +00:00
|
|
|
gl.ofbo.width = width;
|
|
|
|
gl.ofbo.height = height;
|
|
|
|
}
|
|
|
|
if (gl.ofbo.fbo == 0)
|
|
|
|
{
|
|
|
|
// Create the depth+stencil renderbuffer
|
|
|
|
glGenRenderbuffers(1, &gl.ofbo.depthb);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, gl.ofbo.depthb);
|
|
|
|
|
2019-02-19 20:40:21 +00:00
|
|
|
if (gl.is_gles)
|
|
|
|
{
|
|
|
|
#if defined(GL_DEPTH24_STENCIL8_OES) && defined(GL_DEPTH_COMPONENT24_OES)
|
|
|
|
if (gl.GL_OES_packed_depth_stencil_supported)
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, width, height);
|
|
|
|
else if (gl.GL_OES_depth24_supported)
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, width, height);
|
|
|
|
else
|
2019-02-18 23:49:24 +00:00
|
|
|
#endif
|
2019-02-19 20:40:21 +00:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
|
|
|
|
2019-04-07 22:21:06 +00:00
|
|
|
if (gl.gl_major < 3)
|
|
|
|
{
|
|
|
|
// Create a texture for rendering to
|
|
|
|
gl.ofbo.tex = glcache.GenTexture();
|
|
|
|
glcache.BindTexture(GL_TEXTURE_2D, gl.ofbo.tex);
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glcache.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use a renderbuffer and glBlitFramebuffer
|
|
|
|
glGenRenderbuffers(1, &gl.ofbo.colorb);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, gl.ofbo.colorb);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
|
|
|
|
}
|
2019-02-18 23:49:24 +00:00
|
|
|
|
|
|
|
// Create the framebuffer
|
|
|
|
glGenFramebuffers(1, &gl.ofbo.fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gl.ofbo.fbo);
|
|
|
|
|
|
|
|
// Attach the depth buffer to our FBO.
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gl.ofbo.depthb);
|
|
|
|
|
2019-02-19 20:40:21 +00:00
|
|
|
if (!gl.is_gles || gl.GL_OES_packed_depth_stencil_supported)
|
2019-02-18 23:49:24 +00:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gl.ofbo.depthb);
|
|
|
|
|
2019-04-07 22:21:06 +00:00
|
|
|
// Attach the texture/renderbuffer to the FBO
|
|
|
|
if (gl.gl_major < 3)
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl.ofbo.tex, 0);
|
|
|
|
else
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, gl.ofbo.colorb);
|
2019-02-18 23:49:24 +00:00
|
|
|
|
|
|
|
// Check that our FBO creation was successful
|
|
|
|
GLuint uStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
|
|
|
|
verify(uStatus == GL_FRAMEBUFFER_COMPLETE);
|
2019-04-07 22:21:06 +00:00
|
|
|
|
|
|
|
glcache.Disable(GL_SCISSOR_TEST);
|
|
|
|
glcache.ClearColor(0.f, 0.f, 0.f, 0.f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2019-02-18 23:49:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, gl.ofbo.fbo);
|
|
|
|
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
glCheck();
|
2019-02-19 10:36:59 +00:00
|
|
|
|
|
|
|
return gl.ofbo.fbo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_output_framebuffer()
|
|
|
|
{
|
|
|
|
if (gl.ofbo.fbo != 0)
|
|
|
|
{
|
|
|
|
glDeleteFramebuffers(1, &gl.ofbo.fbo);
|
|
|
|
gl.ofbo.fbo = 0;
|
|
|
|
glDeleteRenderbuffers(1, &gl.ofbo.depthb);
|
|
|
|
gl.ofbo.depthb = 0;
|
2019-04-07 22:21:06 +00:00
|
|
|
if (gl.ofbo.tex != 0)
|
|
|
|
{
|
|
|
|
glcache.DeleteTextures(1, &gl.ofbo.tex);
|
|
|
|
gl.ofbo.tex = 0;
|
|
|
|
}
|
|
|
|
if (gl.ofbo.colorb != 0)
|
|
|
|
{
|
|
|
|
glDeleteRenderbuffers(1, &gl.ofbo.colorb);
|
|
|
|
gl.ofbo.colorb = 0;
|
|
|
|
}
|
2019-02-19 10:36:59 +00:00
|
|
|
}
|
2019-02-18 23:49:24 +00:00
|
|
|
}
|