Skip to content

Commit

Permalink
[WIP] Fix multipass shaders texture scaling and filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
KOPRajs authored and garbear committed Apr 21, 2024
1 parent ea5f081 commit 3f2cc35
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
35 changes: 17 additions & 18 deletions xbmc/cores/RetroPlayer/shaders/gl/ShaderPresetGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,26 @@ bool CShaderPresetGL::RenderUpdate(const CPoint* dest,
IShader* firstShader = m_pShaders.front().get();
CShaderTextureGL* firstShaderTexture = m_pShaderTextures.front().get();
IShader* lastShader = m_pShaders.back().get();
int screenWidth = m_context.GetScreenWidth();
int screenHeight = m_context.GetScreenHeight();

const unsigned passesNum = static_cast<unsigned int>(m_pShaderTextures.size());

if (passesNum == 1)
firstShader->Render(source, target);
else if (passesNum == 2)
{
// Initialize FBO
firstShaderTexture->CreateFBO(screenWidth, screenHeight);
// Apply first pass
firstShaderTexture->BindFBO();
RenderShader(firstShader, source, target);
RenderShader(firstShader, source, firstShaderTexture);
firstShaderTexture->UnbindFBO();

// Apply last pass
RenderShader(lastShader, firstShaderTexture, target);
}
else
{
// Initialize FBO
firstShaderTexture->CreateFBO(screenWidth, screenHeight);
// Apply first pass
firstShaderTexture->BindFBO();
RenderShader(firstShader, source, target);
RenderShader(firstShader, source, firstShaderTexture);
firstShaderTexture->UnbindFBO();

// Apply all passes except the first and last one (which needs to be applied to the backbuffer)
Expand All @@ -134,14 +129,13 @@ bool CShaderPresetGL::RenderUpdate(const CPoint* dest,
IShader* shader = m_pShaders[shaderIdx].get();
CShaderTextureGL* prevTexture = m_pShaderTextures[shaderIdx - 1].get();
CShaderTextureGL* texture = m_pShaderTextures[shaderIdx].get();
texture->CreateFBO(screenWidth, screenHeight);
texture->BindFBO();
RenderShader(shader, prevTexture,
target); // The target on each call is only used for setting the viewport
texture); // The target on each call is only used for setting the viewport
texture->UnbindFBO();
}

// TODO: Remove last texture, useless
//! @todo: Remove last texture, useless
// Apply last pass
CShaderTextureGL* secToLastTexture = m_pShaderTextures[m_pShaderTextures.size() - 2].get();
RenderShader(lastShader, secToLastTexture, target);
Expand Down Expand Up @@ -297,6 +291,7 @@ bool CShaderPresetGL::CreateShaderTextures()
static_cast<unsigned int>(scaledSize.y),
static_cast<XB_FMT>(textureFormat)); //! @todo Format translation?

// textureGL->SetScalingMethod(pass.filter == FILTER_TYPE_LINEAR ? TEXTURE_SCALING::LINEAR : TEXTURE_SCALING::NEAREST);
textureGL->CreateTextureObject();

if (textureGL->getMTexture() <= 0)
Expand All @@ -305,11 +300,13 @@ bool CShaderPresetGL::CreateShaderTextures()
return false;
}

auto wrapType = CShaderUtilsGL::TranslateWrapType(WRAP_TYPE_BORDER);
auto wrapType = CShaderUtilsGL::TranslateWrapType(pass.wrap);
auto filterType = (pass.filter == FILTER_TYPE_LINEAR ? GL_LINEAR : GL_NEAREST);

glBindTexture(GL_TEXTURE_2D, textureGL->getMTexture());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, scaledSize.x, scaledSize.y, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, wrapType);
Expand Down Expand Up @@ -351,7 +348,7 @@ bool CShaderPresetGL::CreateShaders()

std::unique_ptr<CShaderGL> videoShader(new CShaderGL(m_context));

auto shaderSource = pass.vertexSource; //also contains fragment source
auto shaderSource = pass.vertexSource; // Also contains fragment source
auto shaderPath = pass.sourcePath;

// Get only the parameters belonging to this specific shader
Expand Down Expand Up @@ -439,9 +436,11 @@ void CShaderPresetGL::RenderShader(IShader* shader,
IShaderTexture* source,
IShaderTexture* target) const
{
CRect newViewPort(0.f, 0.f, target->GetWidth(), target->GetHeight());
m_context.SetViewPort(newViewPort);
m_context.SetScissors(newViewPort);
// CRect newViewPort(0.f, 0.f, target->GetWidth(), target->GetHeight());
// m_context.SetViewPort(newViewPort);
// m_context.SetScissors(newViewPort);
glViewport(0, 0, (GLsizei) target->GetWidth(), (GLsizei) target->GetHeight());
glScissor(0, 0, (GLsizei) target->GetWidth(), (GLsizei) target->GetHeight());

shader->Render(source, target);
}
Expand Down
23 changes: 12 additions & 11 deletions xbmc/cores/RetroPlayer/shaders/gl/ShaderTextureGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@
using namespace KODI;
using namespace SHADER;

bool CShaderTextureGL::CreateFBO(int width, int height)
bool CShaderTextureGL::CreateFBO()
{
if (FBO == 0)
glGenFramebuffers(1, &FBO);

return true;
}

bool CShaderTextureGL::BindFBO()
{
GLuint renderTargetID = GetPointer()->getMTexture();
if (renderTargetID == 0)
return false;

BindFBO();
if (FBO == 0)
if (!CreateFBO())
return false;

glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glBindTexture(GL_TEXTURE_2D, renderTargetID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTargetID, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
Expand All @@ -35,13 +41,8 @@ bool CShaderTextureGL::CreateFBO(int width, int height)
UnbindFBO();
return false;
}
UnbindFBO();
return true;
}

void CShaderTextureGL::BindFBO()
{
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
return true;
}

void CShaderTextureGL::UnbindFBO()
Expand Down
4 changes: 2 additions & 2 deletions xbmc/cores/RetroPlayer/shaders/gl/ShaderTextureGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class CShaderTextureGL : public IShaderTexture
float GetHeight() const override { return static_cast<float>(m_texture->GetHeight()); }

CGLTexture* GetPointer() { return m_texture; }
bool CreateFBO(int width, int height);
void BindFBO();
bool CreateFBO();
bool BindFBO();
void UnbindFBO();

private:
Expand Down
1 change: 1 addition & 0 deletions xbmc/cores/RetroPlayer/shaders/windows/ShaderDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ void CShaderDX::PrepareParameters(CPoint dest[4], bool isLastPass, uint64_t fram
v[3].z = 0;
v[3].tu = 0;
v[3].tv = 1;

UnlockVertexBuffer();
}

Expand Down
2 changes: 1 addition & 1 deletion xbmc/cores/RetroPlayer/shaders/windows/ShaderLutDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::unique_ptr<IShaderSampler> CShaderLutDX::CreateLUTSampler(RETRO::CRenderCon
D3D11_SAMPLER_DESC sampDesc;

auto wrapType = CShaderUtilsDX::TranslateWrapType(lut.wrap);
auto filterType = lut.filter ? D3D11_FILTER_MIN_MAG_MIP_LINEAR : D3D11_FILTER_MIN_MAG_MIP_POINT;
auto filterType = lut.filter == FILTER_TYPE_LINEAR ? D3D11_FILTER_MIN_MAG_MIP_LINEAR : D3D11_FILTER_MIN_MAG_MIP_POINT;

ZeroMemory(&sampDesc, sizeof(D3D11_SAMPLER_DESC));
sampDesc.Filter = filterType;
Expand Down
5 changes: 3 additions & 2 deletions xbmc/cores/RetroPlayer/shaders/windows/ShaderPresetDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ bool CShaderPresetDX::RenderUpdate(const CPoint dest[],
const unsigned passesNum = static_cast<unsigned int>(m_pShaderTextures.size());

if (passesNum == 1)
m_pShaders.front()->Render(source, target);
firstShader->Render(source, target);
else if (passesNum == 2)
{
// Apply first pass
RenderShader(firstShader, source, firstShaderTexture);

// Apply last pass
RenderShader(lastShader, firstShaderTexture, target);
}
Expand Down Expand Up @@ -342,7 +343,7 @@ bool CShaderPresetDX::CreateShaders()
// Get only the parameters belonging to this specific shader
ShaderParameterMap passParameters = GetShaderParameters(pass.parameters, pass.vertexSource);
IShaderSampler* passSampler = reinterpret_cast<IShaderSampler*>(
pass.filter
pass.filter == FILTER_TYPE_LINEAR
? m_pSampLinear
: m_pSampNearest); //! @todo Wrap in CShaderSamplerDX instead of reinterpret_cast

Expand Down

0 comments on commit 3f2cc35

Please sign in to comment.