Skip to content

Commit

Permalink
Move FFmpeg de/encoder over to slangmosh iface.
Browse files Browse the repository at this point in the history
Make it easier to decouple shader compilation for utility code like this.
  • Loading branch information
Themaister committed Oct 15, 2023
1 parent 876e7c1 commit 4e0739f
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 22 deletions.
8 changes: 5 additions & 3 deletions application/platforms/application_headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@ struct WSIPlatformHeadless : Granite::GraniteWSIPlatform
for (unsigned i = 0; i < SwapchainImages; i++)
{
auto &device = app->get_wsi().get_device();
auto *rgb_to_yuv = device.get_shader_manager().register_compute(
FFmpegEncode::Shaders<> shaders;
shaders.rgb_to_yuv = device.get_shader_manager().register_compute(
"builtin://shaders/util/rgb_to_yuv.comp")->register_variant({})->get_program();
auto *chroma_downsample = device.get_shader_manager().register_compute(
shaders.chroma_downsample = device.get_shader_manager().register_compute(
"builtin://shaders/util/chroma_downsample.comp")->register_variant({})->get_program();
ycbcr_pipelines.push_back(encoder.create_ycbcr_pipeline(rgb_to_yuv, chroma_downsample));

ycbcr_pipelines.push_back(encoder.create_ycbcr_pipeline(shaders));
}

record_stream->start();
Expand Down
7 changes: 4 additions & 3 deletions tests/video_encode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ int main()
info.misc = Vulkan::IMAGE_MISC_MUTABLE_SRGB_BIT;
auto img = device.create_image(info);

auto *rgb_to_yuv = device.get_shader_manager().register_compute(
FFmpegEncode::Shaders<> shaders;
shaders.rgb_to_yuv = device.get_shader_manager().register_compute(
"builtin://shaders/util/rgb_to_yuv.comp")->register_variant({})->get_program();
auto *chroma_downsample = device.get_shader_manager().register_compute(
shaders.chroma_downsample = device.get_shader_manager().register_compute(
"builtin://shaders/util/chroma_downsample.comp")->register_variant({})->get_program();
auto pipe = encoder.create_ycbcr_pipeline(rgb_to_yuv, chroma_downsample);
auto pipe = encoder.create_ycbcr_pipeline(shaders);

for (unsigned i = 0; i < 1000; i++)
{
Expand Down
6 changes: 5 additions & 1 deletion tests/video_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ struct VideoTextureRenderable : AbstractRenderable

void begin(Vulkan::Device &device)
{
if (!decoder.begin_device_context(&device))
FFmpegDecode::Shaders<> shaders;
auto *comp = device.get_shader_manager().register_compute("builtin://shaders/util/yuv_to_rgb.comp");
shaders.yuv_to_rgb = comp->register_variant({})->get_program();

if (!decoder.begin_device_context(&device, shaders))
LOGE("Failed to begin device context.\n");
if (!decoder.play())
LOGE("Failed to begin playback.\n");
Expand Down
5 changes: 2 additions & 3 deletions video/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET

add_granite_internal_lib(granite-video
ffmpeg_encode.cpp ffmpeg_encode.hpp
slangmosh_encode_iface.hpp
ffmpeg_decode.cpp ffmpeg_decode.hpp slangmosh_decode_iface.hpp
ffmpeg_hw_device.cpp ffmpeg_hw_device.hpp)
if (GRANITE_VULKAN_SYSTEM_HANDLES)
target_sources(granite-video PRIVATE ffmpeg_decode.cpp ffmpeg_decode.hpp)
endif()

target_link_libraries(granite-video
PUBLIC granite-vulkan
Expand Down
13 changes: 7 additions & 6 deletions video/ffmpeg_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ AVFrameRingStream::~AVFrameRingStream()
struct VideoDecoder::Impl
{
Vulkan::Device *device = nullptr;
FFmpegDecode::Shaders<> shaders;
Audio::Mixer *mixer = nullptr;
DecodeOptions opts;
AVFormatContext *av_format_ctx = nullptr;
Expand Down Expand Up @@ -372,7 +373,7 @@ struct VideoDecoder::Impl
bool init_video_decoder_post_device();
bool init_audio_decoder();

bool begin_device_context(Vulkan::Device *device);
bool begin_device_context(Vulkan::Device *device, const FFmpegDecode::Shaders<> &shaders);
void end_device_context();
bool play();
bool get_stream_id(Audio::StreamID &id) const;
Expand Down Expand Up @@ -1029,8 +1030,7 @@ void VideoDecoder::Impl::setup_yuv_format_planes()

init_yuv_to_rgb();

auto *comp = device->get_shader_manager().register_compute("builtin://shaders/util/yuv_to_rgb.comp");
program = comp->register_variant({})->get_program();
program = shaders.yuv_to_rgb;
}

#ifdef HAVE_FFMPEG_VULKAN
Expand Down Expand Up @@ -1746,9 +1746,10 @@ void VideoDecoder::Impl::release_video_frame(unsigned index, Vulkan::Semaphore s
video_queue[index].idle_order = ++idle_timestamps;
}

bool VideoDecoder::Impl::begin_device_context(Vulkan::Device *device_)
bool VideoDecoder::Impl::begin_device_context(Vulkan::Device *device_, const FFmpegDecode::Shaders<> &shaders_)
{
device = device_;
shaders = shaders_;
thread_group = device->get_system_handles().thread_group;

// Potentially need device here if we're creating a Vulkan HW context.
Expand Down Expand Up @@ -2027,9 +2028,9 @@ unsigned VideoDecoder::get_height() const
return impl->get_height();
}

bool VideoDecoder::begin_device_context(Vulkan::Device *device)
bool VideoDecoder::begin_device_context(Vulkan::Device *device, const FFmpegDecode::Shaders<> &shaders)
{
return impl->begin_device_context(device);
return impl->begin_device_context(device, shaders);
}

void VideoDecoder::end_device_context()
Expand Down
3 changes: 2 additions & 1 deletion video/ffmpeg_decode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "device.hpp"
#include "image.hpp"
#include "semaphore.hpp"
#include "slangmosh_decode_iface.hpp"

namespace Granite
{
Expand Down Expand Up @@ -36,7 +37,7 @@ class VideoDecoder
unsigned get_height() const;

// Must be called before play().
bool begin_device_context(Vulkan::Device *device);
bool begin_device_context(Vulkan::Device *device, const FFmpegDecode::Shaders<> &shaders);

// Should be called after stop().
// If stop() is not called, this call with also do so.
Expand Down
7 changes: 3 additions & 4 deletions video/ffmpeg_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,14 +1389,13 @@ void VideoEncoder::submit_process_rgb(Vulkan::CommandBufferHandle &cmd, YCbCrPip
}
}

VideoEncoder::YCbCrPipeline VideoEncoder::create_ycbcr_pipeline(
Vulkan::Program *rgb_to_ycbcr, Vulkan::Program *chroma_downsample) const
VideoEncoder::YCbCrPipeline VideoEncoder::create_ycbcr_pipeline(const FFmpegEncode::Shaders<> &shaders) const
{
YCbCrPipeline pipeline_ptr{new YCbCrPipelineData};
auto &pipeline = *pipeline_ptr;

pipeline.rgb_to_ycbcr = rgb_to_ycbcr;
pipeline.chroma_downsample = chroma_downsample;
pipeline.rgb_to_ycbcr = shaders.rgb_to_yuv;
pipeline.chroma_downsample = shaders.chroma_downsample;

auto image_info = Vulkan::ImageCreateInfo::immutable_2d_image(impl->options.width, impl->options.height, VK_FORMAT_R8_UNORM);
image_info.initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
Expand Down
3 changes: 2 additions & 1 deletion video/ffmpeg_encode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "device.hpp"
#include "image.hpp"
#include "slangmosh_encode_iface.hpp"

namespace Granite
{
Expand Down Expand Up @@ -110,7 +111,7 @@ class VideoEncoder
};
using YCbCrPipeline = std::unique_ptr<YCbCrPipelineData, YCbCrPipelineDataDeleter>;

YCbCrPipeline create_ycbcr_pipeline(Vulkan::Program *rgb_to_ycbcr, Vulkan::Program *chroma_downsample) const;
YCbCrPipeline create_ycbcr_pipeline(const FFmpegEncode::Shaders<> &shaders) const;
void process_rgb(Vulkan::CommandBuffer &cmd, YCbCrPipeline &pipeline, const Vulkan::ImageView &view);
// Handles GPU synchronization if required.
void submit_process_rgb(Vulkan::CommandBufferHandle &cmd, YCbCrPipeline &pipeline);
Expand Down
10 changes: 10 additions & 0 deletions video/slangmosh_decode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"include": [ "../assets/shaders/inc" ],
"shaders": [
{
"name": "yuv_to_rgb",
"compute": true,
"path": "../assets/shaders/util/yuv_to_rgb.comp"
}
]
}
23 changes: 23 additions & 0 deletions video/slangmosh_decode_iface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Autogenerated from slangmosh, do not edit.
#ifndef SLANGMOSH_GENERATED_FFmpegDecodeiface_H
#define SLANGMOSH_GENERATED_FFmpegDecodeiface_H
#include <stdint.h>
namespace Vulkan
{
class Program;
class Shader;
}

namespace FFmpegDecode
{
template <typename Program = Vulkan::Program *, typename Shader = Vulkan::Shader *>
struct Shaders
{
Program yuv_to_rgb = {};
Shaders() = default;

template <typename Device, typename Layout, typename Resolver>
Shaders(Device &device, Layout &layout, const Resolver &resolver);
};
}
#endif
15 changes: 15 additions & 0 deletions video/slangmosh_encode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": [ "../assets/shaders/inc" ],
"shaders": [
{
"name": "rgb_to_yuv",
"compute": true,
"path": "../assets/shaders/util/rgb_to_yuv.comp"
},
{
"name": "chroma_downsample",
"compute": true,
"path": "../assets/shaders/util/chroma_downsample.comp"
}
]
}
24 changes: 24 additions & 0 deletions video/slangmosh_encode_iface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Autogenerated from slangmosh, do not edit.
#ifndef SLANGMOSH_GENERATED_FFmpegEncodeiface_H
#define SLANGMOSH_GENERATED_FFmpegEncodeiface_H
#include <stdint.h>
namespace Vulkan
{
class Program;
class Shader;
}

namespace FFmpegEncode
{
template <typename Program = Vulkan::Program *, typename Shader = Vulkan::Shader *>
struct Shaders
{
Program rgb_to_yuv = {};
Program chroma_downsample = {};
Shaders() = default;

template <typename Device, typename Layout, typename Resolver>
Shaders(Device &device, Layout &layout, const Resolver &resolver);
};
}
#endif

0 comments on commit 4e0739f

Please sign in to comment.