From 19017bdec412424894262540aaa9137103f05044 Mon Sep 17 00:00:00 2001 From: David Hirvonen Date: Wed, 26 Sep 2018 14:42:12 -0400 Subject: [PATCH] add scope_guard for 'CFRelease(empty)' on iOS (#54) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add scope_guard `CFRelease(empty)` for allocated CFDictionaryRef * add `static_cast(…)` for downcast warnings * bump patch version v0.3.6 --- CMakeLists.txt | 2 +- ogles_gpgpu/common/proc/fifo.cpp | 6 +++--- ogles_gpgpu/common/proc/mesh.cpp | 2 +- .../common/proc/multipass/box_opt_pass.cpp | 8 ++++---- .../common/proc/multipass/gauss_opt_pass.cpp | 10 +++++----- ogles_gpgpu/common/tools.h | 20 +++++++++++++++---- ogles_gpgpu/platform/ios/memtransfer_ios.cpp | 6 +++++- 7 files changed, 35 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d248d4..cb7b327 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ HunterGate( LOCAL ) -project(ogles_gpgpu VERSION 0.3.5) +project(ogles_gpgpu VERSION 0.3.6) hunter_add_package(check_ci_tag) find_package(check_ci_tag CONFIG REQUIRED) diff --git a/ogles_gpgpu/common/proc/fifo.cpp b/ogles_gpgpu/common/proc/fifo.cpp index 188d36b..056d6fe 100644 --- a/ogles_gpgpu/common/proc/fifo.cpp +++ b/ogles_gpgpu/common/proc/fifo.cpp @@ -112,7 +112,7 @@ void FifoProc::process(int position, Logger logger) { } ProcInterface* FifoProc::operator[](int i) const { - int index = modulo(m_outputIndex + i, procPasses.size()); + int index = modulo(static_cast(m_outputIndex) + i, static_cast(procPasses.size())); return procPasses[index]; } @@ -192,13 +192,13 @@ void FifoProc::createFBOTex(bool genMipmap) { int FifoProc::render(int position) { // Render into input FBO if (m_count == int(size())) { - m_outputIndex = modulo(m_inputIndex + 1, size()); + m_outputIndex = modulo(static_cast(m_inputIndex) + 1, static_cast(size())); } getInputFilter()->render(); m_count = std::min(m_count + 1, int(size())); - m_inputIndex = modulo(m_inputIndex + 1, size()); + m_inputIndex = modulo(static_cast(m_inputIndex) + 1, static_cast(size())); return 0; } diff --git a/ogles_gpgpu/common/proc/mesh.cpp b/ogles_gpgpu/common/proc/mesh.cpp index 9a5ddc3..8791579 100644 --- a/ogles_gpgpu/common/proc/mesh.cpp +++ b/ogles_gpgpu/common/proc/mesh.cpp @@ -117,7 +117,7 @@ void MeshShaderProc::filterRenderSetCoords() { } void MeshShaderProc::filterRenderDraw() { - glDrawArrays(triangleKind, 0, vertices.size()); + glDrawArrays(triangleKind, 0, static_cast(vertices.size())); } void MeshShaderProc::setTriangleKind(GLenum kind) { diff --git a/ogles_gpgpu/common/proc/multipass/box_opt_pass.cpp b/ogles_gpgpu/common/proc/multipass/box_opt_pass.cpp index a6a09cc..5e8d533 100644 --- a/ogles_gpgpu/common/proc/multipass/box_opt_pass.cpp +++ b/ogles_gpgpu/common/proc/multipass/box_opt_pass.cpp @@ -32,8 +32,8 @@ static std::string vertexShaderForOptimizedBoxBlur(int blurRadius, float sigma) ss << " vec2 singleStepOffset = vec2(texelWidthOffset, texelHeightOffset);\n"; ss << " blurCoordinates[0] = inputTextureCoordinate.xy;\n"; for (int currentOptimizedOffset = 0; currentOptimizedOffset < numberOfOptimizedOffsets; currentOptimizedOffset++) { - int x1 = (unsigned long)((currentOptimizedOffset * 2) + 1); - int x2 = (unsigned long)((currentOptimizedOffset * 2) + 2); + int x1 = static_cast((currentOptimizedOffset * 2) + 1); + int x2 = static_cast((currentOptimizedOffset * 2) + 2); GLfloat optimizedOffset = (GLfloat)(currentOptimizedOffset * 2) + 1.5; @@ -70,8 +70,8 @@ static std::string fragmentShaderForOptimizedBoxBlur(int blurRadius, float sigma ss << " sum += texture2D(inputImageTexture, blurCoordinates[0]) * " << boxWeight << ";\n"; for (int currentBlurCoordinateIndex = 0; currentBlurCoordinateIndex < numberOfOptimizedOffsets; currentBlurCoordinateIndex++) { - int index1 = (unsigned long)((currentBlurCoordinateIndex * 2) + 1); - int index2 = (unsigned long)((currentBlurCoordinateIndex * 2) + 2); + int index1 = static_cast((currentBlurCoordinateIndex * 2) + 1); + int index2 = static_cast((currentBlurCoordinateIndex * 2) + 2); ss << " sum += texture2D(inputImageTexture, blurCoordinates[" << index1 << "]) * " << boxWeight2 << ";\n"; ss << " sum += texture2D(inputImageTexture, blurCoordinates[" << index2 << "]) * " << boxWeight2 << ";\n"; } diff --git a/ogles_gpgpu/common/proc/multipass/gauss_opt_pass.cpp b/ogles_gpgpu/common/proc/multipass/gauss_opt_pass.cpp index e2d1658..b498d54 100644 --- a/ogles_gpgpu/common/proc/multipass/gauss_opt_pass.cpp +++ b/ogles_gpgpu/common/proc/multipass/gauss_opt_pass.cpp @@ -77,8 +77,8 @@ std::string fragmentShaderForOptimizedBlur(int blurRadius, float sigma, bool doN GLfloat firstWeight = standardGaussianWeights[currentBlurCoordinateIndex * 2 + 1]; GLfloat secondWeight = standardGaussianWeights[currentBlurCoordinateIndex * 2 + 2]; GLfloat optimizedWeight = firstWeight + secondWeight; - int index1 = (unsigned long)((currentBlurCoordinateIndex * 2) + 1); - int index2 = (unsigned long)((currentBlurCoordinateIndex * 2) + 2); + int index1 = static_cast((currentBlurCoordinateIndex * 2) + 1); + int index2 = static_cast((currentBlurCoordinateIndex * 2) + 2); ss << " sum += texture2D(inputImageTexture, blurCoordinates[" << index1 << "]) * " << optimizedWeight << ";\n"; ss << " sum += texture2D(inputImageTexture, blurCoordinates[" << index2 << "]) * " << optimizedWeight << ";\n"; } @@ -125,15 +125,15 @@ std::string vertexShaderForOptimizedBlur(int blurRadius, float sigma) { ss << "attribute vec4 inputTextureCoordinate;\n"; ss << "uniform float texelWidthOffset;\n"; ss << "uniform float texelHeightOffset;\n\n"; - ss << "varying vec2 blurCoordinates[" << (unsigned long)(1 + (numberOfOptimizedOffsets * 2)) << "];\n\n"; + ss << "varying vec2 blurCoordinates[" << static_cast(1 + (numberOfOptimizedOffsets * 2)) << "];\n\n"; ss << "void main()\n"; ss << "{\n"; ss << " gl_Position = position;\n"; ss << " vec2 singleStepOffset = vec2(texelWidthOffset, texelHeightOffset);\n"; ss << " blurCoordinates[0] = inputTextureCoordinate.xy;\n"; for (int currentOptimizedOffset = 0; currentOptimizedOffset < numberOfOptimizedOffsets; currentOptimizedOffset++) { - int x1 = (unsigned long)((currentOptimizedOffset * 2) + 1); - int x2 = (unsigned long)((currentOptimizedOffset * 2) + 2); + int x1 = static_cast((currentOptimizedOffset * 2) + 1); + int x2 = static_cast((currentOptimizedOffset * 2) + 2); const auto& optOffset = optimizedGaussianOffsets[currentOptimizedOffset]; ss << " blurCoordinates[" << x1 << "] = inputTextureCoordinate.xy + singleStepOffset * " << optOffset << ";\n"; diff --git a/ogles_gpgpu/common/tools.h b/ogles_gpgpu/common/tools.h index 73fa8db..416ed24 100644 --- a/ogles_gpgpu/common/tools.h +++ b/ogles_gpgpu/common/tools.h @@ -18,8 +18,7 @@ #include #include #include - -using namespace std; +#include namespace ogles_gpgpu { @@ -47,13 +46,13 @@ class Tools { /** * Split a string by delimiter . */ - static vector split(const string& s, char delim = ' '); + static std::vector split(const std::string& s, char delim = ' '); /** * Replace all strings in by . * Code from http://stackoverflow.com/a/3418285. */ - static void strReplaceAll(string& str, const string& from, const string& to); + static void strReplaceAll(std::string& str, const std::string& from, const std::string& to); #ifdef OGLES_GPGPU_BENCHMARK static void resetTimeMeasurement(); @@ -74,4 +73,17 @@ class Tools { }; } +// https://stackoverflow.com/a/28413370 +struct scope_guard +{ + template + scope_guard(Callable&& f) + : m_f(std::forward(f)) + { + } + scope_guard(scope_guard&&) = default; + ~scope_guard() { m_f(); } + std::function m_f; +}; + #endif diff --git a/ogles_gpgpu/platform/ios/memtransfer_ios.cpp b/ogles_gpgpu/platform/ios/memtransfer_ios.cpp index 19274cd..eae2371 100644 --- a/ogles_gpgpu/platform/ios/memtransfer_ios.cpp +++ b/ogles_gpgpu/platform/ios/memtransfer_ios.cpp @@ -14,7 +14,7 @@ #include "../../common/core.h" /** - * Most code as from http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/ + * Most code is from http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/ */ using namespace std; @@ -97,10 +97,14 @@ void MemTransferIOS::init() { 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + + scope_guard destroy = [&]() { CFRelease(empty); }; + bufferAttr = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + CFDictionarySetValue(bufferAttr, kCVPixelBufferIOSurfacePropertiesKey, empty);