Skip to content

Commit

Permalink
[objc] Enable ARC, update Syphon & fix a lot of Syphon issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Dec 11, 2023
1 parent 98d4ee6 commit a494ef1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 54 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/Syphon-Framework
Submodule Syphon-Framework updated 79 files
+0 −5 Building Syphon.md
+135 −127 CMakeLists.txt
+0 −194 DoxygenLayout.xml
+11 −42 Exported_Symbols.exp
+1 −2 README.md
+25 −0 Syphon.docc/ExtendingSyphon.md
+96 −0 Syphon.docc/GettingStarted.md
+40 −0 Syphon.docc/Syphon.md
+12 −173 Syphon.h
+20 −0 Syphon.modulemap
+182 −326 Syphon.xcodeproj/project.pbxproj
+66 −0 Syphon.xcodeproj/xcshareddata/xcschemes/Syphon.xcscheme
+0 −12 SyphonBuildMacros.h
+1 −7 SyphonCFMessageReceiver.h
+6 −8 SyphonCFMessageReceiver.m
+2 −8 SyphonCFMessageSender.h
+6 −35 SyphonCFMessageSender.m
+3 −9 SyphonClient.h
+1 −7 SyphonClientBase.h
+26 −26 SyphonClientBase.m
+1 −7 SyphonClientConnectionManager.h
+37 −45 SyphonClientConnectionManager.m
+52 −41 SyphonDispatch.c
+2 −2 SyphonGLShader.h
+3 −5 SyphonGLShader.m
+2 −2 SyphonGLVertices.h
+3 −4 SyphonGLVertices.m
+0 −3 SyphonIOSurfaceImageCore.m
+0 −3 SyphonIOSurfaceImageLegacy.m
+2 −8 SyphonImage.h
+1 −3 SyphonImageBase.h
+0 −2 SyphonImageBase.m
+0 −41 SyphonMachMessageReceiver.h
+0 −78 SyphonMachMessageReceiver.m
+0 −40 SyphonMachMessageSender.h
+0 −87 SyphonMachMessageSender.m
+1 −7 SyphonMessageQueue.h
+22 −17 SyphonMessageQueue.m
+7 −9 SyphonMessageReceiver.h
+3 −16 SyphonMessageReceiver.m
+2 −8 SyphonMessageSender.h
+5 −18 SyphonMessageSender.m
+65 −0 SyphonMetalClient.h
+114 −0 SyphonMetalClient.m
+106 −0 SyphonMetalServer.h
+178 −0 SyphonMetalServer.m
+62 −0 SyphonMetalShaders.metal
+2 −8 SyphonOpenGLClient.h
+34 −33 SyphonOpenGLClient.m
+2 −8 SyphonOpenGLImage.h
+32 −54 SyphonOpenGLServer.h
+11 −16 SyphonOpenGLServer.m
+3 −1 SyphonPrivate.h
+1 −2 SyphonPrivate.m
+2 −8 SyphonServer.h
+1 −7 SyphonServerBase.h
+20 −29 SyphonServerBase.m
+1 −7 SyphonServerConnectionManager.h
+22 −29 SyphonServerConnectionManager.m
+10 −27 SyphonServerDirectory.h
+20 −58 SyphonServerDirectory.m
+3 −3 SyphonServerGLShader.h
+3 −3 SyphonServerGLShader.m
+3 −3 SyphonServerGLVertices.h
+3 −3 SyphonServerGLVertices.m
+51 −0 SyphonServerMetalTypes.h
+3 −3 SyphonServerRendererCoreGL.h
+10 −16 SyphonServerRendererCoreGL.m
+2 −2 SyphonServerRendererGL.h
+3 −4 SyphonServerRendererGL.m
+3 −5 SyphonServerRendererLegacyGL.h
+4 −5 SyphonServerRendererLegacyGL.m
+39 −0 SyphonServerRendererMetal.h
+122 −0 SyphonServerRendererMetal.m
+9 −4 SyphonSubclassing.h
+0 −2 Syphon_Prefix.pch
+0 −13 customdoxygen.css
+0 −2,439 doxygen.config
+0 −15 footer.html
6 changes: 6 additions & 0 deletions src/plugins/score-plugin-gfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ if(APPLE)
find_library(AudioToolbox_FK AudioToolbox)
find_library(VideoToolbox_FK VideoToolbox)

target_compile_options(${PROJECT_NAME}
PRIVATE
$<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc>
$<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc-exceptions>
$<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-weak>
)
target_link_libraries(${PROJECT_NAME} PRIVATE
${QuartzCore_FK}
${AppKit_FK}
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/score-plugin-gfx/Gfx/CameraDevice.avf.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void iterateCameraFormats(AVCaptureDevice* device, std::function<void(Cam
CMFormatDescriptionRef formatDescription{};
CMVideoDimensions dimensions{};

formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
formatDescription = (__bridge CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);

for (id range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
Expand All @@ -31,8 +31,6 @@ static void iterateCameraFormats(AVCaptureDevice* device, std::function<void(Cam

void enumerateCameraDevices(std::function<void(CameraSettings, QString)> func)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
Expand All @@ -46,8 +44,6 @@ void enumerateCameraDevices(std::function<void(CameraSettings, QString)> func)
iterateCameraFormats(device, func);
}
}

[pool release];
}
}

9 changes: 9 additions & 0 deletions src/plugins/score-plugin-gfx/Gfx/Graph/CommonUBOs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ struct OutputUBO
float renderSize[2]{};
};

/**
* @brief UBO shared across all video objects.
*/
struct VideoMaterialUBO
{
float scale[2]{1.f, 1.f};
float textureSize[2]{1.f, 1.f};
};

#pragma pack(pop)
}
2 changes: 2 additions & 0 deletions src/plugins/score-plugin-gfx/Gfx/Graph/RenderList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class SCORE_PLUGIN_GFX_EXPORT RenderList

int samples() const noexcept { return m_samples; }

bool canRender() const noexcept { return m_ready; }

private:
OutputUBO m_outputUBOData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ extern "C" {
#define SCORE_GFX_VIDEO_UNIFORMS \
"layout(std140, binding = 0) uniform renderer_t {\n" \
" mat4 clipSpaceCorrMatrix;\n" \
" \n" \
" vec2 renderSize;\n" \
"} renderer;\n" \
"\n" \
"layout(std140, binding = 2) uniform material_t {\n" \
" vec2 scale;\n" \
" vec2 textureSize;\n" \
" vec2 texSz;\n" \
"} mat;\n"

namespace score::gfx
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/score-plugin-gfx/Gfx/Graph/decoders/RGBA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ out gl_PerVertex { vec4 gl_Position; };
void main()
{
v_texcoord = texcoord * mat.textureSize;
v_texcoord = texcoord * mat.texSz.xy;
gl_Position = renderer.clipSpaceCorrMatrix * vec4(position.x * mat.scale.x, position.y * mat.scale.y, 0.0, 1.);
}
)_";
Expand Down
53 changes: 20 additions & 33 deletions src/plugins/score-plugin-gfx/Gfx/Syphon/SyphonInput.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Gfx/GfxApplicationPlugin.hpp>
#include <Gfx/Syphon/SyphonHelpers.hpp>
#include <Syphon/SyphonClient.h>
#include <Syphon/SyphonOpenGLClient.h>
#include <Syphon/SyphonOpenGLImage.h>
#include <Syphon/SyphonServerDirectory.h>
#include <QOpenGLContext>
Expand Down Expand Up @@ -56,10 +57,7 @@ explicit Renderer(const SyphonInputNode& n): node{n} { }
QRhiBuffer* m_processUBO{};
QRhiBuffer* m_materialUBO{};

struct Material {
float scale_w{1.0f}, scale_h{1.0f};
float width{1.f}, height{1.f};
} material;
score::gfx::VideoMaterialUBO material;
std::unique_ptr<score::gfx::PackedRectDecoder> m_gpu{};

bool enabled{};
Expand Down Expand Up @@ -87,15 +85,14 @@ void openServer(QRhi& rhi)
{
enabled = false;
// Need pool
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

SyphonServerDirectory *ssd = [SyphonServerDirectory sharedDirectory];
NSArray *servers = [ssd serversMatchingName:NULL appName:NULL];
if (servers.count != 0)
{
if (NSDictionary *desc = findServer(servers, node.settings.path))
{
m_receiver = [[SYPHON_CLIENT_UNIQUE_CLASS_NAME alloc]
m_receiver = [[SyphonOpenGLClient alloc]
initWithServerDescription:desc
context: nativeContext(rhi)
options:NULL
Expand All @@ -104,15 +101,14 @@ void openServer(QRhi& rhi)
}
enabled = true;
}
[pool drain];
}

score::gfx::TextureRenderTarget renderTargetForInput(const score::gfx::Port& p) override { return { }; }
void init(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res) override
{
// Initialize our rendering structures
auto& rhi = *renderer.state.rhi;
const auto& mesh = renderer.defaultQuad();
const auto& mesh = renderer.defaultTriangle();
if (!m_meshBuffer)
{
auto [mbuffer, ibuffer] = renderer.initMeshBuffer(mesh, res);
Expand All @@ -125,40 +121,37 @@ void init(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res) overri
m_processUBO->create();

m_materialUBO = rhi.newBuffer(
QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, sizeof(Material));
QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, sizeof(score::gfx::VideoMaterialUBO));
m_materialUBO->create();

// Initialize syphon
openServer(rhi);
int w = 16, h = 16;

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
SyphonOpenGLImage* img{};
if(enabled)
{
if(img = [m_receiver newFrameImage]) {
if((img = [m_receiver newFrameImage])) {
NSSize sz = img.textureSize;
w = sz.width;
h = sz.height;
currentTex = img.textureName;
}
}

metadata.width = std::max((int)1, w);
metadata.height = std::max((int)1, h);
material.width = metadata.width;
material.height = metadata.height;
material.textureSize[0] = w;
material.textureSize[1] = h;
res.updateDynamicBuffer(m_materialUBO, 0, sizeof(score::gfx::VideoMaterialUBO), &material);

m_gpu = std::make_unique<score::gfx::PackedRectDecoder>(QRhiTexture::RGBA8, 4, metadata, QString{});
createPipelines(renderer);
m_pixels.resize(w * h * 4);

if(img)
{
rebuildTexture(img);
[img release];
}

[pool drain];
}

void createPipelines(score::gfx::RenderList& r)
Expand All @@ -171,7 +164,7 @@ void createPipelines(score::gfx::RenderList& r)
m_p,
this->node.output[0]->edges,
r,
r.defaultQuad(),
r.defaultTriangle(),
shaders.first,
shaders.second,
m_processUBO,
Expand All @@ -180,19 +173,20 @@ void createPipelines(score::gfx::RenderList& r)
}
}

#include <Gfx/Qt5CompatPush> // clang-format: keep
void rebuildTexture(SyphonOpenGLImage* img)
{
SCORE_ASSERT(!m_gpu->samplers.empty());
auto tex = m_gpu->samplers[0].texture;

QRhiTexture::NativeTexture tt;
tt.layout = 0;
tt.object = reinterpret_cast<decltype(tt.object)>(&currentTex);
tt.object = currentTex;

tex->destroy();
NSSize sz = img.textureSize;
tex->setPixelSize(QSize(sz.width, sz.height));
tex->setFormat(QRhiTexture::Format::BGRA8);
tex->setFlags(tex->flags() | QRhiTexture::TextureRectangleGL);
tex->createFrom(tt);

// FIXME how to ensure this ?
Expand All @@ -208,7 +202,6 @@ void rebuildTexture(SyphonOpenGLImage* img)
for(auto& pass : m_p)
pass.second.srb->create();
}
#include <Gfx/Qt5CompatPop> // clang-format: keep

GLuint currentTex = 0;
void update(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res) override
Expand All @@ -229,7 +222,6 @@ void update(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res) over
// Check the current status of the Syphon remote
bool connected{};

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
auto img = [m_receiver newFrameImage];
if(!img) {
return;
Expand All @@ -240,18 +232,17 @@ void update(score::gfx::RenderList& renderer, QRhiResourceUpdateBatch& res) over
{
metadata.width = std::max(1., sz.width);
metadata.height = std::max(1., sz.height);
material.width = metadata.width;
material.height = metadata.height;
material.scale[0] = 1.f;
material.scale[1] = 1.f;
material.textureSize[0] = metadata.width;
material.textureSize[1] = metadata.height;

currentTex = img.textureName;
rebuildTexture(img);
}

[img release];
[pool drain];

res.updateDynamicBuffer(m_processUBO, 0, sizeof(score::gfx::ProcessUBO), &this->node.standardUBO);
res.updateDynamicBuffer(m_materialUBO, 0, sizeof(Material), &material);
res.updateDynamicBuffer(m_materialUBO, 0, sizeof(score::gfx::VideoMaterialUBO), &material);
}

void runRenderPass(
Expand All @@ -260,7 +251,7 @@ void runRenderPass(
score::gfx::Edge& edge) override
{
const auto& mesh = renderer.defaultTriangle();
score::gfx::quadRenderPass(renderer, {.mesh = m_meshBuffer, .index = m_idxBuffer}, cb, edge, m_p);
score::gfx::defaultRenderPass(renderer, mesh, {.mesh = m_meshBuffer, .index = m_idxBuffer}, cb, edge, m_p);
}

void release(score::gfx::RenderList& r) override
Expand Down Expand Up @@ -289,7 +280,6 @@ void release(score::gfx::RenderList& r) override
}

SyphonOpenGLClient* m_receiver{};
std::vector<unsigned char> m_pixels;
};
#include <Gfx/Qt5CompatPop> // clang-format: keep

Expand Down Expand Up @@ -381,15 +371,12 @@ void registerServer(NSDictionary* desc, std::function<void(const Device::DeviceS
void enumerate(
std::function<void(const Device::DeviceSettings&)> f) const override
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

auto ssd = [SyphonServerDirectory sharedDirectory];
NSArray *servers = [ssd serversMatchingName:NULL appName:NULL];
for(int i = 0; i < servers.count; i++)
{
registerServer(servers[i], f);
}
[pool drain];
}
};

Expand Down
14 changes: 2 additions & 12 deletions src/plugins/score-plugin-gfx/Gfx/Syphon/SyphonOutput.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,21 @@ void startRendering() override
void onRendererChange() override { }
bool canRender() const override
{
return bool(m_syphon);
return bool(m_syphon);
}


void createSyphon(QRhi& rhi)
{
if(!m_created)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
auto serverName = this->m_settings.path.toNSString();
m_syphon = [[SyphonOpenGLServer alloc]
initWithName:serverName
context:nativeContext(rhi)
options:NULL
];
m_created = true;

[pool drain];
}
}

Expand All @@ -73,7 +70,7 @@ void render() override
m_update();

auto renderer = m_renderer.lock();
if (renderer && m_renderState)
if (renderer && renderer->nodes.size() > 1 && m_renderState)
{
auto rhi = m_renderState->rhi;

Expand All @@ -94,7 +91,6 @@ void render() override
if (m_created)
{
auto t = static_cast<QGles2Texture*>(m_texture);
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

[m_syphon
publishFrameTexture:t->texture
Expand All @@ -103,7 +99,6 @@ void render() override
textureDimensions:NSMakeSize(m_settings.width, m_settings.height)
flipped:false
];
[pool drain];
}
}
}
Expand Down Expand Up @@ -164,14 +159,9 @@ void createOutput(

void destroyOutput() override
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

[m_syphon stop];
[m_syphon release];

m_syphon = nullptr;

[pool drain];
}

std::shared_ptr<score::gfx::RenderState> renderState() const override
Expand Down

0 comments on commit a494ef1

Please sign in to comment.