Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GSoC 2019] RetroPlayer: OpenGL back-end for shaders #114

Draft
wants to merge 5 commits into
base: feature_shaders_gl
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/treedata/linux/subdirs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ xbmc/platform/posix platform/posix
xbmc/platform/posix/filesystem platform/posix/filesystem
xbmc/platform/posix/network platform/posix/network
xbmc/platform/posix/utils platform/posix/utils
xbmc/cores/RetroPlayer/shaders/gl cores/RetroPlayer/shaders/gl
xbmc/windowing/linux windowing/linux
1 change: 1 addition & 0 deletions cmake/treedata/osx/subdirs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ xbmc/platform/posix/filesystem platform/posix/filesystem
xbmc/platform/posix/network platform/posix/network
xbmc/platform/posix/utils platform/posix/utils
xbmc/windowing/osx windowing/osx
xbmc/cores/RetroPlayer/shaders/gl cores/RetroPlayer/shaders/gl
117 changes: 82 additions & 35 deletions xbmc/cores/RetroPlayer/rendering/VideoRenderers/RPRendererOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cores/RetroPlayer/buffers/RenderBufferOpenGL.h"
#include "cores/RetroPlayer/buffers/RenderBufferPoolOpenGL.h"
#include "cores/RetroPlayer/rendering/RenderContext.h"
#include "cores/RetroPlayer/shaders/gl/ShaderPresetGL.h"
#include "utils/log.h"

#include <cstddef>
Expand Down Expand Up @@ -45,6 +46,9 @@ CRPRendererOpenGL::CRPRendererOpenGL(const CRenderSettings& renderSettings,
std::shared_ptr<IRenderBufferPool> bufferPool)
: CRPBaseRenderer(renderSettings, context, std::move(bufferPool))
{
// Initialize CRPBaseRenderer
m_shaderPreset.reset(new SHADER::CShaderPresetGL(m_context));

// Initialize CRPRendererOpenGL
m_clearColour = m_context.UseLimitedColor() ? (16.0f / 0xff) : 0.0f;

Expand Down Expand Up @@ -277,60 +281,103 @@ void CRPRendererOpenGL::Render(uint8_t alpha)

const uint32_t color = (alpha << 24) | 0xFFFFFF;

glBindTexture(m_textureTarget, renderBuffer->TextureID());
RenderBufferTextures* rbTextures;
const auto it = m_RBTexturesMap.find(renderBuffer);
if (it != m_RBTexturesMap.end())
{
rbTextures = it->second.get();
}
else
{
// We can't copy or move CGLTexture, so construct source/target in-place
rbTextures = new RenderBufferTextures{{// source texture
static_cast<unsigned int>(renderBuffer->GetWidth()),
static_cast<unsigned int>(renderBuffer->GetHeight()),
GL_RGB, renderBuffer->TextureID()},
{// target texture
static_cast<unsigned int>(m_context.GetScreenWidth()),
static_cast<unsigned int>(m_context.GetScreenHeight())}};
m_RBTexturesMap.emplace(renderBuffer, rbTextures);
}

const auto sourceTexture = &rbTextures->source;
const auto targetTexture = &rbTextures->target;

glBindTexture(m_textureTarget, sourceTexture->getMTexture());

GLint filter = GL_NEAREST;
if (GetRenderSettings().VideoSettings().GetScalingMethod() == SCALINGMETHOD::LINEAR)
filter = GL_LINEAR;

glTexParameteri(m_textureTarget, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(m_textureTarget, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

m_context.EnableGUIShader(GL_SHADER_METHOD::TEXTURE);
Updateshaders();

if (m_bUseShaderPreset)
{
const CPoint destPoints[4] = {m_rotatedDestCoords[0], m_rotatedDestCoords[1],
m_rotatedDestCoords[2], m_rotatedDestCoords[3]};

targetTexture->CreateTextureObject();

SHADER::CShaderTextureGL source(*sourceTexture);
SHADER::CShaderTextureGL target(*targetTexture);
if (!m_shaderPreset->RenderUpdate(destPoints, &source, &target))
{
m_shadersNeedUpdate = false;
m_bUseShaderPreset = false;
}
}
else
{
m_context.EnableGUIShader(GL_SHADER_METHOD::TEXTURE);

GLubyte colour[4];
GLubyte idx[4] = {0, 1, 3, 2}; // Determines order of triangle strip
PackedVertex vertex[4];
GLubyte colour[4];
GLubyte idx[4] = {0, 1, 3, 2}; // Determines order of triangle strip
PackedVertex vertex[4];

GLint uniColLoc = m_context.GUIShaderGetUniCol();
GLint uniColLoc = m_context.GUIShaderGetUniCol();

// Setup color values
colour[0] = static_cast<GLubyte>(GET_R(color));
colour[1] = static_cast<GLubyte>(GET_G(color));
colour[2] = static_cast<GLubyte>(GET_B(color));
colour[3] = static_cast<GLubyte>(GET_A(color));
// Setup color values
colour[0] = static_cast<GLubyte>(GET_R(color));
colour[1] = static_cast<GLubyte>(GET_G(color));
colour[2] = static_cast<GLubyte>(GET_B(color));
colour[3] = static_cast<GLubyte>(GET_A(color));

for (unsigned int i = 0; i < 4; i++)
{
// Setup vertex position values
vertex[i].x = m_rotatedDestCoords[i].x;
vertex[i].y = m_rotatedDestCoords[i].y;
vertex[i].z = 0.0f;
}
for (unsigned int i = 0; i < 4; i++)
{
// Setup vertex position values
vertex[i].x = m_rotatedDestCoords[i].x;
vertex[i].y = m_rotatedDestCoords[i].y;
vertex[i].z = 0.0f;
}

// Setup texture coordinates
vertex[0].u1 = vertex[3].u1 = rect.x1;
vertex[0].v1 = vertex[1].v1 = rect.y1;
vertex[1].u1 = vertex[2].u1 = rect.x2;
vertex[2].v1 = vertex[3].v1 = rect.y2;
// Setup texture coordinates
vertex[0].u1 = vertex[3].u1 = rect.x1;
vertex[0].v1 = vertex[1].v1 = rect.y1;
vertex[1].u1 = vertex[2].u1 = rect.x2;
vertex[2].v1 = vertex[3].v1 = rect.y2;

glBindVertexArray(m_mainVAO);
glBindVertexArray(m_mainVAO);

glBindBuffer(GL_ARRAY_BUFFER, m_mainVertexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(PackedVertex) * 4, &vertex[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_mainVertexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(PackedVertex) * 4, &vertex[0], GL_STATIC_DRAW);

// No need to bind the index VBO, it's part of VAO state
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 4, idx, GL_STATIC_DRAW);
// No need to bind the index VBO, it's part of VAO state
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 4, idx, GL_STATIC_DRAW);

glUniform4f(uniColLoc, (colour[0] / 255.0f), (colour[1] / 255.0f), (colour[2] / 255.0f),
(colour[3] / 255.0f));
glUniform4f(uniColLoc, (colour[0] / 255.0f), (colour[1] / 255.0f), (colour[2] / 255.0f),
(colour[3] / 255.0f));

glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, 0);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, 0);

// Unbind VAO/VBO just to be safe
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Unbind VAO/VBO just to be safe
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

m_context.DisableGUIShader();
m_context.DisableGUIShader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#include "RPBaseRenderer.h"
#include "cores/RetroPlayer/process/RPProcessInfo.h"
#include "guilib/TextureGL.h"

#include <map>
#include <memory>

#include "system_gl.h"

Expand All @@ -18,6 +22,7 @@ namespace KODI
namespace RETRO
{
class CRenderContext;
class CRenderBufferOpenGL;

class CRendererFactoryOpenGL : public IRendererFactory
{
Expand Down Expand Up @@ -58,6 +63,11 @@ class CRPRendererOpenGL : public CRPBaseRenderer
float y;
float z;
};
struct RenderBufferTextures
{
CGLTexture source;
CGLTexture target;
};

// implementation of CRPBaseRenderer
void RenderInternal(bool clear, uint8_t alpha) override;
Expand All @@ -78,6 +88,8 @@ class CRPRendererOpenGL : public CRPBaseRenderer

virtual void Render(uint8_t alpha);

std::map<CRenderBufferOpenGL*, std::unique_ptr<RenderBufferTextures>> m_RBTexturesMap;

GLuint m_mainVAO;
GLuint m_mainVertexVBO;
GLuint m_mainIndexVBO;
Expand Down
9 changes: 0 additions & 9 deletions xbmc/cores/RetroPlayer/shaders/IShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ class IShader
*/
virtual bool CreateVertexBuffer(unsigned vertCount, unsigned vertSize) = 0;

/*!
* \brief Creates the data layout of the input-assembler stage
* \param layout Description of the inputs to the vertex shader
* \param numElements Number of inputs to the vertex shader
* \return False if creating the input layout failed, true otherwise.
*/
// TODO: the first argument is DX-specific (maybe the entire function is)
virtual bool CreateInputLayout(D3D11_INPUT_ELEMENT_DESC* layout, unsigned numElements) = 0;

/*!
* \brief Creates the buffer that will be used to send "input" (as per the spec) data to the
* shader \return False if creating the input buffer failed, true otherwise.
Expand Down
16 changes: 16 additions & 0 deletions xbmc/cores/RetroPlayer/shaders/gl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(SOURCES ShaderGL.cpp
ShaderLutGL.cpp
ShaderPresetGL.cpp
ShaderTextureGL.cpp
ShaderUtilsGL.cpp
)

set(HEADERS ShaderGL.h
ShaderLutGL.h
ShaderPresetGL.h
ShaderTextureGL.h
ShaderTypesGL.h
ShaderUtilsGL.h
)

core_add_library(rp-shaders-gl)
Loading