Merge pull request #15886 from hrydgard/refactor-depal

Rename DepalShaderCache to TextureShaderCache
This commit is contained in:
Henrik Rydgård 2022-08-22 13:30:55 +02:00 committed by GitHub
commit 0e780be539
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 255 additions and 310 deletions

View File

@ -1536,8 +1536,8 @@ set(GPU_SOURCES
${GPU_NEON}
GPU/Common/Draw2D.cpp
GPU/Common/Draw2D.h
GPU/Common/DepalettizeCommon.cpp
GPU/Common/DepalettizeCommon.h
GPU/Common/TextureShaderCommon.cpp
GPU/Common/TextureShaderCommon.h
GPU/Common/DepalettizeShaderCommon.cpp
GPU/Common/DepalettizeShaderCommon.h
GPU/Common/FragmentShaderGenerator.cpp

View File

@ -1,182 +0,0 @@
// Copyright (c) 2014- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
#include <map>
#include <vector>
#include <string>
#include "Common/CommonTypes.h"
#include "Common/GPU/Shader.h"
#include "Common/GPU/thin3d.h"
#include "GPU/ge_constants.h"
#include "GPU/Common/Draw2D.h"
#include "GPU/Common/ShaderCommon.h"
#include "GPU/Common/DepalettizeShaderCommon.h"
class DepalShader {
public:
Draw::ShaderModule *fragShader;
Draw::Pipeline *pipeline;
std::string code;
};
class DepalTexture {
public:
Draw::Texture *texture;
int lastFrame;
};
// Caches both shaders and palette textures.
class DepalShaderCache {
public:
DepalShaderCache(Draw::DrawContext *draw);
~DepalShaderCache();
// This also uploads the palette and binds the correct texture.
DepalShader *GetDepalettizeShader(uint32_t clutMode, GETextureFormat texFormat, GEBufferFormat pixelFormat);
Draw::Texture *GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut);
Draw::SamplerState *GetSampler();
void Clear();
void Decimate();
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
void DeviceLost();
void DeviceRestore(Draw::DrawContext *draw);
private:
static uint32_t GenerateShaderID(uint32_t clutMode, GETextureFormat texFormat, GEBufferFormat pixelFormat) {
return (clutMode & 0xFFFFFF) | (pixelFormat << 24) | (texFormat << 28);
}
static uint32_t GetClutID(GEPaletteFormat clutFormat, uint32_t clutHash) {
// Simplistic.
return clutHash ^ (uint32_t)clutFormat;
}
Draw::DrawContext *draw_;
Draw::ShaderModule *vertexShader_ = nullptr;
Draw::SamplerState *nearestSampler_ = nullptr;
std::map<u32, DepalShader *> cache_;
std::map<u32, DepalTexture *> texCache_;
};
// TODO: Merge with DepalShaderCache?
class TextureShaderApplier {
public:
struct Pos {
float x;
float y;
};
struct UV {
float u;
float v;
};
TextureShaderApplier(Draw::DrawContext *draw, DepalShader *shader, float bufferW, float bufferH, int renderW, int renderH)
: draw_(draw), shader_(shader), bufferW_(bufferW), bufferH_(bufferH), renderW_(renderW), renderH_(renderH) {
static const Pos pos[4] = {
{-1, -1 },
{ 1, -1 },
{-1, 1 },
{ 1, 1 },
};
memcpy(pos_, pos, sizeof(pos_));
static const UV uv[4] = {
{ 0, 0 },
{ 1, 0 },
{ 0, 1 },
{ 1, 1 },
};
memcpy(uv_, uv, sizeof(uv_));
}
void ApplyBounds(const KnownVertexBounds &bounds, u32 uoff, u32 voff) {
// If min is not < max, then we don't have values (wasn't set during decode.)
if (bounds.minV < bounds.maxV) {
const float invWidth = 1.0f / bufferW_;
const float invHeight = 1.0f / bufferH_;
// Inverse of half = double.
const float invHalfWidth = invWidth * 2.0f;
const float invHalfHeight = invHeight * 2.0f;
const int u1 = bounds.minU + uoff;
const int v1 = bounds.minV + voff;
const int u2 = bounds.maxU + uoff;
const int v2 = bounds.maxV + voff;
const float left = u1 * invHalfWidth - 1.0f;
const float right = u2 * invHalfWidth - 1.0f;
const float top = v1 * invHalfHeight - 1.0f;
const float bottom = v2 * invHalfHeight - 1.0f;
// Points are: BL, BR, TR, TL.
pos_[0] = Pos{ left, bottom };
pos_[1] = Pos{ right, bottom };
pos_[2] = Pos{ left, top };
pos_[3] = Pos{ right, top };
// And also the UVs, same order.
const float uvleft = u1 * invWidth;
const float uvright = u2 * invWidth;
const float uvtop = v1 * invHeight;
const float uvbottom = v2 * invHeight;
uv_[0] = UV{ uvleft, uvbottom };
uv_[1] = UV{ uvright, uvbottom };
uv_[2] = UV{ uvleft, uvtop };
uv_[3] = UV{ uvright, uvtop };
// We need to reapply the texture next time since we cropped UV.
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
}
}
void Use() {
draw_->BindPipeline(shader_->pipeline);
struct SimpleVertex {
float pos[2];
float uv[2];
};
for (int i = 0; i < 4; i++) {
memcpy(&verts_[i].x, &pos_[i], sizeof(Pos));
memcpy(&verts_[i].u, &uv_[i], sizeof(UV));
}
}
void Shade() {
Draw::Viewport vp{ 0.0f, 0.0f, (float)renderW_, (float)renderH_, 0.0f, 1.0f };
draw_->SetViewports(1, &vp);
draw_->SetScissorRect(0, 0, renderW_, renderH_);
draw_->DrawUP((const uint8_t *)verts_, 4);
}
protected:
Draw::DrawContext *draw_;
DepalShader *shader_;
Pos pos_[4];
UV uv_[4];
Draw2DVertex verts_[4];
float bufferW_;
float bufferH_;
int renderW_;
int renderH_;
};

View File

@ -34,7 +34,7 @@ static const InputDef vsInputs[2] = {
{ "vec2", "a_texcoord0", Draw::SEM_TEXCOORD0, },
};
// TODO: Deduplicate with DepalettizeCommon.cpp
// TODO: Deduplicate with TextureShaderCommon.cpp
static const SamplerDef samplers[2] = {
{ "tex" },
{ "pal" },
@ -309,7 +309,7 @@ void GenerateDepalFs(char *buffer, const DepalConfig &config, const ShaderLangua
writer.EndFSMain("outColor", FSFLAG_NONE);
}
void GenerateDepalVs(char *buffer, const ShaderLanguageDesc &lang) {
void GenerateVs(char *buffer, const ShaderLanguageDesc &lang) {
ShaderWriter writer(buffer, lang, ShaderStage::Vertex, nullptr, 0);
writer.BeginVSMain(vsInputs, Slice<UniformDef>::empty(), varyings);
writer.C(" v_texcoord = a_texcoord0;\n");

View File

@ -34,4 +34,4 @@ struct DepalConfig {
};
void GenerateDepalFs(char *buffer, const DepalConfig &config, const ShaderLanguageDesc &lang);
void GenerateDepalVs(char *buffer, const ShaderLanguageDesc &lang);
void GenerateVs(char *buffer, const ShaderLanguageDesc &lang);

View File

@ -29,7 +29,7 @@ enum DebugShaderType {
SHADER_TYPE_GEOMETRY = 2,
SHADER_TYPE_VERTEXLOADER = 3, // Not really a shader, but might as well re-use this mechanism
SHADER_TYPE_PIPELINE = 4, // Vulkan and DX12 combines a bunch of state into pipeline objects. Might as well make them inspectable.
SHADER_TYPE_DEPAL = 5,
SHADER_TYPE_TEXTURE = 5,
SHADER_TYPE_SAMPLER = 6, // Not really a shader either. Need to rename this enum...
};

View File

@ -124,11 +124,11 @@ TextureCacheCommon::TextureCacheCommon(Draw::DrawContext *draw)
replacer_.Init();
depalShaderCache_ = new DepalShaderCache(draw);
textureShaderCache_ = new TextureShaderCache(draw);
}
TextureCacheCommon::~TextureCacheCommon() {
delete depalShaderCache_;
delete textureShaderCache_;
FreeAlignedMemory(clutBufConverted_);
FreeAlignedMemory(clutBufRaw_);
@ -1860,7 +1860,7 @@ bool CanDepalettize(GETextureFormat texFormat, GEBufferFormat bufferFormat) {
}
void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, RasterChannel channel) {
DepalShader *depalShader = nullptr;
TextureShader *textureShader = nullptr;
uint32_t clutMode = gstate.clutformat & 0xFFFFFF;
bool depth = channel == RASTER_DEPTH;
@ -1886,7 +1886,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
// Very icky conflation here of native and thin3d rendering. This will need careful work per backend in BindAsClutTexture.
Draw::Texture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
Draw::Texture *clutTexture = textureShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
BindAsClutTexture(clutTexture);
framebufferManager_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
@ -1913,13 +1913,13 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
return;
}
depalShader = depalShaderCache_->GetDepalettizeShader(clutMode, texFormat, depth ? GE_FORMAT_DEPTH16 : framebuffer->drawnFormat);
textureShader = textureShaderCache_->GetDepalettizeShader(clutMode, texFormat, depth ? GE_FORMAT_DEPTH16 : framebuffer->drawnFormat);
gstate_c.SetUseShaderDepal(false);
}
if (depalShader) {
if (textureShader) {
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
Draw::Texture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
Draw::Texture *clutTexture = textureShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
Draw::Framebuffer *depalFBO = framebufferManager_->GetTempFBO(TempFBO::DEPAL, framebuffer->renderWidth, framebuffer->renderHeight);
draw_->BindTexture(0, nullptr);
draw_->BindTexture(1, nullptr);
@ -1929,17 +1929,16 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
Draw::Viewport vp{ 0.0f, 0.0f, (float)framebuffer->renderWidth, (float)framebuffer->renderHeight, 0.0f, 1.0f };
draw_->SetViewports(1, &vp);
TextureShaderApplier shaderApply(draw_, depalShader, framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight);
shaderApply.ApplyBounds(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset);
shaderApply.Use();
draw_->BindFramebufferAsTexture(framebuffer->fbo, 0, depth ? Draw::FB_DEPTH_BIT : Draw::FB_COLOR_BIT, 0);
draw_->BindTexture(1, clutTexture);
Draw::SamplerState *nearest = depalShaderCache_->GetSampler();
Draw::SamplerState *nearest = textureShaderCache_->GetSampler();
draw_->BindSamplerStates(0, 1, &nearest);
draw_->BindSamplerStates(1, 1, &nearest);
shaderApply.Shade();
textureShaderCache_->ApplyShader(textureShader,
framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight,
gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset);
draw_->BindTexture(0, nullptr);
framebufferManager_->RebindFramebuffer("ApplyTextureFramebuffer");
@ -1971,7 +1970,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
}
void TextureCacheCommon::Clear(bool delete_them) {
depalShaderCache_->Clear();
textureShaderCache_->Clear();
ForgetLastTexture();
for (TexCache::iterator iter = cache_.begin(); iter != cache_.end(); ++iter) {
@ -2413,5 +2412,5 @@ CheckAlphaResult TextureCacheCommon::CheckCLUTAlpha(const uint8_t *pixelData, GE
}
void TextureCacheCommon::StartFrame() {
depalShaderCache_->Decimate();
textureShaderCache_->Decimate();
}

View File

@ -29,7 +29,7 @@
#include "GPU/Common/GPUDebugInterface.h"
#include "GPU/Common/TextureDecoder.h"
#include "GPU/Common/TextureScalerCommon.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
enum FramebufferNotification {
NOTIFY_FB_CREATED,
@ -291,7 +291,7 @@ public:
void InvalidateAll(GPUInvalidationType type);
void ClearNextFrame();
DepalShaderCache *GetDepalShaderCache() { return depalShaderCache_; }
TextureShaderCache *GetTextureShaderCache() { return textureShaderCache_; }
virtual void ForgetLastTexture() = 0;
virtual void InvalidateLastTexture() = 0;
@ -403,7 +403,7 @@ protected:
TextureReplacer replacer_;
TextureScalerCommon scaler_;
FramebufferManagerCommon *framebufferManager_;
DepalShaderCache *depalShaderCache_;
TextureShaderCache *textureShaderCache_;
ShaderManagerCommon *shaderManager_;
bool clearCacheNextFrame_ = false;

View File

@ -25,8 +25,8 @@
#include "Core/Reporting.h"
#include "GPU/Common/DrawEngineCommon.h"
#include "GPU/Common/TextureCacheCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Common/DepalettizeShaderCommon.h"
#include "GPU/Common/DepalettizeCommon.h"
static const VaryingDef varyings[1] = {
{ "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" },
@ -37,22 +37,23 @@ static const SamplerDef samplers[2] = {
{ "pal" },
};
DepalShaderCache::DepalShaderCache(Draw::DrawContext *draw) : draw_(draw) { }
TextureShaderCache::TextureShaderCache(Draw::DrawContext *draw) : draw_(draw) { }
DepalShaderCache::~DepalShaderCache() {
TextureShaderCache::~TextureShaderCache() {
DeviceLost();
}
void DepalShaderCache::DeviceRestore(Draw::DrawContext *draw) {
void TextureShaderCache::DeviceRestore(Draw::DrawContext *draw) {
draw_ = draw;
}
void DepalShaderCache::DeviceLost() {
void TextureShaderCache::DeviceLost() {
Clear();
}
Draw::Texture *DepalShaderCache::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
u32 clutId = GetClutID(clutFormat, clutHash);
Draw::Texture *TextureShaderCache::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
// Simplistic, but works well enough.
u32 clutId = clutHash ^ (uint32_t)clutFormat;
auto oldtex = texCache_.find(clutId);
if (oldtex != texCache_.end()) {
@ -62,7 +63,7 @@ Draw::Texture *DepalShaderCache::GetClutTexture(GEPaletteFormat clutFormat, cons
int texturePixels = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;
DepalTexture *tex = new DepalTexture();
ClutTexture *tex = new ClutTexture();
Draw::TextureDesc desc{};
desc.width = texturePixels;
@ -100,15 +101,14 @@ Draw::Texture *DepalShaderCache::GetClutTexture(GEPaletteFormat clutFormat, cons
return tex->texture;
}
void DepalShaderCache::Clear() {
for (auto shader = cache_.begin(); shader != cache_.end(); ++shader) {
shader->second->fragShader->Release();
void TextureShaderCache::Clear() {
for (auto shader = depalCache_.begin(); shader != depalCache_.end(); ++shader) {
if (shader->second->pipeline) {
shader->second->pipeline->Release();
}
delete shader->second;
}
cache_.clear();
depalCache_.clear();
for (auto tex = texCache_.begin(); tex != texCache_.end(); ++tex) {
tex->second->texture->Release();
delete tex->second;
@ -124,7 +124,7 @@ void DepalShaderCache::Clear() {
}
}
void DepalShaderCache::Decimate() {
void TextureShaderCache::Decimate() {
for (auto tex = texCache_.begin(); tex != texCache_.end(); ) {
if (tex->second->lastFrame + DEPAL_TEXTURE_OLD_AGE < gpuStats.numFlips) {
tex->second->texture->Release();
@ -136,7 +136,7 @@ void DepalShaderCache::Decimate() {
}
}
Draw::SamplerState *DepalShaderCache::GetSampler() {
Draw::SamplerState *TextureShaderCache::GetSampler() {
if (!nearestSampler_) {
Draw::SamplerStateDesc desc{};
desc.wrapU = Draw::TextureAddressMode::CLAMP_TO_EDGE;
@ -147,39 +147,16 @@ Draw::SamplerState *DepalShaderCache::GetSampler() {
return nearestSampler_;
}
DepalShader *DepalShaderCache::GetDepalettizeShader(uint32_t clutMode, GETextureFormat textureFormat, GEBufferFormat bufferFormat) {
TextureShader *TextureShaderCache::CreateShader(const char *fs) {
using namespace Draw;
u32 id = GenerateShaderID(clutMode, textureFormat, bufferFormat);
auto shader = cache_.find(id);
if (shader != cache_.end()) {
DepalShader *depal = shader->second;
return shader->second;
}
char *buffer = new char[4096];
if (!vertexShader_) {
GenerateDepalVs(buffer, draw_->GetShaderLanguageDesc());
char *buffer = new char[4096];
GenerateVs(buffer, draw_->GetShaderLanguageDesc());
vertexShader_ = draw_->CreateShaderModule(ShaderStage::Vertex, draw_->GetShaderLanguageDesc().shaderLanguage, (const uint8_t *)buffer, strlen(buffer), "depal_vs");
delete[] buffer;
}
// TODO: Parse these out of clutMode some nice way, to become a bit more stateless.
DepalConfig config;
config.clutFormat = gstate.getClutPaletteFormat();
config.startPos = gstate.getClutIndexStartPos();
config.shift = gstate.getClutIndexShift();
config.mask = gstate.getClutIndexMask();
config.bufferFormat = bufferFormat;
config.textureFormat = textureFormat;
GenerateDepalFs(buffer, config, draw_->GetShaderLanguageDesc());
std::string src(buffer);
ShaderModule *fragShader = draw_->CreateShaderModule(ShaderStage::Fragment, draw_->GetShaderLanguageDesc().shaderLanguage, (const uint8_t *)buffer, strlen(buffer), "depal_fs");
DepalShader *depal = new DepalShader();
ShaderModule *fragShader = draw_->CreateShaderModule(ShaderStage::Fragment, draw_->GetShaderLanguageDesc().shaderLanguage, (const uint8_t *)fs, strlen(fs), "depal_fs");
static const InputLayoutDesc desc = {
{
@ -201,38 +178,67 @@ DepalShader *DepalShaderCache::GetDepalettizeShader(uint32_t clutMode, GETexture
{ vertexShader_, fragShader },
inputLayout, noDepthStencil, blendOff, rasterNoCull, nullptr, samplers
};
Pipeline *pipeline = draw_->CreateGraphicsPipeline(depalPipelineDesc);
inputLayout->Release();
blendOff->Release();
noDepthStencil->Release();
rasterNoCull->Release();
fragShader->Release();
_assert_(pipeline);
TextureShader *depal = new TextureShader();
depal->pipeline = pipeline;
depal->fragShader = fragShader;
depal->code = buffer;
cache_[id] = depal;
delete[] buffer;
return depal->pipeline ? depal : nullptr;
depal->code = fs; // to std::string
return depal;
}
std::vector<std::string> DepalShaderCache::DebugGetShaderIDs(DebugShaderType type) {
TextureShader *TextureShaderCache::GetDepalettizeShader(uint32_t clutMode, GETextureFormat textureFormat, GEBufferFormat bufferFormat) {
using namespace Draw;
// Generate an ID for depal shaders.
u32 id = (clutMode & 0xFFFFFF) | (textureFormat << 24) | (bufferFormat << 28);
auto shader = depalCache_.find(id);
if (shader != depalCache_.end()) {
TextureShader *depal = shader->second;
return shader->second;
}
// TODO: Parse these out of clutMode some nice way, to become a bit more stateless.
DepalConfig config;
config.clutFormat = gstate.getClutPaletteFormat();
config.startPos = gstate.getClutIndexStartPos();
config.shift = gstate.getClutIndexShift();
config.mask = gstate.getClutIndexMask();
config.bufferFormat = bufferFormat;
config.textureFormat = textureFormat;
char *buffer = new char[4096];
GenerateDepalFs(buffer, config, draw_->GetShaderLanguageDesc());
TextureShader *ts = CreateShader(buffer);
delete[] buffer;
depalCache_[id] = ts;
return ts->pipeline ? ts : nullptr;
}
std::vector<std::string> TextureShaderCache::DebugGetShaderIDs(DebugShaderType type) {
std::vector<std::string> ids;
for (auto &iter : cache_) {
for (auto &iter : depalCache_) {
ids.push_back(StringFromFormat("%08x", iter.first));
}
return ids;
}
std::string DepalShaderCache::DebugGetShaderString(std::string idstr, DebugShaderType type, DebugShaderStringType stringType) {
std::string TextureShaderCache::DebugGetShaderString(std::string idstr, DebugShaderType type, DebugShaderStringType stringType) {
uint32_t id;
sscanf(idstr.c_str(), "%08x", &id);
auto iter = cache_.find(id);
if (iter == cache_.end())
auto iter = depalCache_.find(id);
if (iter == depalCache_.end())
return "";
switch (stringType) {
case SHADER_STRING_SHORT_DESC:
@ -243,3 +249,51 @@ std::string DepalShaderCache::DebugGetShaderString(std::string idstr, DebugShade
return "";
}
}
void TextureShaderCache::ApplyShader(TextureShader *shader, float bufferW, float bufferH, int renderW, int renderH, const KnownVertexBounds &bounds, u32 uoff, u32 voff) {
Draw2DVertex verts[4] = {
{-1, -1, 0, 0 },
{ 1, -1, 1, 0 },
{-1, 1, 0, 1 },
{ 1, 1, 1, 1 },
};
// If min is not < max, then we don't have values (wasn't set during decode.)
if (bounds.minV < bounds.maxV) {
const float invWidth = 1.0f / bufferW;
const float invHeight = 1.0f / bufferH;
// Inverse of half = double.
const float invHalfWidth = invWidth * 2.0f;
const float invHalfHeight = invHeight * 2.0f;
const int u1 = bounds.minU + uoff;
const int v1 = bounds.minV + voff;
const int u2 = bounds.maxU + uoff;
const int v2 = bounds.maxV + voff;
const float left = u1 * invHalfWidth - 1.0f;
const float right = u2 * invHalfWidth - 1.0f;
const float top = v1 * invHalfHeight - 1.0f;
const float bottom = v2 * invHalfHeight - 1.0f;
const float uvleft = u1 * invWidth;
const float uvright = u2 * invWidth;
const float uvtop = v1 * invHeight;
const float uvbottom = v2 * invHeight;
// Points are: BL, BR, TR, TL.
verts[0] = Draw2DVertex{ left, bottom, uvleft, uvbottom };
verts[1] = Draw2DVertex{ right, bottom, uvright, uvbottom };
verts[2] = Draw2DVertex{ left, top, uvleft, uvtop };
verts[3] = Draw2DVertex{ right, top, uvright, uvtop };
// We need to reapply the texture next time since we cropped UV.
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
}
Draw::Viewport vp{ 0.0f, 0.0f, (float)renderW, (float)renderH, 0.0f, 1.0f };
draw_->BindPipeline(shader->pipeline);
draw_->SetViewports(1, &vp);
draw_->SetScissorRect(0, 0, renderW, renderH);
draw_->DrawUP((const uint8_t *)verts, 4);
}

View File

@ -0,0 +1,75 @@
// Copyright (c) 2014- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
#include <map>
#include <vector>
#include <string>
#include "Common/CommonTypes.h"
#include "Common/GPU/Shader.h"
#include "Common/GPU/thin3d.h"
#include "GPU/ge_constants.h"
#include "GPU/Common/Draw2D.h"
#include "GPU/Common/ShaderCommon.h"
#include "GPU/Common/DepalettizeShaderCommon.h"
class TextureShader {
public:
Draw::Pipeline *pipeline;
std::string code;
};
class ClutTexture {
public:
Draw::Texture *texture;
int lastFrame;
};
// For CLUT depal shaders, and other pre-bind texture shaders.
// Caches both shaders and palette textures.
class TextureShaderCache {
public:
TextureShaderCache(Draw::DrawContext *draw);
~TextureShaderCache();
TextureShader *GetDepalettizeShader(uint32_t clutMode, GETextureFormat texFormat, GEBufferFormat pixelFormat);
Draw::Texture *GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut);
Draw::SamplerState *GetSampler();
void ApplyShader(TextureShader *shader, float bufferW, float bufferH, int renderW, int renderH, const KnownVertexBounds &bounds, u32 uoff, u32 voff);
void Clear();
void Decimate();
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
void DeviceLost();
void DeviceRestore(Draw::DrawContext *draw);
private:
TextureShader *CreateShader(const char *fs);
Draw::DrawContext *draw_;
Draw::ShaderModule *vertexShader_ = nullptr;
Draw::SamplerState *nearestSampler_ = nullptr;
std::map<u32, TextureShader *> depalCache_;
std::map<u32, ClutTexture *> texCache_;
};

View File

@ -332,8 +332,8 @@ std::vector<std::string> GPU_D3D11::DebugGetShaderIDs(DebugShaderType type) {
switch (type) {
case SHADER_TYPE_VERTEXLOADER:
return drawEngine_.DebugGetVertexLoaderIDs();
case SHADER_TYPE_DEPAL:
return textureCache_->GetDepalShaderCache()->DebugGetShaderIDs(type);
case SHADER_TYPE_TEXTURE:
return textureCache_->GetTextureShaderCache()->DebugGetShaderIDs(type);
default:
return shaderManagerD3D11_->DebugGetShaderIDs(type);
}
@ -343,8 +343,8 @@ std::string GPU_D3D11::DebugGetShaderString(std::string id, DebugShaderType type
switch (type) {
case SHADER_TYPE_VERTEXLOADER:
return drawEngine_.DebugGetVertexLoaderString(id, stringType);
case SHADER_TYPE_DEPAL:
return textureCache_->GetDepalShaderCache()->DebugGetShaderString(id, type, stringType);
case SHADER_TYPE_TEXTURE:
return textureCache_->GetTextureShaderCache()->DebugGetShaderString(id, type, stringType);
default:
return shaderManagerD3D11_->DebugGetShaderString(id, type, stringType);
}

View File

@ -23,7 +23,7 @@
#include "GPU/GPUCommon.h"
#include "GPU/D3D11/DrawEngineD3D11.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Common/VertexDecoderCommon.h"
class FramebufferManagerD3D11;

View File

@ -31,7 +31,7 @@
#include "GPU/D3D11/TextureCacheD3D11.h"
#include "GPU/D3D11/FramebufferManagerD3D11.h"
#include "GPU/D3D11/ShaderManagerD3D11.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/D3D11/D3D11Util.h"
#include "GPU/Common/FramebufferManagerCommon.h"
#include "GPU/Common/TextureDecoder.h"

View File

@ -28,7 +28,7 @@
struct VirtualFramebuffer;
class FramebufferManagerD3D11;
class DepalShaderCache;
class TextureShaderCache;
class ShaderManagerD3D11;
class SamplerCacheD3D11 {

View File

@ -375,8 +375,8 @@ std::vector<std::string> GPU_DX9::DebugGetShaderIDs(DebugShaderType type) {
switch (type) {
case SHADER_TYPE_VERTEXLOADER:
return drawEngine_.DebugGetVertexLoaderIDs();
case SHADER_TYPE_DEPAL:
return textureCache_->GetDepalShaderCache()->DebugGetShaderIDs(type);
case SHADER_TYPE_TEXTURE:
return textureCache_->GetTextureShaderCache()->DebugGetShaderIDs(type);
default:
return shaderManagerDX9_->DebugGetShaderIDs(type);
}
@ -386,8 +386,8 @@ std::string GPU_DX9::DebugGetShaderString(std::string id, DebugShaderType type,
switch (type) {
case SHADER_TYPE_VERTEXLOADER:
return drawEngine_.DebugGetVertexLoaderString(id, stringType);
case SHADER_TYPE_DEPAL:
return textureCache_->GetDepalShaderCache()->DebugGetShaderString(id, type, stringType);
case SHADER_TYPE_TEXTURE:
return textureCache_->GetTextureShaderCache()->DebugGetShaderString(id, type, stringType);
default:
return shaderManagerDX9_->DebugGetShaderString(id, type, stringType);
}

View File

@ -23,7 +23,7 @@
#include "GPU/GPUCommon.h"
#include "GPU/Directx9/FramebufferManagerDX9.h"
#include "GPU/Directx9/DrawEngineDX9.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Common/VertexDecoderCommon.h"
class ShaderManagerDX9;

View File

@ -28,7 +28,7 @@
#include "GPU/Directx9/FramebufferManagerDX9.h"
#include "GPU/Directx9/ShaderManagerDX9.h"
#include "Common/GPU/D3D9/D3D9StateCache.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Common/FramebufferManagerCommon.h"
#include "GPU/Common/TextureDecoder.h"
#include "Core/Config.h"

View File

@ -24,7 +24,7 @@
#include "GPU/Common/TextureCacheCommon.h"
struct VirtualFramebuffer;
class DepalShaderCache;
class TextureShaderCache;
class FramebufferManagerDX9;
class ShaderManagerDX9;

View File

@ -453,8 +453,8 @@ std::vector<std::string> GPU_GLES::DebugGetShaderIDs(DebugShaderType type) {
switch (type) {
case SHADER_TYPE_VERTEXLOADER:
return drawEngine_.DebugGetVertexLoaderIDs();
case SHADER_TYPE_DEPAL:
return textureCache_->GetDepalShaderCache()->DebugGetShaderIDs(type);
case SHADER_TYPE_TEXTURE:
return textureCache_->GetTextureShaderCache()->DebugGetShaderIDs(type);
default:
return shaderManagerGL_->DebugGetShaderIDs(type);
}
@ -464,8 +464,8 @@ std::string GPU_GLES::DebugGetShaderString(std::string id, DebugShaderType type,
switch (type) {
case SHADER_TYPE_VERTEXLOADER:
return drawEngine_.DebugGetVertexLoaderString(id, stringType);
case SHADER_TYPE_DEPAL:
return textureCache_->GetDepalShaderCache()->DebugGetShaderString(id, type, stringType);
case SHADER_TYPE_TEXTURE:
return textureCache_->GetTextureShaderCache()->DebugGetShaderString(id, type, stringType);
default:
return shaderManagerGL_->DebugGetShaderString(id, type, stringType);
}

View File

@ -23,7 +23,7 @@
#include "Common/File/Path.h"
#include "GPU/GPUCommon.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/GLES/FramebufferManagerGLES.h"
#include "GPU/GLES/DrawEngineGLES.h"
#include "GPU/GLES/FragmentTestCacheGLES.h"

View File

@ -36,7 +36,7 @@
#include "GPU/GLES/TextureCacheGLES.h"
#include "GPU/GLES/FramebufferManagerGLES.h"
#include "GPU/Common/FragmentShaderGenerator.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/GLES/ShaderManagerGLES.h"
#include "GPU/GLES/DrawEngineGLES.h"
#include "GPU/Common/TextureDecoder.h"
@ -430,7 +430,7 @@ bool TextureCacheGLES::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level)
}
void TextureCacheGLES::DeviceLost() {
depalShaderCache_->DeviceLost();
textureShaderCache_->DeviceLost();
Clear(false);
draw_ = nullptr;
render_ = nullptr;
@ -439,5 +439,5 @@ void TextureCacheGLES::DeviceLost() {
void TextureCacheGLES::DeviceRestore(Draw::DrawContext *draw) {
draw_ = draw;
render_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
depalShaderCache_->DeviceRestore(draw);
textureShaderCache_->DeviceRestore(draw);
}

View File

@ -27,7 +27,7 @@
struct VirtualFramebuffer;
class FramebufferManagerGLES;
class DepalShaderCache;
class TextureShaderCache;
class ShaderManagerGLES;
class DrawEngineGLES;
class GLRTexture;
@ -41,8 +41,8 @@ public:
void StartFrame() override;
void SetFramebufferManager(FramebufferManagerGLES *fbManager);
void SetDepalShaderCache(DepalShaderCache *dpCache) {
depalShaderCache_ = dpCache;
void SetDepalShaderCache(TextureShaderCache *dpCache) {
textureShaderCache_ = dpCache;
}
void SetDrawEngine(DrawEngineGLES *td) {
drawEngine_ = td;

View File

@ -338,7 +338,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\ext\xbrz\xbrz.h" />
<ClInclude Include="Common\DepalettizeCommon.h" />
<ClInclude Include="Common\TextureShaderCommon.h" />
<ClInclude Include="Common\Draw2D.h" />
<ClInclude Include="Common\ReinterpretFramebuffer.h" />
<ClInclude Include="Common\DepalettizeShaderCommon.h" />
@ -452,7 +452,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\ext\xbrz\xbrz.cpp" />
<ClCompile Include="Common\DepalettizeCommon.cpp" />
<ClCompile Include="Common\TextureShaderCommon.cpp" />
<ClCompile Include="Common\Draw2D.cpp" />
<ClCompile Include="Common\ReinterpretFramebuffer.cpp" />
<ClCompile Include="Common\DepalettizeShaderCommon.cpp" />

View File

@ -255,7 +255,7 @@
<ClInclude Include="Common\Draw2D.h">
<Filter>Common</Filter>
</ClInclude>
<ClInclude Include="Common\DepalettizeCommon.h">
<ClInclude Include="Common\TextureShaderCommon.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup>
@ -503,7 +503,7 @@
<ClCompile Include="Common\Draw2D.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="Common\DepalettizeCommon.cpp">
<ClCompile Include="Common\TextureShaderCommon.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>

View File

@ -604,9 +604,8 @@ std::vector<std::string> GPU_Vulkan::DebugGetShaderIDs(DebugShaderType type) {
return drawEngine_.DebugGetVertexLoaderIDs();
} else if (type == SHADER_TYPE_PIPELINE) {
return pipelineManager_->DebugGetObjectIDs(type);
} else if (type == SHADER_TYPE_DEPAL) {
///...
return std::vector<std::string>();
} else if (type == SHADER_TYPE_TEXTURE) {
return textureCache_->GetTextureShaderCache()->DebugGetShaderIDs(type);
} else if (type == SHADER_TYPE_VERTEX || type == SHADER_TYPE_FRAGMENT) {
return shaderManagerVulkan_->DebugGetShaderIDs(type);
} else if (type == SHADER_TYPE_SAMPLER) {
@ -621,8 +620,8 @@ std::string GPU_Vulkan::DebugGetShaderString(std::string id, DebugShaderType typ
return drawEngine_.DebugGetVertexLoaderString(id, stringType);
} else if (type == SHADER_TYPE_PIPELINE) {
return pipelineManager_->DebugGetObjectString(id, type, stringType);
} else if (type == SHADER_TYPE_DEPAL) {
return "";
} else if (type == SHADER_TYPE_TEXTURE) {
return textureCache_->GetTextureShaderCache()->DebugGetShaderString(id, type, stringType);
} else if (type == SHADER_TYPE_SAMPLER) {
return textureCacheVulkan_->DebugGetSamplerString(id, stringType);
} else if (type == SHADER_TYPE_VERTEX || type == SHADER_TYPE_FRAGMENT) {

View File

@ -25,7 +25,7 @@
#include "GPU/GPUCommon.h"
#include "GPU/Vulkan/DrawEngineVulkan.h"
#include "GPU/Vulkan/PipelineManagerVulkan.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
class FramebufferManagerVulkan;
class ShaderManagerVulkan;

View File

@ -41,7 +41,7 @@
#include "GPU/ge_constants.h"
#include "GPU/GPUState.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Common/PostShader.h"
#include "GPU/Common/TextureCacheCommon.h"
#include "GPU/Common/TextureDecoder.h"
@ -200,7 +200,7 @@ void TextureCacheVulkan::SetFramebufferManager(FramebufferManagerVulkan *fbManag
}
void TextureCacheVulkan::DeviceLost() {
depalShaderCache_->DeviceLost();
textureShaderCache_->DeviceLost();
VulkanContext *vulkan = draw_ ? (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT) : nullptr;
@ -227,7 +227,7 @@ void TextureCacheVulkan::DeviceRestore(Draw::DrawContext *draw) {
_assert_(!allocator_);
samplerCache_.DeviceRestore(vulkan);
depalShaderCache_->DeviceRestore(draw);
textureShaderCache_->DeviceRestore(draw);
VkSamplerCreateInfo samp{ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };
samp.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
@ -320,7 +320,7 @@ void TextureCacheVulkan::StartFrame() {
TextureCacheCommon::StartFrame();
InvalidateLastTexture();
depalShaderCache_->Decimate();
textureShaderCache_->Decimate();
timesInvalidatedAllThisFrame_ = 0;
texelsScaledThisFrame_ = 0;

View File

@ -22,7 +22,7 @@
#include "GPU/GPUState.h"
#include "Common/GPU/Vulkan/VulkanContext.h"
#include "GPU/Common/TextureCacheCommon.h"
#include "GPU/Common/DepalettizeCommon.h"
#include "GPU/Common/TextureShaderCommon.h"
#include "GPU/Vulkan/VulkanUtil.h"
struct VirtualFramebuffer;

View File

@ -1151,7 +1151,7 @@ struct { DebugShaderType type; const char *name; } shaderTypes[] = {
// { SHADER_TYPE_GEOMETRY, "Geometry" },
{ SHADER_TYPE_VERTEXLOADER, "VertexLoader" },
{ SHADER_TYPE_PIPELINE, "Pipeline" },
{ SHADER_TYPE_DEPAL, "Depal" },
{ SHADER_TYPE_TEXTURE, "Texture" },
{ SHADER_TYPE_SAMPLER, "Sampler" },
};

View File

@ -379,7 +379,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\GPU\Common\DepalettizeCommon.h" />
<ClInclude Include="..\..\GPU\Common\TextureShaderCommon.h" />
<ClInclude Include="..\..\GPU\Common\DepalettizeShaderCommon.h" />
<ClInclude Include="..\..\GPU\Common\Draw2D.h" />
<ClInclude Include="..\..\GPU\Common\DrawEngineCommon.h" />
@ -439,7 +439,7 @@
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\GPU\Common\DepalettizeCommon.cpp" />
<ClCompile Include="..\..\GPU\Common\TextureShaderCommon.cpp" />
<ClCompile Include="..\..\GPU\Common\DepalettizeShaderCommon.cpp" />
<ClCompile Include="..\..\GPU\Common\Draw2D.cpp" />
<ClCompile Include="..\..\GPU\Common\DrawEngineCommon.cpp" />
@ -527,4 +527,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -57,7 +57,7 @@
<ClCompile Include="..\..\GPU\Common\VertexShaderGenerator.cpp" />
<ClCompile Include="..\..\GPU\Common\ReinterpretFramebuffer.cpp" />
<ClCompile Include="..\..\GPU\Common\Draw2D.cpp" />
<ClCompile Include="..\..\GPU\Common\DepalettizeCommon.cpp" />
<ClCompile Include="..\..\GPU\Common\TextureShaderCommon.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\GPU\Common\DepalettizeShaderCommon.h" />
@ -117,6 +117,6 @@
<ClInclude Include="..\..\GPU\Common\VertexShaderGenerator.h" />
<ClInclude Include="..\..\GPU\Common\ReinterpretFramebuffer.h" />
<ClInclude Include="..\..\GPU\Common\Draw2D.h" />
<ClInclude Include="..\..\GPU\Common\DepalettizeCommon.h" />
<ClInclude Include="..\..\GPU\Common\TextureShaderCommon.h" />
</ItemGroup>
</Project>
</Project>

View File

@ -319,7 +319,7 @@ EXEC_AND_LIB_FILES := \
$(SRC)/GPU/GeConstants.cpp \
$(SRC)/GPU/GeDisasm.cpp \
$(SRC)/GPU/Common/Draw2D.cpp \
$(SRC)/GPU/Common/DepalettizeCommon.cpp \
$(SRC)/GPU/Common/TextureShaderCommon.cpp \
$(SRC)/GPU/Common/DepalettizeShaderCommon.cpp \
$(SRC)/GPU/Common/FragmentShaderGenerator.cpp \
$(SRC)/GPU/Common/FramebufferManagerCommon.cpp \

View File

@ -327,7 +327,7 @@ SOURCES_CXX += \
$(GPUCOMMONDIR)/ShaderCommon.cpp \
$(GPUCOMMONDIR)/ShaderUniforms.cpp \
$(GPUCOMMONDIR)/GPUDebugInterface.cpp \
$(GPUCOMMONDIR)/DepalettizeCommon.cpp \
$(GPUCOMMONDIR)/TextureShaderCommon.cpp \
$(GPUCOMMONDIR)/DepalettizeShaderCommon.cpp \
$(GPUCOMMONDIR)/TransformCommon.cpp \
$(GPUCOMMONDIR)/IndexGenerator.cpp \

View File

@ -321,7 +321,7 @@ bool TestDepalShaders() {
printf("===\n%s\n===\n", buffer);
}
GenerateDepalVs(buffer, desc);
GenerateVs(buffer, desc);
if (!TestCompileShader(buffer, languages[k], ShaderStage::Vertex, &errorMessage)) {
printf("Error compiling depal shader:\n\n%s\n\n%s\n", LineNumberString(buffer).c_str(), errorMessage.c_str());
failed = true;