Skip to content

Commit

Permalink
Deko3D improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Oct 30, 2024
1 parent b230e25 commit 220330c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
],
"defines": [
"__SWITCH__",
// "USE_DEKO3D",
// "BOREALIS_USE_DEKO3D"
"PLATFORM_SWITCH",
"BOREALIS_USE_DEKO3D"
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
Expand Down
79 changes: 77 additions & 2 deletions app/src/streaming/video/deko3d/DKVideoRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,43 @@

#include <array>

static const glm::vec3 gl_color_offset(bool color_full) {
static const glm::vec3 limitedOffsets = {16.0f / 255.0f, 128.0f / 255.0f,
128.0f / 255.0f};
static const glm::vec3 fullOffsets = {0.0f, 128.0f / 255.0f, 128.0f / 255.0f};
return color_full ? fullOffsets : limitedOffsets;
}

static const glm::mat3 gl_color_matrix(enum AVColorSpace color_space,
bool color_full) {
static const glm::mat3 bt601Lim = {1.1644f, 1.1644f, 1.1644f, 0.0f, -0.3917f,
2.0172f, 1.5960f, -0.8129f, 0.0f};
static const glm::mat3 bt601Full = {
1.0f, 1.0f, 1.0f, 0.0f, -0.3441f, 1.7720f, 1.4020f, -0.7141f, 0.0f};
static const glm::mat3 bt709Lim = {1.1644f, 1.1644f, 1.1644f, 0.0f, -0.2132f,
2.1124f, 1.7927f, -0.5329f, 0.0f};
static const glm::mat3 bt709Full = {
1.0f, 1.0f, 1.0f, 0.0f, -0.1873f, 1.8556f, 1.5748f, -0.4681f, 0.0f};
static const glm::mat3 bt2020Lim = {1.1644f, 1.1644f, 1.1644f,
0.0f, -0.1874f, 2.1418f,
1.6781f, -0.6505f, 0.0f};
static const glm::mat3 bt2020Full = {
1.0f, 1.0f, 1.0f, 0.0f, -0.1646f, 1.8814f, 1.4746f, -0.5714f, 0.0f};

switch (color_space) {
case AVCOL_SPC_SMPTE170M:
case AVCOL_SPC_BT470BG:
return color_full ? bt601Full : bt601Lim;
case AVCOL_SPC_BT709:
return color_full ? bt709Full : bt709Lim;
case AVCOL_SPC_BT2020_NCL:
case AVCOL_SPC_BT2020_CL:
return color_full ? bt2020Full : bt2020Lim;
default:
return bt601Lim;
}
}

namespace
{
static constexpr unsigned StaticCmdSize = 0x10000;
Expand Down Expand Up @@ -46,13 +83,25 @@ DKVideoRenderer::DKVideoRenderer() {}
DKVideoRenderer::~DKVideoRenderer() {
// Destroy the vertex buffer (not strictly needed in this case)
vertexBuffer.destroy();
transformUniformBuffer.destroy();
dkMemBlockDestroy(mappingMemblock);
}

void DKVideoRenderer::checkAndInitialize(int width, int height, AVFrame* frame) {
if (m_is_initialized) return;
// if (m_is_initialized) return;

if ((m_frame_width == frame->width) && (m_frame_height == frame->height) &&
(m_screen_width == width) && (m_screen_height == height))
return;

brls::Logger::info("{}: {} / {}", __PRETTY_FUNCTION__, width, height);

m_frame_width = frame->width;
m_frame_height = frame->height;

m_screen_width = width;
m_screen_height = height;

auto *vctx = (brls::SwitchVideoContext *)brls::Application::getPlatform()->getVideoContext();
this->dev = vctx->getDeko3dDevice();
this->queue = vctx->getQueue();
Expand Down Expand Up @@ -116,6 +165,31 @@ void DKVideoRenderer::checkAndInitialize(int width, int height, AVFrame* frame)
imageDescriptorSet->update(cmdbuf, lumaTextureId, lumaDesc);
imageDescriptorSet->update(cmdbuf, chromaTextureId, chromaDesc);

// Load the transform buffer
transformUniformBuffer = pool_data->allocate(sizeof(transformState), DK_UNIFORM_BUF_ALIGNMENT);

bool colorFull = frame->color_range == AVCOL_RANGE_JPEG;

transformState.offset = gl_color_offset(colorFull);
transformState.yuvmat = gl_color_matrix(frame->colorspace, colorFull);

float frameAspect = ((float)m_frame_height / (float)m_frame_width);
float screenAspect = ((float)m_screen_height / (float)m_screen_width);

if (frameAspect > screenAspect) {
float multiplier = frameAspect / screenAspect;
transformState.uv_data = { 0.5f - 0.5f * (1.0f / multiplier),
0.0f, multiplier, 1.0f };
} else {
float multiplier = screenAspect / frameAspect;
transformState.uv_data = { 0.0f,
0.5f - 0.5f * (1.0f / multiplier), 1.0f, multiplier };
}

cmdbuf.pushConstants(
transformUniformBuffer.getGpuAddr(), transformUniformBuffer.getSize(),
0, sizeof(transformState), &transformState);

queue.submitCommands(cmdbuf.finishList());
queue.waitIdle();

Expand Down Expand Up @@ -143,6 +217,7 @@ void DKVideoRenderer::draw(NVGcontext* vg, int width, int height, AVFrame* frame
cmdbuf.bindShaders(DkStageFlag_GraphicsMask, { vertexShader, fragmentShader });
cmdbuf.bindTextures(DkStage_Fragment, 0, dkMakeTextureHandle(lumaTextureId, 0));
cmdbuf.bindTextures(DkStage_Fragment, 1, dkMakeTextureHandle(chromaTextureId, 0));
cmdbuf.bindUniformBuffer(DkStage_Vertex, 0, transformUniformBuffer.getGpuAddr(), transformUniformBuffer.getSize());
cmdbuf.bindRasterizerState(rasterizerState);
cmdbuf.bindColorState(colorState);
cmdbuf.bindColorWriteState(colorWriteState);
Expand All @@ -155,7 +230,7 @@ void DKVideoRenderer::draw(NVGcontext* vg, int width, int height, AVFrame* frame

// Finish off this command list
queue.submitCommands(cmdbuf.finishList());
queue.waitIdle();
// queue.waitIdle();

m_video_render_stats.total_render_time += LiGetMillis() - before_render;
m_video_render_stats.rendered_frames++;
Expand Down
17 changes: 17 additions & 0 deletions app/src/streaming/video/deko3d/DKVideoRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
#include "IVideoRenderer.hpp"
#include <deko3d.hpp>

#include <glm/mat4x4.hpp>

#include <borealis.hpp>
#include <nanovg/framework/CShader.h>
#include <nanovg/framework/CExternalImage.h>
#include <nanovg/framework/CDescriptorSet.h>
#include <optional>

struct Transformation
{
glm::mat3 yuvmat;
glm::vec3 offset;
glm::vec4 uv_data;
};

class DKVideoRenderer : public IVideoRenderer {
public:
DKVideoRenderer();
Expand All @@ -23,6 +32,14 @@ class DKVideoRenderer : public IVideoRenderer {
void checkAndInitialize(int width, int height, AVFrame* frame);

bool m_is_initialized = false;

int m_frame_width = 0;
int m_frame_height = 0;
int m_screen_width = 0;
int m_screen_height = 0;

Transformation transformState;
CMemPool::Handle transformUniformBuffer;

dk::Device dev;
dk::Queue queue;
Expand Down
25 changes: 12 additions & 13 deletions app/src/streaming/video/deko3d/texture_fsh.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
layout (location = 0) in vec2 vTextureCoord;
layout (location = 0) out vec4 outColor;

layout (binding = 0) uniform sampler2D sTextureY;
layout (binding = 1) uniform sampler2D sTextureUV;
layout (binding = 0) uniform sampler2D plane0;
layout (binding = 1) uniform sampler2D plane1;

void main()
layout (std140, binding = 2) uniform Transformation
{
float r, g, b, y, u, v;

y = texture2D(sTextureY, vTextureCoord).r;
u = texture2D(sTextureUV, vTextureCoord).r - 0.5;
v = texture2D(sTextureUV, vTextureCoord).g - 0.5;

r = y + 1.13983*v;
g = y - 0.39465*u - 0.58060*v;
b = y + 2.03211*u;
mat3 yuvmat;
vec3 offset;
vec4 uv_data;
} u;

outColor = vec4(r, g, b, 1.0);
void main()
{
vec2 uv = (vTextureCoord - u.uv_data.xy) * u.uv_data.zw;
vec3 YCbCr = vec3(texture(plane0, uv).r, texture(plane1, uv).r, texture(plane2, uv).r) - u.offset;
outColor = vec4(clamp(u.yuvmat * YCbCr, 0.0, 1.0), 1.0);
}

0 comments on commit 220330c

Please sign in to comment.