suyu/src/video_core/renderer_opengl/gl_texture_cache.h

144 lines
4.1 KiB
C++
Raw Normal View History

// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>
#include <glad/glad.h>
#include "common/common_types.h"
#include "video_core/engines/shader_bytecode.h"
#include "video_core/renderer_opengl/gl_device.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/texture_cache/texture_cache.h"
namespace OpenGL {
using VideoCommon::SurfaceParams;
2019-05-07 16:57:16 +02:00
using VideoCommon::ViewParams;
class CachedSurfaceView;
class CachedSurface;
class TextureCacheOpenGL;
2019-04-17 01:01:07 +02:00
using Surface = std::shared_ptr<CachedSurface>;
2019-05-07 16:57:16 +02:00
using View = std::shared_ptr<CachedSurfaceView>;
using TextureCacheBase = VideoCommon::TextureCache<Surface, View>;
2019-05-07 16:57:16 +02:00
class CachedSurface final : public VideoCommon::SurfaceBase<View> {
friend CachedSurfaceView;
public:
explicit CachedSurface(GPUVAddr gpu_addr, const SurfaceParams& params);
~CachedSurface();
2019-07-05 03:10:59 +02:00
void UploadTexture(const std::vector<u8>& staging_buffer) override;
2019-05-07 16:57:16 +02:00
void DownloadTexture(std::vector<u8>& staging_buffer) override;
2019-04-14 06:44:16 +02:00
GLenum GetTarget() const {
return target;
}
GLuint GetTexture() const {
return texture.handle;
}
protected:
void DecorateSurfaceName() override;
2019-04-17 01:01:07 +02:00
2019-05-07 16:57:16 +02:00
View CreateView(const ViewParams& view_key) override;
View CreateViewInner(const ViewParams& view_key, bool is_proxy);
private:
2019-07-05 03:10:59 +02:00
void UploadTextureMipmap(u32 level, const std::vector<u8>& staging_buffer);
GLenum internal_format{};
GLenum format{};
GLenum type{};
bool is_compressed{};
2019-04-14 06:44:16 +02:00
GLenum target{};
2019-05-07 16:57:16 +02:00
u32 view_count{};
OGLTexture texture;
OGLBuffer texture_buffer;
};
2019-05-07 16:57:16 +02:00
class CachedSurfaceView final : public VideoCommon::ViewBase {
public:
2019-07-05 03:10:59 +02:00
explicit CachedSurfaceView(CachedSurface& surface, const ViewParams& params, bool is_proxy);
~CachedSurfaceView();
/// Attaches this texture view to the current bound GL_DRAW_FRAMEBUFFER
void Attach(GLenum attachment, GLenum target) const;
2019-07-05 03:10:59 +02:00
GLuint GetTexture() const {
if (is_proxy) {
return surface.GetTexture();
}
return texture_view.handle;
}
const SurfaceParams& GetSurfaceParams() const {
2019-05-07 16:57:16 +02:00
return surface.GetSurfaceParams();
}
2019-05-07 16:57:16 +02:00
void ApplySwizzle(Tegra::Texture::SwizzleSource x_source,
Tegra::Texture::SwizzleSource y_source,
Tegra::Texture::SwizzleSource z_source,
Tegra::Texture::SwizzleSource w_source);
2019-05-07 16:57:16 +02:00
void DecorateViewName(GPUVAddr gpu_addr, std::string prefix);
private:
2019-05-07 16:57:16 +02:00
u32 EncodeSwizzle(Tegra::Texture::SwizzleSource x_source,
Tegra::Texture::SwizzleSource y_source,
Tegra::Texture::SwizzleSource z_source,
2019-05-07 16:57:16 +02:00
Tegra::Texture::SwizzleSource w_source) const {
return (static_cast<u32>(x_source) << 24) | (static_cast<u32>(y_source) << 16) |
(static_cast<u32>(z_source) << 8) | static_cast<u32>(w_source);
}
OGLTextureView CreateTextureView() const;
CachedSurface& surface;
2019-05-07 16:57:16 +02:00
GLenum target{};
OGLTextureView texture_view;
u32 swizzle;
bool is_proxy;
};
class TextureCacheOpenGL final : public TextureCacheBase {
public:
explicit TextureCacheOpenGL(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
const Device& device);
~TextureCacheOpenGL();
protected:
2019-05-07 16:57:16 +02:00
Surface CreateSurface(GPUVAddr gpu_addr, const SurfaceParams& params) override;
void ImageCopy(Surface& src_surface, Surface& dst_surface,
2019-05-07 16:57:16 +02:00
const VideoCommon::CopyParams& copy_params) override;
void ImageBlit(View& src_view, View& dst_view,
const Tegra::Engines::Fermi2D::Config& copy_config) override;
void BufferCopy(Surface& src_surface, Surface& dst_surface) override;
private:
GLuint FetchPBO(std::size_t buffer_size);
OGLFramebuffer src_framebuffer;
OGLFramebuffer dst_framebuffer;
std::unordered_map<u32, OGLBuffer> copy_pbo_cache;
};
} // namespace OpenGL