rasterizer_cache: Remove remaining OpenGL code

This commit is contained in:
emufan4568 2022-08-21 13:32:02 +03:00
parent fc450edd14
commit 1579f96397
5 changed files with 77 additions and 140 deletions

View file

@ -7,13 +7,11 @@
#include "common/alignment.h" #include "common/alignment.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/microprofile.h" #include "common/microprofile.h"
#include "common/scope_exit.h"
#include "video_core/pica_state.h" #include "video_core/pica_state.h"
#include "video_core/rasterizer_cache/rasterizer_cache.h" #include "video_core/rasterizer_cache/rasterizer_cache.h"
#include "video_core/renderer_opengl/texture_filters/texture_filterer.h" #include "video_core/renderer_opengl/texture_filters/texture_filterer.h"
#include "video_core/renderer_opengl/texture_downloader_es.h" #include "video_core/renderer_opengl/texture_downloader_es.h"
#include "video_core/renderer_opengl/gl_format_reinterpreter.h" #include "video_core/renderer_opengl/gl_format_reinterpreter.h"
#include "video_core/renderer_opengl/gl_state.h"
#include "video_core/renderer_opengl/gl_vars.h" #include "video_core/renderer_opengl/gl_vars.h"
namespace OpenGL { namespace OpenGL {
@ -78,76 +76,24 @@ static constexpr auto RangeFromInterval(Map& map, const Interval& interval) {
} }
// Allocate an uninitialized texture of appropriate size and format for the surface // Allocate an uninitialized texture of appropriate size and format for the surface
OGLTexture RasterizerCacheOpenGL::AllocateSurfaceTexture(const FormatTuple& format_tuple, u32 width, OGLTexture RasterizerCacheOpenGL::AllocateSurfaceTexture(const FormatTuple& tuple,
u32 height) { u32 width, u32 height) {
auto recycled_tex = host_texture_recycler.find({format_tuple, width, height}); auto recycled_tex = host_texture_recycler.find({tuple, width, height});
if (recycled_tex != host_texture_recycler.end()) { if (recycled_tex != host_texture_recycler.end()) {
OGLTexture texture = std::move(recycled_tex->second); OGLTexture texture = std::move(recycled_tex->second);
host_texture_recycler.erase(recycled_tex); host_texture_recycler.erase(recycled_tex);
return texture; return texture;
} }
const GLsizei levels = static_cast<GLsizei>(std::log2(std::max(width, height))) + 1;
OGLTexture texture; OGLTexture texture;
texture.Create(); texture.Create();
texture.Allocate(GL_TEXTURE_2D, levels, tuple.internal_format, width, height);
OpenGLState cur_state = OpenGLState::GetCurState();
// Keep track of previous texture bindings
GLuint old_tex = cur_state.texture_units[0].texture_2d;
cur_state.texture_units[0].texture_2d = texture.handle;
cur_state.Apply();
glActiveTexture(GL_TEXTURE0);
if (GL_ARB_texture_storage) {
// Allocate all possible mipmap levels upfront
const GLsizei levels = static_cast<GLsizei>(std::log2(std::max(width, height))) + 1;
glTexStorage2D(GL_TEXTURE_2D, levels, format_tuple.internal_format, width, height);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, format_tuple.internal_format, width, height, 0,
format_tuple.format, format_tuple.type, nullptr);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Restore previous texture bindings
cur_state.texture_units[0].texture_2d = old_tex;
cur_state.Apply();
return texture; return texture;
} }
static void AllocateTextureCube(GLuint texture, const FormatTuple& format_tuple, u32 width) {
OpenGLState cur_state = OpenGLState::GetCurState();
// Keep track of previous texture bindings
GLuint old_tex = cur_state.texture_cube_unit.texture_cube;
cur_state.texture_cube_unit.texture_cube = texture;
cur_state.Apply();
glActiveTexture(TextureUnits::TextureCube.Enum());
if (GL_ARB_texture_storage) {
// Allocate all possible mipmap levels in case the game uses them later
const GLsizei levels = static_cast<GLsizei>(std::log2(width)) + 1;
glTexStorage2D(GL_TEXTURE_CUBE_MAP, levels, format_tuple.internal_format, width, width);
} else {
for (auto faces : {
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
}) {
glTexImage2D(faces, 0, format_tuple.internal_format, width, width, 0,
format_tuple.format, format_tuple.type, nullptr);
}
}
// Restore previous texture bindings
cur_state.texture_cube_unit.texture_cube = old_tex;
cur_state.Apply();
}
MICROPROFILE_DEFINE(OpenGL_CopySurface, "OpenGL", "CopySurface", MP_RGB(128, 192, 64)); MICROPROFILE_DEFINE(OpenGL_CopySurface, "OpenGL", "CopySurface", MP_RGB(128, 192, 64));
void RasterizerCacheOpenGL::CopySurface(const Surface& src_surface, const Surface& dst_surface, void RasterizerCacheOpenGL::CopySurface(const Surface& src_surface, const Surface& dst_surface,
SurfaceInterval copy_interval) { SurfaceInterval copy_interval) {
@ -562,20 +508,19 @@ const CachedTextureCube& RasterizerCacheOpenGL::GetTextureCube(const TextureCube
auto& cube = texture_cube_cache[config]; auto& cube = texture_cube_cache[config];
struct Face { struct Face {
Face(std::shared_ptr<SurfaceWatcher>& watcher, PAddr address, GLenum gl_face) Face(std::shared_ptr<SurfaceWatcher>& watcher, PAddr address)
: watcher(watcher), address(address), gl_face(gl_face) {} : watcher(watcher), address(address) {}
std::shared_ptr<SurfaceWatcher>& watcher; std::shared_ptr<SurfaceWatcher>& watcher;
PAddr address; PAddr address;
GLenum gl_face;
}; };
const std::array<Face, 6> faces{{ const std::array<Face, 6> faces{{
{cube.px, config.px, GL_TEXTURE_CUBE_MAP_POSITIVE_X}, {cube.px, config.px},
{cube.nx, config.nx, GL_TEXTURE_CUBE_MAP_NEGATIVE_X}, {cube.nx, config.nx},
{cube.py, config.py, GL_TEXTURE_CUBE_MAP_POSITIVE_Y}, {cube.py, config.py},
{cube.ny, config.ny, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y}, {cube.ny, config.ny},
{cube.pz, config.pz, GL_TEXTURE_CUBE_MAP_POSITIVE_Z}, {cube.pz, config.pz},
{cube.nz, config.nz, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z}, {cube.nz, config.nz},
}}; }};
for (const Face& face : faces) { for (const Face& face : faces) {
@ -606,18 +551,17 @@ const CachedTextureCube& RasterizerCacheOpenGL::GetTextureCube(const TextureCube
} }
} }
const auto& tuple = GetFormatTuple(PixelFormatFromTextureFormat(config.format));
const u32 width = cube.res_scale * config.width;
const GLsizei levels = static_cast<GLsizei>(std::log2(width)) + 1;
// Allocate the cube texture
cube.texture.Create(); cube.texture.Create();
AllocateTextureCube( cube.texture.Allocate(GL_TEXTURE_CUBE_MAP, levels, tuple.internal_format, width, width);
cube.texture.handle,
GetFormatTuple(PixelFormatFromTextureFormat(config.format)),
cube.res_scale * config.width);
} }
u32 scaled_size = cube.res_scale * config.width; u32 scaled_size = cube.res_scale * config.width;
OpenGLState prev_state = OpenGLState::GetCurState();
SCOPE_EXIT({ prev_state.Apply(); });
for (const Face& face : faces) { for (const Face& face : faces) {
if (face.watcher && !face.watcher->IsValid()) { if (face.watcher && !face.watcher->IsValid()) {
auto surface = face.watcher->Get(); auto surface = face.watcher->Get();

View file

@ -717,27 +717,21 @@ bool RasterizerOpenGL::Draw(bool accelerate, bool is_indexed) {
} }
OGLTexture temp_tex; OGLTexture temp_tex;
if (need_duplicate_texture && (GLAD_GL_ARB_copy_image || GLES)) { if (need_duplicate_texture) {
const auto& tuple = GetFormatTuple(color_surface->pixel_format);
const GLsizei levels = color_surface->max_level + 1;
// The game is trying to use a surface as a texture and framebuffer at the same time // The game is trying to use a surface as a texture and framebuffer at the same time
// which causes unpredictable behavior on the host. // which causes unpredictable behavior on the host.
// Making a copy to sample from eliminates this issue and seems to be fairly cheap. // Making a copy to sample from eliminates this issue and seems to be fairly cheap.
temp_tex.Create(); temp_tex.Create();
glBindTexture(GL_TEXTURE_2D, temp_tex.handle); temp_tex.Allocate(GL_TEXTURE_2D, levels, tuple.internal_format,
auto [internal_format, format, type] = GetFormatTuple(color_surface->pixel_format); color_surface->GetScaledWidth(),
OGLTexture::Allocate(GL_TEXTURE_2D, color_surface->max_level + 1, internal_format, format,
type, color_surface->GetScaledWidth(),
color_surface->GetScaledHeight()); color_surface->GetScaledHeight());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, state.texture_units[0].texture_2d);
for (u32 level{0}; level <= color_surface->max_level; ++level) { temp_tex.CopyFrom(color_surface->texture, GL_TEXTURE_2D, levels,
glCopyImageSubData(color_surface->texture.handle, GL_TEXTURE_2D, level, 0, 0, 0, color_surface->GetScaledWidth(),
temp_tex.handle, GL_TEXTURE_2D, level, 0, 0, 0, color_surface->GetScaledHeight());
color_surface->GetScaledWidth() >> level,
color_surface->GetScaledHeight() >> level, 1);
}
for (auto& unit : state.texture_units) { for (auto& unit : state.texture_units) {
if (unit.texture_2d == color_surface->texture.handle) { if (unit.texture_2d == color_surface->texture.handle) {

View file

@ -6,7 +6,6 @@
#include "video_core/renderer_opengl/gl_resource_manager.h" #include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_util.h" #include "video_core/renderer_opengl/gl_shader_util.h"
#include "video_core/renderer_opengl/gl_state.h" #include "video_core/renderer_opengl/gl_state.h"
#include "video_core/renderer_opengl/gl_vars.h"
MICROPROFILE_DEFINE(OpenGL_ResourceCreation, "OpenGL", "Resource Creation", MP_RGB(128, 128, 192)); MICROPROFILE_DEFINE(OpenGL_ResourceCreation, "OpenGL", "Resource Creation", MP_RGB(128, 128, 192));
MICROPROFILE_DEFINE(OpenGL_ResourceDeletion, "OpenGL", "Resource Deletion", MP_RGB(128, 128, 192)); MICROPROFILE_DEFINE(OpenGL_ResourceDeletion, "OpenGL", "Resource Deletion", MP_RGB(128, 128, 192));
@ -14,16 +13,18 @@ MICROPROFILE_DEFINE(OpenGL_ResourceDeletion, "OpenGL", "Resource Deletion", MP_R
namespace OpenGL { namespace OpenGL {
void OGLRenderbuffer::Create() { void OGLRenderbuffer::Create() {
if (handle != 0) if (handle != 0) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceCreation); MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
glGenRenderbuffers(1, &handle); glGenRenderbuffers(1, &handle);
} }
void OGLRenderbuffer::Release() { void OGLRenderbuffer::Release() {
if (handle == 0) if (handle == 0) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceDeletion); MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
glDeleteRenderbuffers(1, &handle); glDeleteRenderbuffers(1, &handle);
@ -32,16 +33,18 @@ void OGLRenderbuffer::Release() {
} }
void OGLTexture::Create() { void OGLTexture::Create() {
if (handle != 0) if (handle != 0) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceCreation); MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
glGenTextures(1, &handle); glGenTextures(1, &handle);
} }
void OGLTexture::Release() { void OGLTexture::Release() {
if (handle == 0) if (handle == 0) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceDeletion); MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
glDeleteTextures(1, &handle); glDeleteTextures(1, &handle);
@ -49,73 +52,66 @@ void OGLTexture::Release() {
handle = 0; handle = 0;
} }
void OGLTexture::Allocate(GLenum target, GLsizei levels, GLenum internalformat, GLenum format, void OGLTexture::Allocate(GLenum target, GLsizei levels, GLenum internalformat,
GLenum type, GLsizei width, GLsizei height, GLsizei depth) { GLsizei width, GLsizei height, GLsizei depth) {
const bool tex_storage = GLAD_GL_ARB_texture_storage || GLES; GLuint old_tex = OpenGLState::GetCurState().texture_units[0].texture_2d;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle);
switch (target) { switch (target) {
case GL_TEXTURE_1D: case GL_TEXTURE_1D:
case GL_TEXTURE: case GL_TEXTURE:
if (tex_storage) {
glTexStorage1D(target, levels, internalformat, width); glTexStorage1D(target, levels, internalformat, width);
} else {
for (GLsizei level{0}; level < levels; ++level) {
glTexImage1D(target, level, internalformat, width, 0, format, type, nullptr);
width >>= 1;
}
}
break; break;
case GL_TEXTURE_2D: case GL_TEXTURE_2D:
case GL_TEXTURE_1D_ARRAY: case GL_TEXTURE_1D_ARRAY:
case GL_TEXTURE_RECTANGLE: case GL_TEXTURE_RECTANGLE:
case GL_TEXTURE_CUBE_MAP: case GL_TEXTURE_CUBE_MAP:
if (tex_storage) {
glTexStorage2D(target, levels, internalformat, width, height); glTexStorage2D(target, levels, internalformat, width, height);
} else {
for (GLsizei level{0}; level < levels; ++level) {
glTexImage2D(target, level, internalformat, width, height, 0, format, type,
nullptr);
width >>= 1;
if (target != GL_TEXTURE_1D_ARRAY)
height >>= 1;
}
}
break; break;
case GL_TEXTURE_3D: case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY: case GL_TEXTURE_2D_ARRAY:
case GL_TEXTURE_CUBE_MAP_ARRAY: case GL_TEXTURE_CUBE_MAP_ARRAY:
if (tex_storage) {
glTexStorage3D(target, levels, internalformat, width, height, depth); glTexStorage3D(target, levels, internalformat, width, height, depth);
} else {
for (GLsizei level{0}; level < levels; ++level) {
glTexImage3D(target, level, internalformat, width, height, depth, 0, format, type,
nullptr);
width >>= 1;
height >>= 1;
if (target == GL_TEXTURE_3D)
depth >>= 1;
}
}
break; break;
} }
if (!tex_storage) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels - 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, old_tex);
} }
void OGLTexture::CopyFrom(const OGLTexture& other, GLenum target, GLsizei levels,
GLsizei width, GLsizei height) {
GLuint old_tex = OpenGLState::GetCurState().texture_units[0].texture_2d;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle);
for (u32 level = 0; level < levels; level++) {
glCopyImageSubData(other.handle, target, level, 0, 0, 0,
handle, target, level, 0, 0, 0,
width >> level,
height >> level, 1);
}
glBindTexture(GL_TEXTURE_2D, old_tex);
} }
void OGLSampler::Create() { void OGLSampler::Create() {
if (handle != 0) if (handle != 0) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceCreation); MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
glGenSamplers(1, &handle); glGenSamplers(1, &handle);
} }
void OGLSampler::Release() { void OGLSampler::Release() {
if (handle == 0) if (handle == 0) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceDeletion); MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
glDeleteSamplers(1, &handle); glDeleteSamplers(1, &handle);

View file

@ -58,8 +58,11 @@ public:
/// Deletes the internal OpenGL resource /// Deletes the internal OpenGL resource
void Release(); void Release();
static void Allocate(GLenum target, GLsizei levels, GLenum internalformat, GLenum format, void Allocate(GLenum target, GLsizei levels, GLenum internalformat,
GLenum type, GLsizei width, GLsizei height = 1, GLsizei depth = 1); GLsizei width, GLsizei height = 1, GLsizei depth = 1);
void CopyFrom(const OGLTexture& other, GLenum target, GLsizei levels,
GLsizei width, GLsizei height);
GLuint handle = 0; GLuint handle = 0;
}; };

View file

@ -6,7 +6,7 @@
#include <vector> #include <vector>
#include <fmt/chrono.h> #include <fmt/chrono.h>
#include "common/logging/log.h" #include "common/logging/log.h"
#include "video_core/rasterizer_cache/rasterizer_cache.h" #include "video_core/rasterizer_cache/rasterizer_cache_utils.h"
#include "video_core/renderer_opengl/gl_state.h" #include "video_core/renderer_opengl/gl_state.h"
#include "video_core/renderer_opengl/texture_downloader_es.h" #include "video_core/renderer_opengl/texture_downloader_es.h"
#include "shaders/depth_to_color.frag" #include "shaders/depth_to_color.frag"