From da1e10308002779c56c97c2aebdbbfbc0074d41a Mon Sep 17 00:00:00 2001 From: pradeep Date: Sat, 21 May 2016 17:22:52 +0530 Subject: [PATCH 01/16] Fix for compiling with GCC >= 6.1.1 --- examples/CMakeLists.txt | 63 +++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 25be4cdf..820631a4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -41,35 +41,27 @@ ELSE(TARGET forge) ${FREEIMAGE_INCLUDE_PATH} ) ADD_DEFINITIONS(-DGLEW_MX) -IF(UNIX) - ADD_DEFINITIONS(-Wall -std=c++11 -fvisibility=hidden) - # GCC 5.3 and above give errors for mempcy from - # This is a (temporary) fix for that - IF("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "5.3.0") - ADD_DEFINITIONS(-D_FORCE_INLINES) - ENDIF() -ENDIF() - -# OS Definitions -IF(UNIX) - IF(APPLE) - ADD_DEFINITIONS(-DOS_MAC) - SET(CMAKE_MACOSX_RPATH ON) - SET(CMAKE_SKIP_BUILD_RPATH FALSE) - SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) - SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}") - SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) - - LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}" isSystemDir) - IF("${isSystemDir}" STREQUAL "-1") + # OS Definitions + IF(UNIX) + ADD_DEFINITIONS(-Wall -std=c++11 -fvisibility=hidden) + IF(APPLE) + ADD_DEFINITIONS(-DOS_MAC) + SET(CMAKE_MACOSX_RPATH ON) + SET(CMAKE_SKIP_BUILD_RPATH FALSE) + SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}") - ENDIF("${isSystemDir}" STREQUAL "-1") - ELSE(APPLE) - ADD_DEFINITIONS(-DOS_LNX) - ENDIF(APPLE) -ELSE(UNIX) - ADD_DEFINITIONS(-DOS_WIN -DNOMINMAX) -ENDIF(UNIX) + SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + + LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}" isSystemDir) + IF("${isSystemDir}" STREQUAL "-1") + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}") + ENDIF("${isSystemDir}" STREQUAL "-1") + ELSE(APPLE) + ADD_DEFINITIONS(-DOS_LNX) + ENDIF(APPLE) + ELSE(UNIX) + ADD_DEFINITIONS(-DOS_WIN -DNOMINMAX) + ENDIF(UNIX) ENDIF(TARGET forge) FILE(GLOB CPU_EXAMPLE_SRC_FILES "cpu/*.cpp") @@ -90,6 +82,21 @@ ENDFOREACH() IF (BUILD_EXAMPLES_CUDA) FIND_PACKAGE(CUDA QUIET) IF(CUDA_FOUND) + + IF(UNIX) + # GCC 5.3 and above give errors for mempcy from + # This is a (temporary) fix for that + IF("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "5.3.0") + ADD_DEFINITIONS(-D_FORCE_INLINES) + ENDIF() + # GCC 6.0 and above default to g++14, enabling c++11 features by default + # Enabling c++11 with nvcc 7.5 + gcc 6.x doesn't seem to work + # Only solution for now is to force use c++03 for gcc 6.x + IF("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "6.0.0") + SET(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler -std=c++98") + ENDIF() + ENDIF(UNIX) + INCLUDE_DIRECTORIES(${CUDA_INCLUDE_DIRS}) # GCC 6.0 and above default to g++14, enabling c++11 features by default From 5093411947eb8f12879567e1468bffc60016dd91 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 23 May 2016 13:51:06 +0530 Subject: [PATCH 02/16] Remove obsolete code from histogram fragment shader --- src/backend/opengl/shaders/histogram_fs.glsl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/opengl/shaders/histogram_fs.glsl b/src/backend/opengl/shaders/histogram_fs.glsl index 095d50a3..e39f8d92 100644 --- a/src/backend/opengl/shaders/histogram_fs.glsl +++ b/src/backend/opengl/shaders/histogram_fs.glsl @@ -9,6 +9,5 @@ out vec4 outColor; void main(void) { - float a = isPVAOn ? pervcol.w : 1.0; outColor = vec4(isPVCOn ? pervcol.xyz : barColor.xyz, isPVAOn ? pervcol.w : 1.0); } From 491afc6c76779baa7d52921ab19983e72cc0bf9b Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 23 May 2016 13:52:12 +0530 Subject: [PATCH 03/16] Remove unwanted headers inclusion from opengl backend --- src/backend/opengl/chart_impl.cpp | 4 ---- src/backend/opengl/font_impl.cpp | 2 -- src/backend/opengl/histogram_impl.cpp | 3 --- src/backend/opengl/image_impl.cpp | 3 --- src/backend/opengl/plot_impl.cpp | 2 -- 5 files changed, 14 deletions(-) diff --git a/src/backend/opengl/chart_impl.cpp b/src/backend/opengl/chart_impl.cpp index f4091d51..9ab98bd7 100644 --- a/src/backend/opengl/chart_impl.cpp +++ b/src/backend/opengl/chart_impl.cpp @@ -7,10 +7,6 @@ * http://arrayfire.com/licenses/BSD-3-Clause ********************************************************/ -#include -#include -#include - #include #include diff --git a/src/backend/opengl/font_impl.cpp b/src/backend/opengl/font_impl.cpp index a4d3370b..e81e7057 100644 --- a/src/backend/opengl/font_impl.cpp +++ b/src/backend/opengl/font_impl.cpp @@ -7,8 +7,6 @@ * http://arrayfire.com/licenses/BSD-3-Clause ********************************************************/ -#include - #include #include #include diff --git a/src/backend/opengl/histogram_impl.cpp b/src/backend/opengl/histogram_impl.cpp index 742510f8..56cd9a2a 100644 --- a/src/backend/opengl/histogram_impl.cpp +++ b/src/backend/opengl/histogram_impl.cpp @@ -7,9 +7,6 @@ * http://arrayfire.com/licenses/BSD-3-Clause ********************************************************/ -#include -#include - #include #include #include diff --git a/src/backend/opengl/image_impl.cpp b/src/backend/opengl/image_impl.cpp index f088d909..69a925d2 100644 --- a/src/backend/opengl/image_impl.cpp +++ b/src/backend/opengl/image_impl.cpp @@ -7,9 +7,6 @@ * http://arrayfire.com/licenses/BSD-3-Clause ********************************************************/ -#include -#include - #include #include #include diff --git a/src/backend/opengl/plot_impl.cpp b/src/backend/opengl/plot_impl.cpp index fafe8a58..88bf55de 100644 --- a/src/backend/opengl/plot_impl.cpp +++ b/src/backend/opengl/plot_impl.cpp @@ -7,8 +7,6 @@ * http://arrayfire.com/licenses/BSD-3-Clause ********************************************************/ -#include - #include #include From 0dc5422401daf97335c5e886fa596dd5e62af533 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 23 May 2016 13:57:42 +0530 Subject: [PATCH 04/16] Move shader headers inclusion to correct location --- src/backend/opengl/plot_impl.cpp | 5 +++++ src/backend/opengl/plot_impl.hpp | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/backend/opengl/plot_impl.cpp b/src/backend/opengl/plot_impl.cpp index 88bf55de..6ed1f08f 100644 --- a/src/backend/opengl/plot_impl.cpp +++ b/src/backend/opengl/plot_impl.cpp @@ -9,6 +9,11 @@ #include #include +#include +#include +#include +#include +#include #include diff --git a/src/backend/opengl/plot_impl.hpp b/src/backend/opengl/plot_impl.hpp index 074bfff6..4e9cee60 100644 --- a/src/backend/opengl/plot_impl.hpp +++ b/src/backend/opengl/plot_impl.hpp @@ -11,11 +11,6 @@ #include #include -#include -#include -#include -#include -#include #include #include From 37153092a78e6e8023c15ce898c05f199b420eaa Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 23 May 2016 14:32:33 +0530 Subject: [PATCH 05/16] 2D Vector Fields Place holder code for 3d fields has been added. An example for 2d field is added. --- examples/CMakeLists.txt | 7 - examples/cpu/field.cpp | 96 +++++++ include/fg/chart.h | 13 + include/fg/defines.h | 1 + include/fg/vector_field.h | 179 ++++++++++++ src/api/c/chart.cpp | 16 ++ src/api/c/vector_field.cpp | 138 ++++++++++ src/api/cpp/chart.cpp | 8 + src/api/cpp/vector_field.cpp | 98 +++++++ src/backend/chart_renderables.hpp | 27 ++ src/backend/handle.cpp | 10 + src/backend/handle.hpp | 4 + .../opengl/shaders/vector_field2d_gs.glsl | 94 +++++++ .../opengl/shaders/vector_field2d_vs.glsl | 18 ++ .../opengl/shaders/vector_field_gs.glsl | 46 ++++ .../opengl/shaders/vector_field_vs.glsl | 20 ++ src/backend/opengl/vector_field_impl.cpp | 259 ++++++++++++++++++ src/backend/opengl/vector_field_impl.hpp | 82 ++++++ 18 files changed, 1109 insertions(+), 7 deletions(-) create mode 100644 examples/cpu/field.cpp create mode 100644 include/fg/vector_field.h create mode 100644 src/api/c/vector_field.cpp create mode 100644 src/api/cpp/vector_field.cpp create mode 100644 src/backend/opengl/shaders/vector_field2d_gs.glsl create mode 100644 src/backend/opengl/shaders/vector_field2d_vs.glsl create mode 100644 src/backend/opengl/shaders/vector_field_gs.glsl create mode 100644 src/backend/opengl/shaders/vector_field_vs.glsl create mode 100644 src/backend/opengl/vector_field_impl.cpp create mode 100644 src/backend/opengl/vector_field_impl.hpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 820631a4..b438d6f0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -99,13 +99,6 @@ IF (BUILD_EXAMPLES_CUDA) INCLUDE_DIRECTORIES(${CUDA_INCLUDE_DIRS}) - # GCC 6.0 and above default to g++14, enabling c++11 features by default - # Enabling c++11 with nvcc 7.5 + gcc 6.x doesn't seem to work - # Only solution for now is to force use c++03 for gcc 6.x - IF("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "6.0.0") - SET(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler -std=c++98") - ENDIF() - FILE(GLOB CUDA_EXAMPLE_SRC_FILES "cuda/*.cu") FOREACH(FILE ${CUDA_EXAMPLE_SRC_FILES}) GET_FILENAME_COMPONENT(EXAMPLE ${FILE} NAME_WE) diff --git a/examples/cpu/field.cpp b/examples/cpu/field.cpp new file mode 100644 index 00000000..434621c9 --- /dev/null +++ b/examples/cpu/field.cpp @@ -0,0 +1,96 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#include +#include +#include +#include +#include +#include + +const unsigned DIMX = 640; +const unsigned DIMY = 480; +const float PI = 3.14159265359; +const float MINIMUM = 1.0f; +const float MAXIMUM = 20.f; +const float STEP = 2.0f; + +using namespace std; + +inline float randn() +{ + return ((float) rand() / (RAND_MAX)); +} + +void generateColors(std::vector& colors) +{ + int elems = (MAXIMUM-MINIMUM)/STEP; + int numElems = elems*elems; + colors.clear(); + for (int i=0; i &points, std::vector &dirs) +{ + int numElems = (MAXIMUM-MINIMUM)/STEP; + points.clear(); + + for (int j=0; j points; + std::vector colors; + std::vector dirs; + generatePoints(points, dirs); + generateColors(colors); + + fg::copy(field.vertices(), field.verticesSize(), (const void*)points.data()); + fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); + fg::copy(field.directions(), field.directionsSize(), (const void*)dirs.data()); + + do { + generateColors(colors); + fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); + wnd.draw(chart); + } while(!wnd.close()); + + return 0; +} diff --git a/include/fg/chart.h b/include/fg/chart.h index 88b1f2b9..bd120b55 100644 --- a/include/fg/chart.h +++ b/include/fg/chart.h @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -53,6 +54,9 @@ FGAPI fg_err fg_add_surface_to_chart(fg_surface* pSurface, fg_chart pHandle, const uint pXPoints, const uint pYPoints, const fg_dtype pType, const fg_plot_type pPlotType, const fg_marker_type pMarkerType); +FGAPI fg_err fg_add_vector_field_to_chart(fg_vector_field* pField, fg_chart pHandle, + const uint pNPoints, const fg_dtype pType); + FGAPI fg_err fg_render_chart(const fg_window pWindow, const fg_chart pChart, const int pX, const int pY, const int pWidth, const int pHeight, @@ -213,6 +217,15 @@ class Chart { FGAPI Surface surface(const uint pNumXPoints, const uint pNumYPoints, const dtype pDataType, const PlotType pPlotType=FG_PLOT_SURFACE, const MarkerType pMarkerType=FG_MARKER_NONE); + /** + Create and add an Vector Field object to the current chart + + \param[in] pNumPoints is number of data points to display + \param[in] pDataType takes one of the values of \ref dtype that indicates + the integral data type of vector field data + */ + FGAPI VectorField vectorField(const uint pNumPoints, const dtype pDataType); + /** Render the chart to given window diff --git a/include/fg/defines.h b/include/fg/defines.h index 4e3d2033..e1f2eb8d 100644 --- a/include/fg/defines.h +++ b/include/fg/defines.h @@ -55,6 +55,7 @@ typedef void* fg_image; typedef void* fg_histogram; typedef void* fg_plot; typedef void* fg_surface; +typedef void* fg_vector_field; typedef unsigned int uint; typedef unsigned short ushort; diff --git a/include/fg/vector_field.h b/include/fg/vector_field.h new file mode 100644 index 00000000..00e61c8a --- /dev/null +++ b/include/fg/vector_field.h @@ -0,0 +1,179 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#pragma once + +#include + + +#ifdef __cplusplus +extern "C" { +#endif + +FGAPI fg_err fg_create_vector_field(fg_vector_field *pField, + const uint pNPoints, + const fg_dtype pType, + const fg_chart_type pChartType); + +FGAPI fg_err fg_destroy_vector_field(fg_vector_field pField); + +FGAPI fg_err fg_set_vector_field_color(fg_vector_field pField, + const float pRed, const float pGreen, + const float pBlue, const float pAlpha); + +FGAPI fg_err fg_set_vector_field_legend(fg_vector_field pField, const char* pLegend); + +FGAPI fg_err fg_get_vector_field_vbo(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_cbo(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_abo(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_dbo(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_vbo_size(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_cbo_size(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_abo_size(uint* pOut, const fg_vector_field pField); + +FGAPI fg_err fg_get_vector_field_dbo_size(uint* pOut, const fg_vector_field pField); + +#ifdef __cplusplus +} +#endif + + +#ifdef __cplusplus + +namespace fg +{ + +/** + \class VectorField + + \brief VectorField is a line graph to display two dimensional data. + */ +class VectorField { + private: + fg_vector_field mValue; + + public: + /** + Creates a VectorField object + + \param[in] pNumPoints is number of data points to display + \param[in] pDataType takes one of the values of \ref dtype that indicates + the integral data type of VectorField data + \param[in] pChartType dictates the dimensionality of the chart + */ + FGAPI VectorField(const uint pNumPoints, const dtype pDataType, const ChartType pChartType); + + /** + Copy constructor for VectorField + + \param[in] pOther is the VectorField of which we make a copy of. + */ + FGAPI VectorField(const VectorField& pOther); + + /** + VectorField Destructor + */ + FGAPI ~VectorField(); + + /** + Set global color for the field lines + + \param[in] pColor takes values of fg::Color to define VectorField color + */ + FGAPI void setColor(const fg::Color pColor); + + /** + Set global color for the field lines + + \param[in] pRed is Red component in range [0, 1] + \param[in] pGreen is Green component in range [0, 1] + \param[in] pBlue is Blue component in range [0, 1] + \param[in] pAlpha is Blue component in range [0, 1] + */ + FGAPI void setColor(const float pRed, const float pGreen, + const float pBlue, const float pAlpha); + + /** + Set VectorField legend + + \param[in] pLegend + */ + FGAPI void setLegend(const char* pLegend); + + /** + Get the OpenGL buffer object identifier for vertices + + \return OpenGL VBO resource id. + */ + FGAPI uint vertices() const; + + /** + Get the OpenGL buffer object identifier for color values per vertex + + \return OpenGL CBO resource id. + */ + FGAPI uint colors() const; + + /** + Get the OpenGL buffer object identifier for alpha values per vertex + + \return OpenGL ABO resource id. + */ + FGAPI uint alphas() const; + + /** + Get the OpenGL buffer object identifier for field direction per vertex + + \return OpenGL DBO resource id. + */ + FGAPI uint directions() const; + + /** + Get the OpenGL Vertex Buffer Object resource size + + \return vertex buffer object size in bytes + */ + FGAPI uint verticesSize() const; + + /** + Get the OpenGL colors Buffer Object resource size + + \return colors buffer object size in bytes + */ + FGAPI uint colorsSize() const; + + /** + Get the OpenGL alphas Buffer Object resource size + + \return alphas buffer object size in bytes + */ + FGAPI uint alphasSize() const; + + /** + Get the OpenGL directions Buffer Object resource size + + \return direction buffer object size in bytes + */ + FGAPI uint directionsSize() const; + + /** + Get the handle to internal implementation of VectorField + */ + FGAPI fg_vector_field get() const; +}; + +} + +#endif diff --git a/src/api/c/chart.cpp b/src/api/c/chart.cpp index 850da09e..41ec21d9 100644 --- a/src/api/c/chart.cpp +++ b/src/api/c/chart.cpp @@ -163,6 +163,22 @@ fg_err fg_add_surface_to_chart(fg_surface* pSurface, fg_chart pHandle, return FG_ERR_NONE; } +fg_err fg_add_vector_field_to_chart(fg_vector_field* pField, fg_chart pHandle, + const uint pNPoints, const fg_dtype pType) +{ + try { + common::Chart* chrt = getChart(pHandle); + common::VectorField* field = new common::VectorField(pNPoints, + (fg::dtype)pType, + chrt->chartType()); + chrt->addRenderable(field->impl()); + *pField = getHandle(field); + } + CATCHALL + + return FG_ERR_NONE; +} + fg_err fg_render_chart(const fg_window pWindow, const fg_chart pChart, const int pX, const int pY, const int pWidth, const int pHeight, const float* pTransform) diff --git a/src/api/c/vector_field.cpp b/src/api/c/vector_field.cpp new file mode 100644 index 00000000..071cacb3 --- /dev/null +++ b/src/api/c/vector_field.cpp @@ -0,0 +1,138 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#include + +#include +#include + +fg_err fg_create_vector_field(fg_vector_field *pField, + const uint pNPoints, + const fg_dtype pType, + const fg_chart_type pChartType) +{ + try { + *pField = getHandle(new common::VectorField(pNPoints, (fg::dtype)pType, pChartType)); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_destroy_vector_field(fg_vector_field pField) +{ + try { + delete getVectorField(pField); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_set_vector_field_color(fg_vector_field pField, + const float pRed, const float pGreen, + const float pBlue, const float pAlpha) +{ + try { + getVectorField(pField)->setColor(pRed, pGreen, pBlue, pAlpha); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_set_vector_field_legend(fg_vector_field pField, const char* pLegend) +{ + try { + getVectorField(pField)->setLegend(pLegend); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_vbo(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = getVectorField(pField)->vbo(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_cbo(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = getVectorField(pField)->cbo(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_abo(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = getVectorField(pField)->abo(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_dbo(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = getVectorField(pField)->dbo(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_vbo_size(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = (uint)getVectorField(pField)->vboSize(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_cbo_size(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = (uint)getVectorField(pField)->cboSize(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_abo_size(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = (uint)getVectorField(pField)->aboSize(); + } + CATCHALL + + return FG_ERR_NONE; +} + +fg_err fg_get_vector_field_dbo_size(uint* pOut, const fg_vector_field pField) +{ + try { + *pOut = (uint)getVectorField(pField)->dboSize(); + } + CATCHALL + + return FG_ERR_NONE; +} diff --git a/src/api/cpp/chart.cpp b/src/api/cpp/chart.cpp index 6a04171a..ab44e5a1 100644 --- a/src/api/cpp/chart.cpp +++ b/src/api/cpp/chart.cpp @@ -135,6 +135,14 @@ Surface Chart::surface(const uint pNumXPoints, const uint pNumYPoints, const dty } } +VectorField Chart::vectorField(const uint pNumPoints, const dtype pDataType) +{ + common::Chart* chrt = getChart(mValue); + VectorField retVal(pNumPoints, pDataType, chrt->chartType()); + chrt->addRenderable(getVectorField(retVal.get())->impl()); + return retVal; +} + void Chart::render(const Window& pWindow, const int pX, const int pY, const int pVPW, const int pVPH, const float* pTransform) const diff --git a/src/api/cpp/vector_field.cpp b/src/api/cpp/vector_field.cpp new file mode 100644 index 00000000..42ae1463 --- /dev/null +++ b/src/api/cpp/vector_field.cpp @@ -0,0 +1,98 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#include + +#include +#include + +namespace fg +{ + +VectorField::VectorField(const uint pNumPoints, const dtype pDataType, const ChartType pChartType) +{ + mValue = getHandle(new common::VectorField(pNumPoints, pDataType, pChartType)); +} + +VectorField::VectorField(const VectorField& pOther) +{ + mValue = getHandle(new common::VectorField(pOther.get())); +} + +VectorField::~VectorField() +{ + delete getVectorField(mValue); +} + +void VectorField::setColor(const Color pColor) +{ + float r = (((int) pColor >> 24 ) & 0xFF ) / 255.f; + float g = (((int) pColor >> 16 ) & 0xFF ) / 255.f; + float b = (((int) pColor >> 8 ) & 0xFF ) / 255.f; + float a = (((int) pColor ) & 0xFF ) / 255.f; + getVectorField(mValue)->setColor(r, g, b, a); +} + +void VectorField::setColor(const float pRed, const float pGreen, + const float pBlue, const float pAlpha) +{ + getVectorField(mValue)->setColor(pRed, pGreen, pBlue, pAlpha); +} + +void VectorField::setLegend(const char* pLegend) +{ + getVectorField(mValue)->setLegend(pLegend); +} + +uint VectorField::vertices() const +{ + return getVectorField(mValue)->vbo(); +} + +uint VectorField::colors() const +{ + return getVectorField(mValue)->cbo(); +} + +uint VectorField::alphas() const +{ + return getVectorField(mValue)->abo(); +} + +uint VectorField::directions() const +{ + return getVectorField(mValue)->dbo(); +} + +uint VectorField::verticesSize() const +{ + return (uint)getVectorField(mValue)->vboSize(); +} + +uint VectorField::colorsSize() const +{ + return (uint)getVectorField(mValue)->cboSize(); +} + +uint VectorField::alphasSize() const +{ + return (uint)getVectorField(mValue)->aboSize(); +} + +uint VectorField::directionsSize() const +{ + return (uint)getVectorField(mValue)->dboSize(); +} + +fg_vector_field VectorField::get() const +{ + return mValue; +} + +} diff --git a/src/backend/chart_renderables.hpp b/src/backend/chart_renderables.hpp index 1bf39fb4..6e635642 100644 --- a/src/backend/chart_renderables.hpp +++ b/src/backend/chart_renderables.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -88,4 +89,30 @@ class Surface : public ChartRenderableBase { } }; +class VectorField : public ChartRenderableBase { + public: + VectorField(const uint pNumPoints, + const fg::dtype pDataType, + const fg::ChartType pChartType) { + if (pChartType == FG_CHART_2D) { + mShrdPtr = std::make_shared< detail::vector_field2d_impl >(pNumPoints, pDataType); + } else { + mShrdPtr = std::make_shared< detail::vector_field_impl >(pNumPoints, pDataType); + } + } + + VectorField(const fg_vector_field pOther) + : ChartRenderableBase( + reinterpret_cast(pOther)->impl()) { + } + + inline GLuint dbo() const { + return mShrdPtr->directions(); + } + + inline size_t dboSize() const { + return mShrdPtr->directionsSize(); + } +}; + } diff --git a/src/backend/handle.cpp b/src/backend/handle.cpp index 9ef70d87..a2a2697b 100644 --- a/src/backend/handle.cpp +++ b/src/backend/handle.cpp @@ -44,6 +44,11 @@ fg_surface getHandle(common::Surface* pValue) return reinterpret_cast(pValue); } +fg_vector_field getHandle(common::VectorField* pValue) +{ + return reinterpret_cast(pValue); +} + common::Window* getWindow(const fg_window& pValue) { return reinterpret_cast(pValue); @@ -78,3 +83,8 @@ common::Surface* getSurface(const fg_surface& pValue) { return reinterpret_cast(pValue); } + +common::VectorField* getVectorField(const fg_vector_field& pValue) +{ + return reinterpret_cast(pValue); +} diff --git a/src/backend/handle.hpp b/src/backend/handle.hpp index 7dd1a35f..459bddfd 100644 --- a/src/backend/handle.hpp +++ b/src/backend/handle.hpp @@ -31,6 +31,8 @@ fg_plot getHandle(common::Plot* pValue); fg_surface getHandle(common::Surface* pValue); +fg_vector_field getHandle(common::VectorField* pValue); + common::Window* getWindow(const fg_window& pValue); common::Font* getFont(const fg_font& pValue); @@ -44,3 +46,5 @@ common::Histogram* getHistogram(const fg_histogram& pValue); common::Plot* getPlot(const fg_plot& pValue); common::Surface* getSurface(const fg_surface& pValue); + +common::VectorField* getVectorField(const fg_vector_field& pValue); diff --git a/src/backend/opengl/shaders/vector_field2d_gs.glsl b/src/backend/opengl/shaders/vector_field2d_gs.glsl new file mode 100644 index 00000000..786511f0 --- /dev/null +++ b/src/backend/opengl/shaders/vector_field2d_gs.glsl @@ -0,0 +1,94 @@ +#version 330 +layout(points) in; +layout(triangle_strip, max_vertices = 9) out; + +uniform mat4 transform; + +in VS_OUT { + vec4 color; + vec2 dir; +} gs_in[]; + +out vec4 pervcol; + +mat4 rotationMatrix(vec3 axis, float angle) +{ + axis = normalize(axis); + float s = sin(angle); + float c = cos(angle); + float oc = 1.0 - c; + + return mat4(oc * axis.x * axis.x + c, + oc * axis.x * axis.y - axis.z * s, + oc * axis.z * axis.x + axis.y * s, + 0.0, + oc * axis.x * axis.y + axis.z * s, + oc * axis.y * axis.y + c, + oc * axis.y * axis.z - axis.x * s, + 0.0, + oc * axis.z * axis.x - axis.y * s, + oc * axis.y * axis.z + axis.x * s, + oc * axis.z * axis.z + c, + 0.0, + 0.0, 0.0, 0.0, 1.0); +} + +void main() +{ + vec4 pos = transform * gl_in[0].gl_Position; + vec2 dir = gs_in[0].dir; + + mat4 rmat = rotationMatrix(vec3(0,0,1), atan(dir.y, dir.x)); + mat4 trans = transform * rmat; + + vec4 origin = pos + trans * vec4(0,0,0,1); // (2/5) + vec4 leftTip = pos + trans * vec4(-0.333, -0.333, 0.0, 1.0); // (1) + vec4 leftTop = pos + trans * vec4(-0.167, -0.333, 0.0, 1.0); // (3/7) + vec4 leftBot = pos + trans * vec4(-0.167, -1.000, 0.0, 1.0); // (9) + vec4 rightBot = pos + trans * vec4( 0.167, -1.000, 0.0, 1.0); // (8) + vec4 rightTop = pos + trans * vec4( 0.167, -0.333, 0.0, 1.0); // (6) + vec4 rightTip = pos + trans * vec4( 0.333, -0.333, 0.0, 1.0); // (4) + + // (2/5) + // //\\ + // / / \ \ + // / / \ \ + // / / \ \ + // (1)__(3/7)____(6)__(4) + // |\ | + // | \ | + // | \ | + // | \ | + // | \ | + // | \ | + // (9)_____\(8) + + pervcol = gs_in[0].color; + + gl_Position = leftTip; + EmitVertex(); + gl_Position = origin; + EmitVertex(); + gl_Position = leftTop; + EmitVertex(); + + gl_Position = rightTip; + EmitVertex(); + + gl_Position = origin; + EmitVertex(); + + gl_Position = rightTop; + EmitVertex(); + + gl_Position = leftTop; + EmitVertex(); + + gl_Position = rightBot; + EmitVertex(); + + gl_Position = leftBot; + EmitVertex(); + + EndPrimitive(); +} diff --git a/src/backend/opengl/shaders/vector_field2d_vs.glsl b/src/backend/opengl/shaders/vector_field2d_vs.glsl new file mode 100644 index 00000000..c4c9ef91 --- /dev/null +++ b/src/backend/opengl/shaders/vector_field2d_vs.glsl @@ -0,0 +1,18 @@ +#version 330 + +in vec2 point; +in vec3 color; +in float alpha; +in vec2 direction; + +out VS_OUT { + vec4 color; + vec2 dir; +} vs_out; + +void main(void) +{ + vs_out.color = vec4(color, alpha); + vs_out.dir = direction; + gl_Position = vec4(point.xy, 0.0, 1); +} diff --git a/src/backend/opengl/shaders/vector_field_gs.glsl b/src/backend/opengl/shaders/vector_field_gs.glsl new file mode 100644 index 00000000..6f8e8f4d --- /dev/null +++ b/src/backend/opengl/shaders/vector_field_gs.glsl @@ -0,0 +1,46 @@ +#version 330 +layout(points) in; +layout(triangle_strip, max_vertices = 13) out; + +in VS_OUT { + vec4 color; + vec3 dir; +} gs_in[]; + +out vec4 pervcol; + +mat4 rotationMatrix(vec3 axis, float angle) +{ + axis = normalize(axis); + float s = sin(angle); + float c = cos(angle); + float oc = 1.0 - c; + + return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, + oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, + oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, + 0.0, 0.0, 0.0, 1.0); +} + +void build_house(vec4 position) +{ + pervcol = gs_in[0].color; // gs_in[0] since there's only one input vertex + gl_Position = position + vec4(-0.2f, -0.2f, 0.0f, 0.0f); // 1:bottom-left + EmitVertex(); + gl_Position = position + vec4( 0.2f, -0.2f, 0.0f, 0.0f); // 2:bottom-right + EmitVertex(); + gl_Position = position + vec4(-0.2f, 0.2f, 0.0f, 0.0f); // 3:top-left + EmitVertex(); + gl_Position = position + vec4( 0.2f, 0.2f, 0.0f, 0.0f); // 4:top-right + EmitVertex(); + gl_Position = position + vec4( 0.0f, 0.4f, 0.0f, 0.0f); // 5:top + pervcol = vec4(1.0, 1.0, 1.0, 1.0); + EmitVertex(); + EndPrimitive(); +} + + +void main() +{ + build_house(gl_in[0].gl_Position); +} diff --git a/src/backend/opengl/shaders/vector_field_vs.glsl b/src/backend/opengl/shaders/vector_field_vs.glsl new file mode 100644 index 00000000..61284695 --- /dev/null +++ b/src/backend/opengl/shaders/vector_field_vs.glsl @@ -0,0 +1,20 @@ +#version 330 + +uniform mat4 transform; + +in vec3 point; +in vec3 color; +in float alpha; +in vec3 direction; + +out VS_OUT { + vec4 color; + vec3 dir; +} vs_out; + +void main(void) +{ + vs_out.color = vec4(color, alpha); + vs_out.dir = direction; + gl_Position = transform * vec4(point.xyz, 1); +} diff --git a/src/backend/opengl/vector_field_impl.cpp b/src/backend/opengl/vector_field_impl.cpp new file mode 100644 index 00000000..e99435c3 --- /dev/null +++ b/src/backend/opengl/vector_field_impl.cpp @@ -0,0 +1,259 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace std; + +// identity matrix +static const glm::mat4 I(1.0f); + +namespace opengl +{ + +void vector_field_impl::bindResources(const int pWindowId) +{ + if (mVAOMap.find(pWindowId) == mVAOMap.end()) { + GLuint vao = 0; + /* create a vertex array object + * with appropriate bindings */ + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + // attach vertices + glEnableVertexAttribArray(mFieldPointIndex); + glBindBuffer(GL_ARRAY_BUFFER, mVBO); + glVertexAttribPointer(mFieldPointIndex, mDimension, mGLType, GL_FALSE, 0, 0); + // attach colors + glEnableVertexAttribArray(mFieldColorIndex); + glBindBuffer(GL_ARRAY_BUFFER, mCBO); + glVertexAttribPointer(mFieldColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0); + // attach alphas + glEnableVertexAttribArray(mFieldAlphaIndex); + glBindBuffer(GL_ARRAY_BUFFER, mABO); + glVertexAttribPointer(mFieldAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0); + // attach field directions + glEnableVertexAttribArray(mFieldDirectionIndex); + glBindBuffer(GL_ARRAY_BUFFER, mDBO); + glVertexAttribPointer(mFieldDirectionIndex, mDimension, GL_FLOAT, GL_FALSE, 0, 0); + glBindVertexArray(0); + /* store the vertex array object corresponding to + * the window instance in the map */ + mVAOMap[pWindowId] = vao; + } + + glBindVertexArray(mVAOMap[pWindowId]); +} + +void vector_field_impl::unbindResources() const +{ + glBindVertexArray(0); +} + +void vector_field_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, + const int pX, const int pY, + const int pVPW, const int pVPH) +{ + float range_x = mRange[1] - mRange[0]; + float range_y = mRange[3] - mRange[2]; + float range_z = mRange[5] - mRange[4]; + // set scale to zero if input is constant array + // otherwise compute scale factor by standard equation + float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); + float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); + float graph_scale_z = std::abs(range_z) < 1.0e-3 ? 0.0f : 2/(range_z); + + float coor_offset_x = (-mRange[0] * graph_scale_x); + float coor_offset_y = (-mRange[2] * graph_scale_y); + float coor_offset_z = (-mRange[4] * graph_scale_z); + + glm::mat4 rMat = glm::rotate(I, -glm::radians(90.f), glm::vec3(1,0,0)); + glm::mat4 tMat = glm::translate(I, + glm::vec3(-1 + coor_offset_x , -1 + coor_offset_y, -1 + coor_offset_z)); + glm::mat4 sMat = glm::scale(I, + glm::vec3(1.0f * graph_scale_x, -1.0f * graph_scale_y, 1.0f * graph_scale_z)); + + glm::mat4 model= rMat * tMat * sMat; + + pOut = pInput * model; + glScissor(pX, pY, pVPW, pVPH); +} + +vector_field_impl::vector_field_impl(const uint pNumPoints, const fg::dtype pDataType, const int pD) + : mDimension(pD), mNumPoints(pNumPoints), mDataType(pDataType), mGLType(dtype2gl(mDataType)), + mFieldProgram(-1), mDBO(-1), mDBOSize(0), mFieldMatIndex(-1), mFieldPointIndex(-1), + mFieldColorIndex(-1), mFieldAlphaIndex(-1), mFieldDirectionIndex(-1), + mFieldPVCOnIndex(-1), mFieldPVAOnIndex(-1), mFieldUColorIndex(-1) +{ + CheckGL("Begin vector_field_impl::vector_field_impl"); + mIsPVCOn = false; + mIsPVAOn = false; + + setColor(0, 1, 0, 1); + mLegend = std::string(""); + + // FIXME + if (mDimension==2) { + mFieldProgram = initShaders(glsl::vector_field2d_vs.c_str(), glsl::histogram_fs.c_str(), + glsl::vector_field2d_gs.c_str()); + mVBOSize = 2*mNumPoints; + mDBOSize = 2*mNumPoints; + } else { + mFieldProgram = initShaders(glsl::vector_field_vs.c_str(), glsl::histogram_fs.c_str(), + glsl::vector_field_gs.c_str()); + mVBOSize = 3*mNumPoints; + mDBOSize = 3*mNumPoints; + } + + mCBOSize = 3*mNumPoints; + mABOSize = mNumPoints; + + mFieldMatIndex = glGetUniformLocation(mFieldProgram, "transform"); + mFieldPointIndex = glGetAttribLocation (mFieldProgram, "point"); + mFieldColorIndex = glGetAttribLocation (mFieldProgram, "color"); + mFieldAlphaIndex = glGetAttribLocation (mFieldProgram, "alpha"); + mFieldDirectionIndex = glGetAttribLocation (mFieldProgram, "direction"); + + mFieldPVCOnIndex = glGetUniformLocation(mFieldProgram, "isPVCOn"); + mFieldPVAOnIndex = glGetUniformLocation(mFieldProgram, "isPVAOn"); + mFieldUColorIndex = glGetUniformLocation(mFieldProgram, "barColor"); + +#define PLOT_CREATE_BUFFERS(type) \ + mVBO = createBuffer(GL_ARRAY_BUFFER, mVBOSize, NULL, GL_DYNAMIC_DRAW); \ + mCBO = createBuffer(GL_ARRAY_BUFFER, mCBOSize, NULL, GL_DYNAMIC_DRAW); \ + mABO = createBuffer(GL_ARRAY_BUFFER, mABOSize, NULL, GL_DYNAMIC_DRAW); \ + mDBO = createBuffer(GL_ARRAY_BUFFER, mDBOSize, NULL, GL_DYNAMIC_DRAW); \ + mVBOSize *= sizeof(type); \ + mCBOSize *= sizeof(float); \ + mABOSize *= sizeof(float); \ + mDBOSize *= sizeof(float); + + switch(mGLType) { + case GL_FLOAT : PLOT_CREATE_BUFFERS(float) ; break; + case GL_INT : PLOT_CREATE_BUFFERS(int) ; break; + case GL_UNSIGNED_INT : PLOT_CREATE_BUFFERS(uint) ; break; + case GL_SHORT : PLOT_CREATE_BUFFERS(short) ; break; + case GL_UNSIGNED_SHORT : PLOT_CREATE_BUFFERS(ushort); break; + case GL_UNSIGNED_BYTE : PLOT_CREATE_BUFFERS(float) ; break; + default: fg::TypeError("vector_field_impl::vector_field_impl", __LINE__, 1, mDataType); + } +#undef PLOT_CREATE_BUFFERS + CheckGL("End vector_field_impl::vector_field_impl"); +} + +vector_field_impl::~vector_field_impl() +{ + CheckGL("Begin vector_field_impl::~vector_field_impl"); + for (auto it = mVAOMap.begin(); it!=mVAOMap.end(); ++it) { + GLuint vao = it->second; + glDeleteVertexArrays(1, &vao); + } + glDeleteBuffers(1, &mVBO); + glDeleteBuffers(1, &mCBO); + glDeleteBuffers(1, &mABO); + glDeleteBuffers(1, &mDBO); + glDeleteProgram(mFieldProgram); + CheckGL("End vector_field_impl::~vector_field_impl"); +} + +GLuint vector_field_impl::directions() +{ + return mDBO; +} + +size_t vector_field_impl::directionsSize() const +{ + return mDBOSize; +} + +void vector_field_impl::render(const int pWindowId, + const int pX, const int pY, const int pVPW, const int pVPH, + const glm::mat4& pTransform) +{ + CheckGL("Begin vector_field_impl::render"); + glEnable(GL_SCISSOR_TEST); + glDepthMask(GL_FALSE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glm::mat4 mvp(1.0); + this->computeTransformMat(mvp, pTransform, pX, pY, pVPW, pVPH); + + glUseProgram(mFieldProgram); + + glUniformMatrix4fv(mFieldMatIndex, 1, GL_FALSE, glm::value_ptr(mvp)); + glUniform1i(mFieldPVCOnIndex, mIsPVCOn); + glUniform1i(mFieldPVAOnIndex, mIsPVAOn); + glUniform4fv(mFieldUColorIndex, 1, mColor); + + //FIXME remove debug polygon mode setting + vector_field_impl::bindResources(pWindowId); + glDrawArrays(GL_POINTS, 0, mNumPoints); + vector_field_impl::unbindResources(); + + glUseProgram(0); + + glDisable(GL_BLEND); + glDepthMask(GL_TRUE); + glDisable(GL_SCISSOR_TEST); + CheckGL("End vector_field_impl::render"); +} + +void vector_field2d_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, + const int pX, const int pY, + const int pVPW, const int pVPH) +{ + float range_x = mRange[1] - mRange[0]; + float range_y = mRange[3] - mRange[2]; + // set scale to zero if input is constant array + // otherwise compute scale factor by standard equation + float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); + float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); + + float coor_offset_x = (-mRange[0] * graph_scale_x); + float coor_offset_y = (-mRange[2] * graph_scale_y); + + //FIXME: Using hard constants for now, find a way to get chart values + const float lMargin = 68; + const float rMargin = 8; + const float tMargin = 8; + const float bMargin = 44; + const float tickSize = 10; + + float viewWidth = pVPW - (lMargin + rMargin + tickSize/2); + float viewHeight = pVPH - (bMargin + tMargin + tickSize/2); + float view_scale_x = viewWidth/pVPW; + float view_scale_y = viewHeight/pVPH; + + coor_offset_x *= view_scale_x; + coor_offset_y *= view_scale_y; + + float view_offset_x = (2.0f * (lMargin + tickSize/2 )/ pVPW ) ; + float view_offset_y = (2.0f * (bMargin + tickSize )/ pVPH ) ; + + glm::mat4 tMat = glm::translate(I, + glm::vec3(-1 + view_offset_x + coor_offset_x , -1 + view_offset_y + coor_offset_y, 0)); + pOut = glm::scale(tMat, + glm::vec3(graph_scale_x * view_scale_x , graph_scale_y * view_scale_y ,1)); + + pOut = pInput * pOut; + + glScissor(pX + lMargin + tickSize/2, pY+bMargin + tickSize/2, + pVPW - lMargin - rMargin - tickSize/2, + pVPH - bMargin - tMargin - tickSize/2); +} + +} diff --git a/src/backend/opengl/vector_field_impl.hpp b/src/backend/opengl/vector_field_impl.hpp new file mode 100644 index 00000000..5300443a --- /dev/null +++ b/src/backend/opengl/vector_field_impl.hpp @@ -0,0 +1,82 @@ +/******************************************************* +* Copyright (c) 2015-2019, ArrayFire +* All rights reserved. +* +* This file is distributed under 3-clause BSD license. +* The complete license agreement can be obtained at: +* http://arrayfire.com/licenses/BSD-3-Clause +********************************************************/ + +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include + +namespace opengl +{ + +class vector_field_impl : public AbstractRenderable { + protected: + GLuint mDimension; + /* plot points characteristics */ + GLuint mNumPoints; + fg::dtype mDataType; + GLenum mGLType; + /* OpenGL Objects */ + GLuint mFieldProgram; + GLuint mDBO; + size_t mDBOSize; + /* shader variable index locations */ + /* vertex shader */ + GLuint mFieldMatIndex; + GLuint mFieldPointIndex; + GLuint mFieldColorIndex; + GLuint mFieldAlphaIndex; + GLuint mFieldDirectionIndex; + /* fragment shader */ + GLuint mFieldPVCOnIndex; + GLuint mFieldPVAOnIndex; + GLuint mFieldUColorIndex; + + std::map mVAOMap; + + /* bind and unbind helper functions + * for rendering resources */ + void bindResources(const int pWindowId); + void unbindResources() const; + + virtual void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, + const int pX, const int pY, + const int pVPW, const int pVPH); + + public: + vector_field_impl(const uint pNumPoints, const fg::dtype pDataType, + const int pDimension=3); + ~vector_field_impl(); + + GLuint directions(); + size_t directionsSize() const; + + virtual void render(const int pWindowId, + const int pX, const int pY, const int pVPW, const int pVPH, + const glm::mat4& pTransform); +}; + +class vector_field2d_impl : public vector_field_impl { + protected: + void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, + const int pX, const int pY, + const int pVPW, const int pVPH) override; + public: + vector_field2d_impl(const uint pNumPoints, const fg::dtype pDataType) + : vector_field_impl(pNumPoints, pDataType, 2) {} +}; + +} From 6a53c61f054a0ff308ff24ef775afd8ac9875b5e Mon Sep 17 00:00:00 2001 From: pradeep Date: Fri, 27 May 2016 22:03:24 +0530 Subject: [PATCH 06/16] Geometry shader for 3d vector field --- examples/cpu/field.cpp | 2 +- .../opengl/shaders/vector_field2d_gs.glsl | 4 +- .../opengl/shaders/vector_field2d_vs.glsl | 4 +- .../opengl/shaders/vector_field_gs.glsl | 110 +++++++++++++++--- 4 files changed, 97 insertions(+), 23 deletions(-) diff --git a/examples/cpu/field.cpp b/examples/cpu/field.cpp index 434621c9..eb2e6dbb 100644 --- a/examples/cpu/field.cpp +++ b/examples/cpu/field.cpp @@ -19,7 +19,7 @@ const unsigned DIMY = 480; const float PI = 3.14159265359; const float MINIMUM = 1.0f; const float MAXIMUM = 20.f; -const float STEP = 2.0f; +const float STEP = 1.0f; using namespace std; diff --git a/src/backend/opengl/shaders/vector_field2d_gs.glsl b/src/backend/opengl/shaders/vector_field2d_gs.glsl index 786511f0..aaeb2528 100644 --- a/src/backend/opengl/shaders/vector_field2d_gs.glsl +++ b/src/backend/opengl/shaders/vector_field2d_gs.glsl @@ -35,8 +35,8 @@ mat4 rotationMatrix(vec3 axis, float angle) void main() { - vec4 pos = transform * gl_in[0].gl_Position; - vec2 dir = gs_in[0].dir; + vec4 pos = gl_in[0].gl_Position; + vec2 dir = normalize(gs_in[0].dir); mat4 rmat = rotationMatrix(vec3(0,0,1), atan(dir.y, dir.x)); mat4 trans = transform * rmat; diff --git a/src/backend/opengl/shaders/vector_field2d_vs.glsl b/src/backend/opengl/shaders/vector_field2d_vs.glsl index c4c9ef91..abc0c116 100644 --- a/src/backend/opengl/shaders/vector_field2d_vs.glsl +++ b/src/backend/opengl/shaders/vector_field2d_vs.glsl @@ -5,6 +5,8 @@ in vec3 color; in float alpha; in vec2 direction; +uniform mat4 transform; + out VS_OUT { vec4 color; vec2 dir; @@ -14,5 +16,5 @@ void main(void) { vs_out.color = vec4(color, alpha); vs_out.dir = direction; - gl_Position = vec4(point.xy, 0.0, 1); + gl_Position = transform * vec4(point.xy, 0.0, 1); } diff --git a/src/backend/opengl/shaders/vector_field_gs.glsl b/src/backend/opengl/shaders/vector_field_gs.glsl index 6f8e8f4d..bd34f654 100644 --- a/src/backend/opengl/shaders/vector_field_gs.glsl +++ b/src/backend/opengl/shaders/vector_field_gs.glsl @@ -1,6 +1,6 @@ #version 330 layout(points) in; -layout(triangle_strip, max_vertices = 13) out; +layout(triangle_strip, max_vertices = 25) out; in VS_OUT { vec4 color; @@ -16,31 +16,103 @@ mat4 rotationMatrix(vec3 axis, float angle) float c = cos(angle); float oc = 1.0 - c; - return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, - oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, - oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, - 0.0, 0.0, 0.0, 1.0); + return mat4(oc * axis.x * axis.x + c, + oc * axis.x * axis.y - axis.z * s, + oc * axis.z * axis.x + axis.y * s, + 0.0, + oc * axis.x * axis.y + axis.z * s, + oc * axis.y * axis.y + c, + oc * axis.y * axis.z - axis.x * s, + 0.0, + oc * axis.z * axis.x - axis.y * s, + oc * axis.y * axis.z + axis.x * s, + oc * axis.z * axis.z + c, + 0.0, + 0.0, 0.0, 0.0, 1.0); } -void build_house(vec4 position) +void main() { - pervcol = gs_in[0].color; // gs_in[0] since there's only one input vertex - gl_Position = position + vec4(-0.2f, -0.2f, 0.0f, 0.0f); // 1:bottom-left + vec4 pos = gl_in[0].gl_Position; + vec3 dir = normalize(gs_in[0].dir); + float theta = acos(dir.z, 1.0); + float phi = atan(dir.y, dir.x); + mat4 zrot = rotationMatrix(vec3(0,0,1), phi); + vec4 sndAxs = zrot * vec4(0,1,0,1); + mat4 arot = rotationMatrix(sndAxs.xyz, theta); + mat4 trans = transform * arot * zrot; + + vec4 origin = pos + trans * vec4(0,0,0,1); + vec4 ltop = pos + trans * vec4(-0.333, 0.333, 0.333, 1.0); + vec4 lbot = pos + trans * vec4(-0.333,-0.333, 0.333, 1.0); + vec4 rtop = pos + trans * vec4(-0.333,-0.333,-0.333, 1.0); + vec4 rbot = pos + trans * vec4(-0.333, 0.333,-0.333, 1.0); + + vec4 iltop = pos + trans * vec4(-0.333, 0.167, 0.167, 1.0); + vec4 ilbot = pos + trans * vec4(-0.333,-0.167, 0.167, 1.0); + vec4 irtop = pos + trans * vec4(-0.333,-0.167,-0.167, 1.0); + vec4 irbot = pos + trans * vec4(-0.333, 0.167,-0.167, 1.0); + + vec4 iltop2 = pos + trans * vec4(-1.000, 0.167, 0.167, 1.0); + vec4 ilbot2 = pos + trans * vec4(-1.000,-0.167, 0.167, 1.0); + vec4 irtop2 = pos + trans * vec4(-1.000,-0.167,-0.167, 1.0); + vec4 irbot2 = pos + trans * vec4(-1.000, 0.167,-0.167, 1.0); + + pervcol = gs_in[0].color; + + // Roof top + gl_Position = ltop; + EmitVertex(); + gl_Position = origin; + EmitVertex(); + gl_Position = lbot; + EmitVertex(); + gl_Position = rbot; + EmitVertex(); + gl_Position = origin; + EmitVertex(); + gl_Position = rtop; + EmitVertex(); + gl_Position = ltop; + EmitVertex(); + // over the edge areas of roof + gl_Position = iltop; + EmitVertex(); + gl_Position = lbot; + EmitVertex(); + gl_Position = ilbot; + EmitVertex(); + gl_Position = rbot; EmitVertex(); - gl_Position = position + vec4( 0.2f, -0.2f, 0.0f, 0.0f); // 2:bottom-right + gl_Position = irbot; EmitVertex(); - gl_Position = position + vec4(-0.2f, 0.2f, 0.0f, 0.0f); // 3:top-left + gl_Position = rtop; EmitVertex(); - gl_Position = position + vec4( 0.2f, 0.2f, 0.0f, 0.0f); // 4:top-right + gl_Position = irtop; EmitVertex(); - gl_Position = position + vec4( 0.0f, 0.4f, 0.0f, 0.0f); // 5:top - pervcol = vec4(1.0, 1.0, 1.0, 1.0); + gl_Position = iltop; + EmitVertex(); + // Pillar + gl_Position = iltop2; + EmitVertex(); + gl_Position = ilbot; + EmitVertex(); + gl_Position = ilbot2; + EmitVertex(); + gl_Position = irbot; + EmitVertex(); + gl_Position = irbot2; + EmitVertex(); + gl_Position = irtop; + EmitVertex(); + gl_Position = irtop2; + EmitVertex(); + gl_Position = iltop2; + EmitVertex(); + // Pillar base + gl_Position = irbot2; + EmitVertex(); + gl_Position = ilbot2; EmitVertex(); EndPrimitive(); } - - -void main() -{ - build_house(gl_in[0].gl_Position); -} From d6b8e152064089385550bd8b780963d2965e2275 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 30 May 2016 15:32:41 +0530 Subject: [PATCH 07/16] 3d field geometry shader compilation fix --- src/backend/opengl/shaders/vector_field_gs.glsl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/backend/opengl/shaders/vector_field_gs.glsl b/src/backend/opengl/shaders/vector_field_gs.glsl index bd34f654..9d86d2ce 100644 --- a/src/backend/opengl/shaders/vector_field_gs.glsl +++ b/src/backend/opengl/shaders/vector_field_gs.glsl @@ -2,6 +2,8 @@ layout(points) in; layout(triangle_strip, max_vertices = 25) out; +uniform mat4 transform; + in VS_OUT { vec4 color; vec3 dir; @@ -35,8 +37,8 @@ void main() { vec4 pos = gl_in[0].gl_Position; vec3 dir = normalize(gs_in[0].dir); - float theta = acos(dir.z, 1.0); - float phi = atan(dir.y, dir.x); + float theta = acos(dir.z/ 1.0); + float phi = atan(dir.y/ dir.x); mat4 zrot = rotationMatrix(vec3(0,0,1), phi); vec4 sndAxs = zrot * vec4(0,1,0,1); mat4 arot = rotationMatrix(sndAxs.xyz, theta); From 7d6551779ab1fe8d27efd8e206ca835800ec53de Mon Sep 17 00:00:00 2001 From: pradeep Date: Tue, 21 Jun 2016 19:07:55 +0530 Subject: [PATCH 08/16] Fix Vector field transformation issue Also includes other fixes and changes given below * Add chart ticks at end of axes as well * Remove obsolete transformation related code from all renderables * Updated 2d vector field example * Fixed plot axes limits for bubblechart and plotting examples --- examples/cpu/bubblechart.cpp | 2 +- examples/cpu/field.cpp | 37 +---- examples/cpu/plotting.cpp | 2 +- include/fg/chart.h | 10 +- include/fg/image.h | 10 +- src/api/c/chart.cpp | 5 +- src/api/c/image.cpp | 5 +- src/api/cpp/chart.cpp | 5 +- src/api/cpp/image.cpp | 5 +- src/backend/chart.hpp | 4 +- src/backend/chart_renderables.hpp | 6 +- src/backend/image.hpp | 4 +- src/backend/opengl/chart_impl.cpp | 76 ++++++---- src/backend/opengl/chart_impl.hpp | 13 +- src/backend/opengl/common.hpp | 3 +- src/backend/opengl/glfw/window.cpp | 6 +- src/backend/opengl/glfw/window.hpp | 2 +- src/backend/opengl/histogram_impl.cpp | 7 +- src/backend/opengl/histogram_impl.hpp | 3 +- src/backend/opengl/image_impl.cpp | 4 +- src/backend/opengl/image_impl.hpp | 2 +- src/backend/opengl/plot_impl.cpp | 130 +++++++---------- src/backend/opengl/plot_impl.hpp | 12 +- .../opengl/shaders/vector_field2d_gs.glsl | 52 +++---- .../opengl/shaders/vector_field2d_vs.glsl | 6 +- .../opengl/shaders/vector_field_gs.glsl | 33 +++-- .../opengl/shaders/vector_field_vs.glsl | 4 +- src/backend/opengl/surface_impl.cpp | 109 +++++++------- src/backend/opengl/surface_impl.hpp | 4 +- src/backend/opengl/vector_field_impl.cpp | 135 +++++++----------- src/backend/opengl/vector_field_impl.hpp | 15 +- src/backend/opengl/window_impl.cpp | 14 +- 32 files changed, 317 insertions(+), 408 deletions(-) diff --git a/examples/cpu/bubblechart.cpp b/examples/cpu/bubblechart.cpp index 56d27011..29306413 100644 --- a/examples/cpu/bubblechart.cpp +++ b/examples/cpu/bubblechart.cpp @@ -74,7 +74,7 @@ int main(void) wnd.makeCurrent(); fg::Chart chart(FG_CHART_2D); - chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.1f, 1.1f); + chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.0f, 1.0f); /* Create several plot objects which creates the necessary * vertex buffer objects to hold the different plot types diff --git a/examples/cpu/field.cpp b/examples/cpu/field.cpp index eb2e6dbb..2e1d35ce 100644 --- a/examples/cpu/field.cpp +++ b/examples/cpu/field.cpp @@ -19,35 +19,18 @@ const unsigned DIMY = 480; const float PI = 3.14159265359; const float MINIMUM = 1.0f; const float MAXIMUM = 20.f; -const float STEP = 1.0f; +const float STEP = 2.0f; +const float NELEMS = (MAXIMUM-MINIMUM+1)/STEP; using namespace std; -inline float randn() -{ - return ((float) rand() / (RAND_MAX)); -} - -void generateColors(std::vector& colors) -{ - int elems = (MAXIMUM-MINIMUM)/STEP; - int numElems = elems*elems; - colors.clear(); - for (int i=0; i &points, std::vector &dirs) { - int numElems = (MAXIMUM-MINIMUM)/STEP; points.clear(); - for (int j=0; j points; std::vector colors; std::vector dirs; generatePoints(points, dirs); - generateColors(colors); fg::copy(field.vertices(), field.verticesSize(), (const void*)points.data()); - fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); fg::copy(field.directions(), field.directionsSize(), (const void*)dirs.data()); do { - generateColors(colors); - fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); wnd.draw(chart); } while(!wnd.close()); diff --git a/examples/cpu/plotting.cpp b/examples/cpu/plotting.cpp index e331f048..4e3b370a 100644 --- a/examples/cpu/plotting.cpp +++ b/examples/cpu/plotting.cpp @@ -52,7 +52,7 @@ int main(void) wnd.makeCurrent(); fg::Chart chart(FG_CHART_2D); - chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.1f, 1.1f); + chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.0f, 1.0f); /* Create several plot objects which creates the necessary * vertex buffer objects to hold the different plot types diff --git a/include/fg/chart.h b/include/fg/chart.h index bd120b55..c8d87087 100644 --- a/include/fg/chart.h +++ b/include/fg/chart.h @@ -59,8 +59,7 @@ FGAPI fg_err fg_add_vector_field_to_chart(fg_vector_field* pField, fg_chart pHan FGAPI fg_err fg_render_chart(const fg_window pWindow, const fg_chart pChart, - const int pX, const int pY, const int pWidth, const int pHeight, - const float* pTransform); + const int pX, const int pY, const int pWidth, const int pHeight); #ifdef __cplusplus } @@ -234,14 +233,9 @@ class Chart { \param[in] pY is y coordinate of origin of viewport in window coordinates \param[in] pVPW is the width of the viewport \param[in] pVPH is the height of the viewport - \param[in] pTransform is an array of floats. This array is expected to contain - at least 16 elements - - Note: pTransform array is assumed to be of expected length. */ FGAPI void render(const Window& pWindow, - const int pX, const int pY, const int pVPW, const int pVPH, - const float* pTransform) const; + const int pX, const int pY, const int pVPW, const int pVPH) const; /** Get the handle to internal implementation of Chart diff --git a/include/fg/image.h b/include/fg/image.h index a5a58f3a..8046a236 100644 --- a/include/fg/image.h +++ b/include/fg/image.h @@ -40,8 +40,7 @@ FGAPI fg_err fg_get_image_pbo_size(uint* pOut, const fg_image pImage); FGAPI fg_err fg_render_image(const fg_window pWindow, const fg_image pImage, - const int pX, const int pY, const int pWidth, const int pHeight, - const float* pTransform); + const int pX, const int pY, const int pWidth, const int pHeight); #ifdef __cplusplus } @@ -150,14 +149,9 @@ class Image { \param[in] pY is y coordinate of origin of viewport in window coordinates \param[in] pVPW is the width of the viewport \param[in] pVPH is the height of the viewport - \param[in] pTransform is an array of floats. This array is expected to contain - at least 16 elements - - Note: pTransform array is assumed to be of expected length. */ FGAPI void render(const Window& pWindow, - const int pX, const int pY, const int pVPW, const int pVPH, - const float* pTransform) const; + const int pX, const int pY, const int pVPW, const int pVPH) const; /** Get the handle to internal implementation of Image diff --git a/src/api/c/chart.cpp b/src/api/c/chart.cpp index 41ec21d9..a0a247a0 100644 --- a/src/api/c/chart.cpp +++ b/src/api/c/chart.cpp @@ -180,13 +180,12 @@ fg_err fg_add_vector_field_to_chart(fg_vector_field* pField, fg_chart pHandle, } fg_err fg_render_chart(const fg_window pWindow, const fg_chart pChart, - const int pX, const int pY, const int pWidth, const int pHeight, - const float* pTransform) + const int pX, const int pY, const int pWidth, const int pHeight) { try { getChart(pChart)->render(getWindow(pWindow)->getID(), pX, pY, pWidth, pHeight, - glm::make_mat4(pTransform)); + IDENTITY); } CATCHALL diff --git a/src/api/c/image.cpp b/src/api/c/image.cpp index fc36d1d3..c4add0fb 100644 --- a/src/api/c/image.cpp +++ b/src/api/c/image.cpp @@ -122,13 +122,12 @@ fg_err fg_get_image_pbo_size(uint* pOut, const fg_image pImage) fg_err fg_render_image(const fg_window pWindow, const fg_image pImage, - const int pX, const int pY, const int pWidth, const int pHeight, - const float* pTransform) + const int pX, const int pY, const int pWidth, const int pHeight) { try { getImage(pImage)->render(getWindow(pWindow)->getID(), pX, pY, pWidth, pHeight, - glm::make_mat4(pTransform)); + IDENTITY); } CATCHALL diff --git a/src/api/cpp/chart.cpp b/src/api/cpp/chart.cpp index ab44e5a1..20545154 100644 --- a/src/api/cpp/chart.cpp +++ b/src/api/cpp/chart.cpp @@ -144,12 +144,11 @@ VectorField Chart::vectorField(const uint pNumPoints, const dtype pDataType) } void Chart::render(const Window& pWindow, - const int pX, const int pY, const int pVPW, const int pVPH, - const float* pTransform) const + const int pX, const int pY, const int pVPW, const int pVPH) const { getChart(mValue)->render(getWindow(pWindow.get())->getID(), pX, pY, pVPW, pVPH, - glm::make_mat4(pTransform)); + IDENTITY); } fg_chart Chart::get() const diff --git a/src/api/cpp/image.cpp b/src/api/cpp/image.cpp index 14026b4f..46c9f28f 100644 --- a/src/api/cpp/image.cpp +++ b/src/api/cpp/image.cpp @@ -77,12 +77,11 @@ uint Image::size() const } void Image::render(const Window& pWindow, - const int pX, const int pY, const int pVPW, const int pVPH, - const float* pTransform) const + const int pX, const int pY, const int pVPW, const int pVPH) const { getImage(mValue)->render(getWindow(pWindow.get())->getID(), pX, pY, pVPW, pVPH, - glm::make_mat4(pTransform)); + IDENTITY); } diff --git a/src/backend/chart.hpp b/src/backend/chart.hpp index 9f7b2c91..00c4a384 100644 --- a/src/backend/chart.hpp +++ b/src/backend/chart.hpp @@ -74,8 +74,8 @@ class Chart { inline void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4 &pTransform) const { - mChart->render(pWindowId, pX, pY, pVPW, pVPH, pTransform); + const glm::mat4 &pView) const { + mChart->render(pWindowId, pX, pY, pVPW, pVPH, pView); } }; diff --git a/src/backend/chart_renderables.hpp b/src/backend/chart_renderables.hpp index 6e635642..b0131f6f 100644 --- a/src/backend/chart_renderables.hpp +++ b/src/backend/chart_renderables.hpp @@ -97,7 +97,11 @@ class VectorField : public ChartRenderableBase { if (pChartType == FG_CHART_2D) { mShrdPtr = std::make_shared< detail::vector_field2d_impl >(pNumPoints, pDataType); } else { - mShrdPtr = std::make_shared< detail::vector_field_impl >(pNumPoints, pDataType); + throw fg::Error(__PRETTY_FUNCTION__, __LINE__, + "3d fields not yet supported", + FG_ERR_NOT_SUPPORTED); + // FIXME Uncomment it once 3d fields are working properly + //mShrdPtr = std::make_shared< detail::vector_field_impl >(pNumPoints, pDataType); } } diff --git a/src/backend/image.hpp b/src/backend/image.hpp index 8cee15c8..8d399b71 100644 --- a/src/backend/image.hpp +++ b/src/backend/image.hpp @@ -52,8 +52,8 @@ class Image { inline void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) const { - mImage->render(pWindowId, pX, pY, pVPW, pVPH, pTransform); + const glm::mat4 &pView) const { + mImage->render(pWindowId, pX, pY, pVPW, pVPH, pView); } }; diff --git a/src/backend/opengl/chart_impl.cpp b/src/backend/opengl/chart_impl.cpp index 9ab98bd7..a4007aa2 100644 --- a/src/backend/opengl/chart_impl.cpp +++ b/src/backend/opengl/chart_impl.cpp @@ -100,11 +100,12 @@ void AbstractChart::renderTickLabels( if(pCoordsOffset < mTickCount) { // offset for y axis labels pos[0] -= (CHART2D_FONT_SIZE*it->length()/2.0f+mTickSize); - }else if(pCoordsOffset >= mTickCount && pCoordsOffset < 2*mTickCount) { + pos[1] -= (CHART2D_FONT_SIZE*.36); + } else if(pCoordsOffset >= mTickCount && pCoordsOffset < 2*mTickCount) { // offset for x axis labels pos[0] -= (CHART2D_FONT_SIZE*it->length()/4.0f); pos[1] -= (CHART2D_FONT_SIZE*1.32); - }else { + } else { // offsets for 3d chart axes ticks pos[0] -= (CHART2D_FONT_SIZE*it->length()/2.0f); pos[1] -= (CHART2D_FONT_SIZE); @@ -253,8 +254,8 @@ void chart2d_impl::generateChartData() std::vector decorData; std::copy(border, border+nValues, std::back_inserter(decorData)); - float step = 2.0f/(mTickCount); - int ticksLeft = mTickCount/2; + float step = getTickStepSize(-1, 1); + int ticksLeft = getNumTicksC2E(); /* push tick points for y axis: * push (0) first followed by @@ -342,11 +343,13 @@ void chart2d_impl::generateTickLabels() mYText.clear(); mZText.clear(); - float xstep = (mXMax-mXMin)/(mTickCount); - float ystep = (mYMax-mYMin)/(mTickCount); - float xmid = mXMin + (mXMax-mXMin)/2.0f; - float ymid = mYMin + (mYMax-mYMin)/2.0f; - int ticksLeft = mTickCount/2; + float xstep = getTickStepSize(mXMin, mXMax); + float ystep = getTickStepSize(mYMin, mYMax); + float xmid = (mXMax+mXMin)/2.0f; + float ymid = (mYMax+mYMin)/2.0f; + + int ticksLeft = getNumTicksC2E(); + /* push tick points for y axis */ mYText.push_back(toString(ymid)); size_t maxYLabelWidth = 0; @@ -354,6 +357,7 @@ void chart2d_impl::generateTickLabels() std::string temp = toString(ymid + i*-ystep); mYText.push_back(temp); maxYLabelWidth = std::max(maxYLabelWidth, temp.length()); + temp = toString(ymid + i*ystep); mYText.push_back(temp); maxYLabelWidth = std::max(maxYLabelWidth, temp.length()); @@ -376,16 +380,18 @@ chart2d_impl::chart2d_impl() void chart2d_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) + const glm::mat4& pView) { CheckGL("Begin chart2d_impl::renderChart"); - float w = float(pVPW - (mLeftMargin + mRightMargin + mTickSize)); - float h = float(pVPH - (mTopMargin + mBottomMargin + mTickSize)); - float offset_x = (2.0f * (mLeftMargin+mTickSize) + (w - pVPW)) / pVPW; - float offset_y = (2.0f * (mBottomMargin+mTickSize) + (h - pVPH)) / pVPH; - float scale_x = w / pVPW; - float scale_y = h / pVPH; + float lgap = mLeftMargin + mTickSize/2; + float bgap = mBottomMargin + mTickSize/2; + float w = pVPW - (lgap + mRightMargin + mTickSize/2); + float h = pVPH - (bgap + mTopMargin + mTickSize/2); + float offset_x = (lgap-mRightMargin) / pVPW; + float offset_y = (bgap-mTopMargin) / pVPH; + float scale_x = w / pVPW; + float scale_y = h / pVPH; /* set uniform attributes of shader * for drawing the plot borders */ @@ -402,11 +408,14 @@ void chart2d_impl::render(const int pWindowId, glUseProgram(0); chart2d_impl::unbindResources(); + glEnable(GL_SCISSOR_TEST); + glScissor(pX+mLeftMargin, pY+mBottomMargin, w, h); /* render all renderables */ for (auto renderable : mRenderables) { renderable->setRanges(mXMin, mXMax, mYMin, mYMax, mZMin, mZMax); - renderable->render(pWindowId, pX, pY, pVPW, pVPH, trans*pTransform); + renderable->render(pWindowId, pX, pY, pVPW, pVPH, pView * trans); } + glDisable(GL_SCISSOR_TEST); chart2d_impl::bindResources(pWindowId); @@ -525,7 +534,8 @@ void chart3d_impl::generateChartData() std::vector decorData; std::copy(border, border+nValues, std::back_inserter(decorData)); - float step = 2.0f/(mTickCount); + float step = getTickStepSize(-1, 1); + int ticksLeft = getNumTicksC2E(); /* push tick points for z axis: * push (0,0) first followed by @@ -535,7 +545,6 @@ void chart3d_impl::generateChartData() pushTicktextCoords(-1.0f, -1.0f, 0.0f); mZText.push_back(toString(0)); - int ticksLeft = mTickCount/2; for(int i=1; i<=ticksLeft; ++i) { /* (0, -1] to [-1, -1] */ float neg = i*-step; @@ -664,13 +673,15 @@ void chart3d_impl::generateTickLabels() mYText.clear(); mZText.clear(); - float xstep = (mXMax-mXMin)/(mTickCount); - float ystep = (mYMax-mYMin)/(mTickCount); - float zstep = (mZMax-mZMin)/(mTickCount); - float xmid = mXMin + (mXMax-mXMin)/2.0f; - float ymid = mYMin + (mYMax-mYMin)/2.0f; - float zmid = mZMin + (mZMax-mZMin)/2.0f; - int ticksLeft = mTickCount/2; + float xstep = getTickStepSize(mXMin, mXMax); + float ystep = getTickStepSize(mYMin, mYMax); + float zstep = getTickStepSize(mZMin, mZMax); + float xmid = (mXMax+mXMin)/2.0f; + float ymid = (mYMax+mYMin)/2.0f; + float zmid = (mZMax+mZMin)/2.0f; + + int ticksLeft = getNumTicksC2E(); + /* push tick points for z axis */ mZText.push_back(toString(zmid)); for (int i = 1; i <= ticksLeft; i++) { @@ -698,17 +709,16 @@ chart3d_impl::chart3d_impl() void chart3d_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) + const glm::mat4& pView) { /* set uniform attributes of shader * for drawing the plot borders */ static const glm::mat4 VIEW = glm::lookAt(glm::vec3(-1.f,0.5f, 1.f), glm::vec3( 1.f,-1.f,-1.f), glm::vec3( 0.f, 1.f, 0.f)); - static const glm::mat4 PROJECTION = glm::ortho(-2.f, 2.f, -2.f, 2.f, -1.f, 100.f); + static const glm::mat4 PROJECTION = glm::ortho(-1.75f, 1.75f, -1.75f, 1.75f, -0.001f, 100.f); static const glm::mat4 MODEL = glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(0,1,0)) * - glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(1,0,0)) * - glm::scale(glm::mat4(1.f), glm::vec3(1.0f, 1.0f, 1.0f)); + glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(1,0,0)); static const glm::mat4 PV = PROJECTION * VIEW; static const glm::mat4 PVM = PV * MODEL; @@ -723,11 +733,15 @@ void chart3d_impl::render(const int pWindowId, glUseProgram(0); chart3d_impl::unbindResources(); + glEnable(GL_SCISSOR_TEST); + glScissor(pX, pY, pVPW, pVPH); + glm::mat4 renderableMat = PROJECTION * pView * VIEW; /* render all the renderables */ for (auto renderable : mRenderables) { renderable->setRanges(mXMin, mXMax, mYMin, mYMax, mZMin, mZMax); - renderable->render(pWindowId, pX, pY, pVPW, pVPH, PV*pTransform); + renderable->render(pWindowId, pX, pY, pVPW, pVPH, renderableMat); } + glDisable(GL_SCISSOR_TEST); /* Draw borders */ chart3d_impl::bindResources(pWindowId); diff --git a/src/backend/opengl/chart_impl.hpp b/src/backend/opengl/chart_impl.hpp index fa205fd2..37aaef72 100644 --- a/src/backend/opengl/chart_impl.hpp +++ b/src/backend/opengl/chart_impl.hpp @@ -67,6 +67,15 @@ class AbstractChart : public AbstractRenderable { std::vector< std::shared_ptr > mRenderables; /* rendering helper functions */ + inline float getTickStepSize(float minval, float maxval) const { + return (maxval-minval)/(mTickCount-1); + } + + int getNumTicksC2E() const { + /* Get # of ticks from center(0,0) to edge along axis */ + return (mTickCount-1)/2; + } + void renderTickLabels(const int pWindowId, const uint pW, const uint pH, const std::vector &pTexts, const glm::mat4 &pTransformation, const int pCoordsOffset, @@ -123,7 +132,7 @@ class chart2d_impl : public AbstractChart { void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform); + const glm::mat4 &pView); }; class chart3d_impl : public AbstractChart { @@ -144,7 +153,7 @@ class chart3d_impl : public AbstractChart { void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform); + const glm::mat4 &pView); }; } diff --git a/src/backend/opengl/common.hpp b/src/backend/opengl/common.hpp index 2dfd52df..76a6e1c2 100644 --- a/src/backend/opengl/common.hpp +++ b/src/backend/opengl/common.hpp @@ -24,6 +24,7 @@ static const float BLACK[] = {0.0f , 0.0f , 0.0f , 1.0f}; static const float GRAY[] = {0.85f , 0.85f , 0.85f , 1.0f}; static const float WHITE[] = {1.0f , 1.0f , 1.0f , 1.0f}; static const float AF_BLUE[] = {0.0588f , 0.1137f , 0.2745f , 1.0f}; +static const glm::mat4 IDENTITY(1.0f); /* clamp the float to [0-1] range * @@ -237,7 +238,7 @@ class AbstractRenderable { */ virtual void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4 &pTransform) = 0; + const glm::mat4 &pView) = 0; }; } diff --git a/src/backend/opengl/glfw/window.cpp b/src/backend/opengl/glfw/window.cpp index f101ab68..6cb6a803 100644 --- a/src/backend/opengl/glfw/window.cpp +++ b/src/backend/opengl/glfw/window.cpp @@ -220,7 +220,7 @@ void Widget::cursorHandler(const float pXPos, const float pYPos) int r, c; getViewIds(&r, &c); - glm::mat4& mvp = mMVPs[r+c*mRows]; + glm::mat4& mvp = mViewMatrices[r+c*mRows]; if (mButton == GLFW_MOUSE_BUTTON_LEFT) { // Translate @@ -236,6 +236,7 @@ void Widget::cursorHandler(const float pXPos, const float pYPos) mvp = scale(mvp, glm::vec3(pow(deltaY, SPEED))); } } else if (mButton == GLFW_MOUSE_BUTTON_RIGHT) { + // Rotation int width, height; glfwGetWindowSize(mWindow, &width, &height); @@ -276,8 +277,7 @@ void Widget::mouseButtonHandler(int pButton, int pAction, int pMods) if (pButton == GLFW_MOUSE_BUTTON_MIDDLE && pMods == GLFW_MOD_CONTROL && pAction == GLFW_PRESS) { int r, c; getViewIds(&r, &c); - glm::mat4& mvp = mMVPs[r+c*mRows]; - mvp = glm::mat4(1.0f); + mViewMatrices[r+c*mRows] = glm::mat4(1); } } diff --git a/src/backend/opengl/glfw/window.hpp b/src/backend/opengl/glfw/window.hpp index 9c5421db..6274da8e 100644 --- a/src/backend/opengl/glfw/window.hpp +++ b/src/backend/opengl/glfw/window.hpp @@ -55,7 +55,7 @@ class Widget { int mCols; int mCellWidth; int mCellHeight; - std::vector mMVPs; + std::vector mViewMatrices; GLuint mFramePBO; diff --git a/src/backend/opengl/histogram_impl.cpp b/src/backend/opengl/histogram_impl.cpp index 56cd9a2a..e65b1f4d 100644 --- a/src/backend/opengl/histogram_impl.cpp +++ b/src/backend/opengl/histogram_impl.cpp @@ -135,20 +135,18 @@ histogram_impl::~histogram_impl() void histogram_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) + const glm::mat4& pView) { CheckGL("Begin histogram_impl::render"); glDepthMask(GL_FALSE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_SCISSOR_TEST); - glScissor(pX, pY, pVPW, pVPH); glUseProgram(mProgram); glUniform1f(mYMaxIndex, mRange[3]); glUniform1f(mNBinsIndex, (GLfloat)mNBins); - glUniformMatrix4fv(mMatIndex, 1, GL_FALSE, glm::value_ptr(pTransform)); + glUniformMatrix4fv(mMatIndex, 1, GL_FALSE, glm::value_ptr(pView)); glUniform1i(mPVCIndex, mIsPVCOn); glUniform1i(mPVAIndex, mIsPVAOn); glUniform4fv(mBColorIndex, 1, mColor); @@ -161,7 +159,6 @@ void histogram_impl::render(const int pWindowId, histogram_impl::unbindResources(); glUseProgram(0); - glDisable(GL_SCISSOR_TEST); glDisable(GL_BLEND); glDepthMask(GL_TRUE); CheckGL("End histogram_impl::render"); diff --git a/src/backend/opengl/histogram_impl.hpp b/src/backend/opengl/histogram_impl.hpp index f7d40799..22314dad 100644 --- a/src/backend/opengl/histogram_impl.hpp +++ b/src/backend/opengl/histogram_impl.hpp @@ -45,7 +45,6 @@ class histogram_impl : public AbstractRenderable { * for rendering resources */ void bindResources(const int pWindowId); void unbindResources() const; - void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput); public: histogram_impl(const uint pNBins, const fg::dtype pDataType); @@ -53,7 +52,7 @@ class histogram_impl : public AbstractRenderable { void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform); + const glm::mat4 &pView); }; } diff --git a/src/backend/opengl/image_impl.cpp b/src/backend/opengl/image_impl.cpp index 69a925d2..96e2390a 100644 --- a/src/backend/opengl/image_impl.cpp +++ b/src/backend/opengl/image_impl.cpp @@ -132,7 +132,7 @@ uint image_impl::size() const { return (uint)mPBOsize; } void image_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) + const glm::mat4 &pView) { CheckGL("Begin image_impl::render"); @@ -152,7 +152,7 @@ void image_impl::render(const int pWindowId, } } - glm::mat4 strans = glm::scale(pTransform, glm::vec3(xscale, yscale, 1)); + glm::mat4 strans = glm::scale(pView, glm::vec3(xscale, yscale, 1)); glDepthMask(GL_FALSE); glEnable(GL_BLEND); diff --git a/src/backend/opengl/image_impl.hpp b/src/backend/opengl/image_impl.hpp index 7c3996a8..d6c47800 100644 --- a/src/backend/opengl/image_impl.hpp +++ b/src/backend/opengl/image_impl.hpp @@ -66,7 +66,7 @@ class image_impl : public AbstractRenderable { void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform); + const glm::mat4 &pView); }; } diff --git a/src/backend/opengl/plot_impl.cpp b/src/backend/opengl/plot_impl.cpp index 6ed1f08f..e4e7d360 100644 --- a/src/backend/opengl/plot_impl.cpp +++ b/src/backend/opengl/plot_impl.cpp @@ -19,9 +19,6 @@ using namespace std; -// identity matrix -static const glm::mat4 I(1.0f); - namespace opengl { @@ -63,33 +60,31 @@ void plot_impl::unbindResources() const glBindVertexArray(0); } -void plot_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH) +glm::mat4 plot_impl::computeTransformMat(const glm::mat4 pView) { - float range_x = mRange[1] - mRange[0]; - float range_y = mRange[3] - mRange[2]; - float range_z = mRange[5] - mRange[4]; - // set scale to zero if input is constant array - // otherwise compute scale factor by standard equation - float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); - float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); - float graph_scale_z = std::abs(range_z) < 1.0e-3 ? 0.0f : 2/(range_z); - - float coor_offset_x = (-mRange[0] * graph_scale_x); - float coor_offset_y = (-mRange[2] * graph_scale_y); - float coor_offset_z = (-mRange[4] * graph_scale_z); - - glm::mat4 rMat = glm::rotate(I, -glm::radians(90.f), glm::vec3(1,0,0)); - glm::mat4 tMat = glm::translate(I, - glm::vec3(-1 + coor_offset_x , -1 + coor_offset_y, -1 + coor_offset_z)); - glm::mat4 sMat = glm::scale(I, - glm::vec3(1.0f * graph_scale_x, -1.0f * graph_scale_y, 1.0f * graph_scale_z)); - - glm::mat4 model= rMat * tMat * sMat; - - pOut = pInput * model; - glScissor(pX, pY, pVPW, pVPH); + static const glm::mat4 MODEL = glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(0,1,0)) * + glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(1,0,0)); + + float xRange = mRange[1] - mRange[0]; + float yRange = mRange[3] - mRange[2]; + float zRange = mRange[5] - mRange[4]; + + float xDataScale = std::abs(xRange) < 1.0e-3 ? 0.0f : 2/(xRange); + float yDataScale = std::abs(yRange) < 1.0e-3 ? 0.0f : 2/(yRange); + float zDataScale = std::abs(zRange) < 1.0e-3 ? 0.0f : 2/(zRange); + + float xDataOffset = (-mRange[0] * xDataScale); + float yDataOffset = (-mRange[2] * yDataScale); + float zDataOffset = (-mRange[4] * zDataScale); + + glm::vec3 scaleVector(xDataScale, -1.0f * yDataScale, zDataScale); + + glm::vec3 shiftVector(-(mRange[0]+mRange[1])/2.0f, + -(mRange[2]+mRange[3])/2.0f, + -(mRange[4]+mRange[5])/2.0f); + shiftVector += glm::vec3(-1 + xDataOffset, -1 + yDataOffset, -1 + zDataOffset); + + return pView * glm::translate(glm::scale(MODEL, scaleVector), shiftVector); } void plot_impl::bindDimSpecificUniforms() @@ -205,22 +200,22 @@ size_t plot_impl::markersSizes() const void plot_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) + const glm::mat4& pView) { CheckGL("Begin plot_impl::render"); - glEnable(GL_SCISSOR_TEST); - glDepthMask(GL_FALSE); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + if (mIsPVAOn) { + glDepthMask(GL_FALSE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } - glm::mat4 mvp(1.0); - this->computeTransformMat(mvp, pTransform, pX, pY, pVPW, pVPH); + glm::mat4 viewModelMatrix = this->computeTransformMat(pView); if (mPlotType == FG_PLOT_LINE) { glUseProgram(mPlotProgram); this->bindDimSpecificUniforms(); - glUniformMatrix4fv(mPlotMatIndex, 1, GL_FALSE, glm::value_ptr(mvp)); + glUniformMatrix4fv(mPlotMatIndex, 1, GL_FALSE, glm::value_ptr(viewModelMatrix)); glUniform1i(mPlotPVCOnIndex, mIsPVCOn); glUniform1i(mPlotPVAOnIndex, mIsPVAOn); @@ -235,7 +230,7 @@ void plot_impl::render(const int pWindowId, glEnable(GL_PROGRAM_POINT_SIZE); glUseProgram(mMarkerProgram); - glUniformMatrix4fv(mMarkerMatIndex, 1, GL_FALSE, glm::value_ptr(mvp)); + glUniformMatrix4fv(mMarkerMatIndex, 1, GL_FALSE, glm::value_ptr(viewModelMatrix)); glUniform1i(mMarkerPVCOnIndex, mIsPVCOn); glUniform1i(mMarkerPVAOnIndex, mIsPVAOn); glUniform1i(mMarkerPVROnIndex, mIsPVROn); @@ -251,54 +246,25 @@ void plot_impl::render(const int pWindowId, glDisable(GL_PROGRAM_POINT_SIZE); } - glDisable(GL_BLEND); - glDepthMask(GL_TRUE); - glDisable(GL_SCISSOR_TEST); + if (mIsPVAOn) { + glDisable(GL_BLEND); + glDepthMask(GL_TRUE); + } CheckGL("End plot_impl::render"); } -void plot2d_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH) +glm::mat4 plot2d_impl::computeTransformMat(const glm::mat4 pView) { - float range_x = mRange[1] - mRange[0]; - float range_y = mRange[3] - mRange[2]; - // set scale to zero if input is constant array - // otherwise compute scale factor by standard equation - float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); - float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); - - float coor_offset_x = (-mRange[0] * graph_scale_x); - float coor_offset_y = (-mRange[2] * graph_scale_y); - - //FIXME: Using hard constants for now, find a way to get chart values - const float lMargin = 68; - const float rMargin = 8; - const float tMargin = 8; - const float bMargin = 44; - const float tickSize = 10; - - float viewWidth = pVPW - (lMargin + rMargin + tickSize/2); - float viewHeight = pVPH - (bMargin + tMargin + tickSize/2); - float view_scale_x = viewWidth/pVPW; - float view_scale_y = viewHeight/pVPH; - - coor_offset_x *= view_scale_x; - coor_offset_y *= view_scale_y; - - float view_offset_x = (2.0f * (lMargin + tickSize/2 )/ pVPW ) ; - float view_offset_y = (2.0f * (bMargin + tickSize )/ pVPH ) ; - - glm::mat4 tMat = glm::translate(I, - glm::vec3(-1 + view_offset_x + coor_offset_x , -1 + view_offset_y + coor_offset_y, 0)); - pOut = glm::scale(tMat, - glm::vec3(graph_scale_x * view_scale_x , graph_scale_y * view_scale_y ,1)); - - pOut = pInput * pOut; - - glScissor(pX + lMargin + tickSize/2, pY+bMargin + tickSize/2, - pVPW - lMargin - rMargin - tickSize/2, - pVPH - bMargin - tMargin - tickSize/2); + float xRange = mRange[1] - mRange[0]; + float yRange = mRange[3] - mRange[2]; + + float xDataScale = std::abs(xRange) < 1.0e-3 ? 1.0f : 2/(xRange); + float yDataScale = std::abs(yRange) < 1.0e-3 ? 1.0f : 2/(yRange); + + glm::vec3 shiftVector(-(mRange[0]+mRange[1])/2.0f, -(mRange[2]+mRange[3])/2.0f, 0.0f); + glm::vec3 scaleVector(xDataScale, yDataScale, 1); + + return pView * glm::translate(glm::scale(IDENTITY, scaleVector), shiftVector); } void plot2d_impl::bindDimSpecificUniforms() diff --git a/src/backend/opengl/plot_impl.hpp b/src/backend/opengl/plot_impl.hpp index 4e9cee60..9996db9f 100644 --- a/src/backend/opengl/plot_impl.hpp +++ b/src/backend/opengl/plot_impl.hpp @@ -67,9 +67,8 @@ class plot_impl : public AbstractRenderable { void bindResources(const int pWindowId); void unbindResources() const; - virtual void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH); + virtual glm::mat4 computeTransformMat(const glm::mat4 pView); + virtual void bindDimSpecificUniforms(); // has to be called only after shaders are bound public: @@ -85,14 +84,13 @@ class plot_impl : public AbstractRenderable { virtual void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform); + const glm::mat4 &pView); }; class plot2d_impl : public plot_impl { protected: - void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH) override; + glm::mat4 computeTransformMat(const glm::mat4 pView) override; + void bindDimSpecificUniforms() override; // has to be called only after shaders are bound public: diff --git a/src/backend/opengl/shaders/vector_field2d_gs.glsl b/src/backend/opengl/shaders/vector_field2d_gs.glsl index aaeb2528..85ae8674 100644 --- a/src/backend/opengl/shaders/vector_field2d_gs.glsl +++ b/src/backend/opengl/shaders/vector_field2d_gs.glsl @@ -2,7 +2,8 @@ layout(points) in; layout(triangle_strip, max_vertices = 9) out; -uniform mat4 transform; +uniform mat4 arrowScaleMat; +uniform mat4 viewMat; in VS_OUT { vec4 color; @@ -11,43 +12,32 @@ in VS_OUT { out vec4 pervcol; -mat4 rotationMatrix(vec3 axis, float angle) +mat4 zrotate(float angle) { - axis = normalize(axis); float s = sin(angle); float c = cos(angle); - float oc = 1.0 - c; - - return mat4(oc * axis.x * axis.x + c, - oc * axis.x * axis.y - axis.z * s, - oc * axis.z * axis.x + axis.y * s, - 0.0, - oc * axis.x * axis.y + axis.z * s, - oc * axis.y * axis.y + c, - oc * axis.y * axis.z - axis.x * s, - 0.0, - oc * axis.z * axis.x - axis.y * s, - oc * axis.y * axis.z + axis.x * s, - oc * axis.z * axis.z + c, - 0.0, - 0.0, 0.0, 0.0, 1.0); + + return mat4(c, -s, 0, 0, + s, c, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); } void main() { - vec4 pos = gl_in[0].gl_Position; - vec2 dir = normalize(gs_in[0].dir); - - mat4 rmat = rotationMatrix(vec3(0,0,1), atan(dir.y, dir.x)); - mat4 trans = transform * rmat; - - vec4 origin = pos + trans * vec4(0,0,0,1); // (2/5) - vec4 leftTip = pos + trans * vec4(-0.333, -0.333, 0.0, 1.0); // (1) - vec4 leftTop = pos + trans * vec4(-0.167, -0.333, 0.0, 1.0); // (3/7) - vec4 leftBot = pos + trans * vec4(-0.167, -1.000, 0.0, 1.0); // (9) - vec4 rightBot = pos + trans * vec4( 0.167, -1.000, 0.0, 1.0); // (8) - vec4 rightTop = pos + trans * vec4( 0.167, -0.333, 0.0, 1.0); // (6) - vec4 rightTip = pos + trans * vec4( 0.333, -0.333, 0.0, 1.0); // (4) + vec2 dir = normalize(gs_in[0].dir); + + mat4 arrowModelMat = arrowScaleMat * zrotate(atan(dir.y, dir.x)); + + vec4 pos = gl_in[0].gl_Position; + + vec4 origin = viewMat * (pos + arrowModelMat*vec4( 0.000, 0.333, 0.0, 1.0)); // (2/5) + vec4 leftTip = viewMat * (pos + arrowModelMat*vec4(-0.333, -0.333, 0.0, 1.0)); // (1) + vec4 leftTop = viewMat * (pos + arrowModelMat*vec4(-0.167, -0.333, 0.0, 1.0)); // (3/7) + vec4 leftBot = viewMat * (pos + arrowModelMat*vec4(-0.167, -1.000, 0.0, 1.0)); // (9) + vec4 rightBot = viewMat * (pos + arrowModelMat*vec4( 0.167, -1.000, 0.0, 1.0)); // (8) + vec4 rightTop = viewMat * (pos + arrowModelMat*vec4( 0.167, -0.333, 0.0, 1.0)); // (6) + vec4 rightTip = viewMat * (pos + arrowModelMat*vec4( 0.333, -0.333, 0.0, 1.0)); // (4) // (2/5) // //\\ diff --git a/src/backend/opengl/shaders/vector_field2d_vs.glsl b/src/backend/opengl/shaders/vector_field2d_vs.glsl index abc0c116..0e0de54a 100644 --- a/src/backend/opengl/shaders/vector_field2d_vs.glsl +++ b/src/backend/opengl/shaders/vector_field2d_vs.glsl @@ -1,12 +1,12 @@ #version 330 +uniform mat4 modelMat; + in vec2 point; in vec3 color; in float alpha; in vec2 direction; -uniform mat4 transform; - out VS_OUT { vec4 color; vec2 dir; @@ -16,5 +16,5 @@ void main(void) { vs_out.color = vec4(color, alpha); vs_out.dir = direction; - gl_Position = transform * vec4(point.xy, 0.0, 1); + gl_Position = modelMat * vec4(point.xy, 0.0, 1); } diff --git a/src/backend/opengl/shaders/vector_field_gs.glsl b/src/backend/opengl/shaders/vector_field_gs.glsl index 9d86d2ce..48335d5d 100644 --- a/src/backend/opengl/shaders/vector_field_gs.glsl +++ b/src/backend/opengl/shaders/vector_field_gs.glsl @@ -2,7 +2,8 @@ layout(points) in; layout(triangle_strip, max_vertices = 25) out; -uniform mat4 transform; +uniform mat4 arrowScaleMat; +uniform mat4 viewMat; in VS_OUT { vec4 color; @@ -37,28 +38,30 @@ void main() { vec4 pos = gl_in[0].gl_Position; vec3 dir = normalize(gs_in[0].dir); + float theta = acos(dir.z/ 1.0); float phi = atan(dir.y/ dir.x); + mat4 zrot = rotationMatrix(vec3(0,0,1), phi); vec4 sndAxs = zrot * vec4(0,1,0,1); mat4 arot = rotationMatrix(sndAxs.xyz, theta); - mat4 trans = transform * arot * zrot; + mat4 trans = arrowScaleMat * arot * zrot; - vec4 origin = pos + trans * vec4(0,0,0,1); - vec4 ltop = pos + trans * vec4(-0.333, 0.333, 0.333, 1.0); - vec4 lbot = pos + trans * vec4(-0.333,-0.333, 0.333, 1.0); - vec4 rtop = pos + trans * vec4(-0.333,-0.333,-0.333, 1.0); - vec4 rbot = pos + trans * vec4(-0.333, 0.333,-0.333, 1.0); + vec4 origin = viewMat * (pos + trans * vec4(0,0,0,1)); + vec4 ltop = viewMat * (pos + trans * vec4(-0.333, 0.333, 0.333, 1.0)); + vec4 lbot = viewMat * (pos + trans * vec4(-0.333,-0.333, 0.333, 1.0)); + vec4 rtop = viewMat * (pos + trans * vec4(-0.333,-0.333,-0.333, 1.0)); + vec4 rbot = viewMat * (pos + trans * vec4(-0.333, 0.333,-0.333, 1.0)); - vec4 iltop = pos + trans * vec4(-0.333, 0.167, 0.167, 1.0); - vec4 ilbot = pos + trans * vec4(-0.333,-0.167, 0.167, 1.0); - vec4 irtop = pos + trans * vec4(-0.333,-0.167,-0.167, 1.0); - vec4 irbot = pos + trans * vec4(-0.333, 0.167,-0.167, 1.0); + vec4 iltop = viewMat * (pos + trans * vec4(-0.333, 0.167, 0.167, 1.0)); + vec4 ilbot = viewMat * (pos + trans * vec4(-0.333,-0.167, 0.167, 1.0)); + vec4 irtop = viewMat * (pos + trans * vec4(-0.333,-0.167,-0.167, 1.0)); + vec4 irbot = viewMat * (pos + trans * vec4(-0.333, 0.167,-0.167, 1.0)); - vec4 iltop2 = pos + trans * vec4(-1.000, 0.167, 0.167, 1.0); - vec4 ilbot2 = pos + trans * vec4(-1.000,-0.167, 0.167, 1.0); - vec4 irtop2 = pos + trans * vec4(-1.000,-0.167,-0.167, 1.0); - vec4 irbot2 = pos + trans * vec4(-1.000, 0.167,-0.167, 1.0); + vec4 iltop2 = viewMat * (pos + trans * vec4(-1.000, 0.167, 0.167, 1.0)); + vec4 ilbot2 = viewMat * (pos + trans * vec4(-1.000,-0.167, 0.167, 1.0)); + vec4 irtop2 = viewMat * (pos + trans * vec4(-1.000,-0.167,-0.167, 1.0)); + vec4 irbot2 = viewMat * (pos + trans * vec4(-1.000, 0.167,-0.167, 1.0)); pervcol = gs_in[0].color; diff --git a/src/backend/opengl/shaders/vector_field_vs.glsl b/src/backend/opengl/shaders/vector_field_vs.glsl index 61284695..d535837e 100644 --- a/src/backend/opengl/shaders/vector_field_vs.glsl +++ b/src/backend/opengl/shaders/vector_field_vs.glsl @@ -1,6 +1,6 @@ #version 330 -uniform mat4 transform; +uniform mat4 modelMat; in vec3 point; in vec3 color; @@ -16,5 +16,5 @@ void main(void) { vs_out.color = vec4(color, alpha); vs_out.dir = direction; - gl_Position = transform * vec4(point.xyz, 1); + gl_Position = modelMat * vec4(point.xyz, 1); } diff --git a/src/backend/opengl/surface_impl.cpp b/src/backend/opengl/surface_impl.cpp index 726a36b7..4ada51e8 100644 --- a/src/backend/opengl/surface_impl.cpp +++ b/src/backend/opengl/surface_impl.cpp @@ -24,89 +24,86 @@ using namespace std; void generateGridIndices(unsigned short rows, unsigned short cols, unsigned short *indices) { - unsigned short idx = 0; - for(unsigned short r = 0; r < rows-1; ++r){ - for(unsigned short c = 0; c < cols*2; ++c){ - unsigned short i = c + (r * (cols*2)); - - if(c == cols * 2 - 1) { - *indices++ = idx; - }else{ - *indices++ = idx; - if(i%2 == 0){ - idx += cols; - } else { - idx -= (r%2 == 0) ? (cols-1) : (cols+1); - } +unsigned short idx = 0; +for(unsigned short r = 0; r < rows-1; ++r){ + for(unsigned short c = 0; c < cols*2; ++c){ + unsigned short i = c + (r * (cols*2)); + + if(c == cols * 2 - 1) { + *indices++ = idx; + }else{ + *indices++ = idx; + if(i%2 == 0){ + idx += cols; + } else { + idx -= (r%2 == 0) ? (cols-1) : (cols+1); } } } } +} namespace opengl { void surface_impl::bindResources(const int pWindowId) { - if (mVAOMap.find(pWindowId) == mVAOMap.end()) { - GLuint vao = 0; - /* create a vertex array object - * with appropriate bindings */ - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - // attach plot vertices - glEnableVertexAttribArray(mSurfPointIndex); - glBindBuffer(GL_ARRAY_BUFFER, mVBO); - glVertexAttribPointer(mSurfPointIndex, 3, mDataType, GL_FALSE, 0, 0); - glEnableVertexAttribArray(mSurfColorIndex); - glBindBuffer(GL_ARRAY_BUFFER, mCBO); - glVertexAttribPointer(mSurfColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(mSurfAlphaIndex); - glBindBuffer(GL_ARRAY_BUFFER, mABO); - glVertexAttribPointer(mSurfAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0); - //attach indices - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO); - glBindVertexArray(0); - /* store the vertex array object corresponding to - * the window instance in the map */ - mVAOMap[pWindowId] = vao; - } +if (mVAOMap.find(pWindowId) == mVAOMap.end()) { + GLuint vao = 0; + /* create a vertex array object + * with appropriate bindings */ + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + // attach plot vertices + glEnableVertexAttribArray(mSurfPointIndex); + glBindBuffer(GL_ARRAY_BUFFER, mVBO); + glVertexAttribPointer(mSurfPointIndex, 3, mDataType, GL_FALSE, 0, 0); + glEnableVertexAttribArray(mSurfColorIndex); + glBindBuffer(GL_ARRAY_BUFFER, mCBO); + glVertexAttribPointer(mSurfColorIndex, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(mSurfAlphaIndex); + glBindBuffer(GL_ARRAY_BUFFER, mABO); + glVertexAttribPointer(mSurfAlphaIndex, 1, GL_FLOAT, GL_FALSE, 0, 0); + //attach indices + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO); + glBindVertexArray(0); + /* store the vertex array object corresponding to + * the window instance in the map */ + mVAOMap[pWindowId] = vao; +} - glBindVertexArray(mVAOMap[pWindowId]); +glBindVertexArray(mVAOMap[pWindowId]); } void surface_impl::unbindResources() const { - glBindVertexArray(0); +glBindVertexArray(0); } -void surface_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput) +glm::mat4 surface_impl::computeTransformMat(const glm::mat4& pView) { - // identity matrix - static const glm::mat4 I(1.0f); + static const glm::mat4 MODEL = glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(0,1,0)) * + glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(1,0,0)); float range_x = mRange[1] - mRange[0]; float range_y = mRange[3] - mRange[2]; float range_z = mRange[5] - mRange[4]; // set scale to zero if input is constant array // otherwise compute scale factor by standard equation - float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); - float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); - float graph_scale_z = std::abs(range_z) < 1.0e-3 ? 0.0f : 2/(range_z); + float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 1.0f : 2/(range_x); + float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 1.0f : 2/(range_y); + float graph_scale_z = std::abs(range_z) < 1.0e-3 ? 1.0f : 2/(range_z); float coor_offset_x = (-mRange[0] * graph_scale_x); float coor_offset_y = (-mRange[2] * graph_scale_y); float coor_offset_z = (-mRange[4] * graph_scale_z); - glm::mat4 rMat = glm::rotate(I, -glm::radians(90.f), glm::vec3(1,0,0)); - glm::mat4 tMat = glm::translate(I, - glm::vec3(-1 + coor_offset_x , -1 + coor_offset_y, -1 + coor_offset_z)); - glm::mat4 sMat = glm::scale(I, - glm::vec3(1.0f * graph_scale_x, -1.0f * graph_scale_y, 1.0f * graph_scale_z)); - - glm::mat4 model = rMat * tMat * sMat; + glm::mat4 sMat = glm::scale(MODEL, + glm::vec3(graph_scale_x, -1.0f * graph_scale_y, graph_scale_z)); + glm::mat4 tMat = glm::translate(sMat, + glm::vec3(-1 + coor_offset_x, -1 + coor_offset_y, -1 + coor_offset_z)); - pOut = pInput * model; + return pView * tMat; } void surface_impl::renderGraph(const int pWindowId, const glm::mat4& transform) @@ -231,7 +228,7 @@ surface_impl::~surface_impl() void surface_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4 &pModel) + const glm::mat4& pView) { CheckGL("Begin surface_impl::render"); // FIXME: even when per vertex alpha is enabled @@ -243,9 +240,7 @@ void surface_impl::render(const int pWindowId, glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } - glm::mat4 mvp(1.0); - computeTransformMat(mvp, pModel); - renderGraph(pWindowId, mvp); + renderGraph(pWindowId, computeTransformMat(pView)); if (mIsPVAOn) { glDisable(GL_BLEND); diff --git a/src/backend/opengl/surface_impl.hpp b/src/backend/opengl/surface_impl.hpp index fde99bb0..1be9119b 100644 --- a/src/backend/opengl/surface_impl.hpp +++ b/src/backend/opengl/surface_impl.hpp @@ -57,7 +57,7 @@ class surface_impl : public AbstractRenderable { * for rendering resources */ void bindResources(const int pWindowId); void unbindResources() const; - void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput); + glm::mat4 computeTransformMat(const glm::mat4& pView); virtual void renderGraph(const int pWindowId, const glm::mat4& transform); public: @@ -67,7 +67,7 @@ class surface_impl : public AbstractRenderable { void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4 &pTransform); + const glm::mat4 &pView); inline void usePerVertexColors(const bool pFlag=true) { mIsPVCOn = pFlag; diff --git a/src/backend/opengl/vector_field_impl.cpp b/src/backend/opengl/vector_field_impl.cpp index e99435c3..5cf96895 100644 --- a/src/backend/opengl/vector_field_impl.cpp +++ b/src/backend/opengl/vector_field_impl.cpp @@ -63,39 +63,35 @@ void vector_field_impl::unbindResources() const glBindVertexArray(0); } -void vector_field_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH) +glm::mat4 vector_field_impl::computeModelMatrix() { - float range_x = mRange[1] - mRange[0]; - float range_y = mRange[3] - mRange[2]; - float range_z = mRange[5] - mRange[4]; - // set scale to zero if input is constant array - // otherwise compute scale factor by standard equation - float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); - float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); - float graph_scale_z = std::abs(range_z) < 1.0e-3 ? 0.0f : 2/(range_z); - - float coor_offset_x = (-mRange[0] * graph_scale_x); - float coor_offset_y = (-mRange[2] * graph_scale_y); - float coor_offset_z = (-mRange[4] * graph_scale_z); - - glm::mat4 rMat = glm::rotate(I, -glm::radians(90.f), glm::vec3(1,0,0)); - glm::mat4 tMat = glm::translate(I, - glm::vec3(-1 + coor_offset_x , -1 + coor_offset_y, -1 + coor_offset_z)); - glm::mat4 sMat = glm::scale(I, - glm::vec3(1.0f * graph_scale_x, -1.0f * graph_scale_y, 1.0f * graph_scale_z)); - - glm::mat4 model= rMat * tMat * sMat; - - pOut = pInput * model; - glScissor(pX, pY, pVPW, pVPH); + float xRange = mRange[1] - mRange[0]; + float yRange = mRange[3] - mRange[2]; + float zRange = mRange[5] - mRange[4]; + + float xDataScale = std::abs(xRange) < 1.0e-3 ? 0.0f : 4/(xRange); + float yDataScale = std::abs(yRange) < 1.0e-3 ? 0.0f : 4/(yRange); + float zDataScale = std::abs(zRange) < 1.0e-3 ? 0.0f : 4/(zRange); + + float xDataOffset = (-mRange[0] * xDataScale); + float yDataOffset = (-mRange[2] * yDataScale); + float zDataOffset = (-mRange[4] * zDataScale); + + glm::vec3 scaleVector(xDataScale, -1.0f * yDataScale, zDataScale); + + glm::vec3 shiftVector(-(mRange[0]+mRange[1])/2.0f, + -(mRange[2]+mRange[3])/2.0f, + -(mRange[4]+mRange[5])/2.0f); + shiftVector += glm::vec3(-1 + xDataOffset, -1 + yDataOffset, -1 + zDataOffset); + + return glm::translate(glm::scale(IDENTITY, scaleVector), shiftVector); } vector_field_impl::vector_field_impl(const uint pNumPoints, const fg::dtype pDataType, const int pD) : mDimension(pD), mNumPoints(pNumPoints), mDataType(pDataType), mGLType(dtype2gl(mDataType)), - mFieldProgram(-1), mDBO(-1), mDBOSize(0), mFieldMatIndex(-1), mFieldPointIndex(-1), + mFieldProgram(-1), mDBO(-1), mDBOSize(0), mFieldPointIndex(-1), mFieldColorIndex(-1), mFieldAlphaIndex(-1), mFieldDirectionIndex(-1), + mFieldPVMatIndex(-1), mFieldModelMatIndex(-1), mFieldAScaleMatIndex(-1), mFieldPVCOnIndex(-1), mFieldPVAOnIndex(-1), mFieldUColorIndex(-1) { CheckGL("Begin vector_field_impl::vector_field_impl"); @@ -121,12 +117,15 @@ vector_field_impl::vector_field_impl(const uint pNumPoints, const fg::dtype pDat mCBOSize = 3*mNumPoints; mABOSize = mNumPoints; - mFieldMatIndex = glGetUniformLocation(mFieldProgram, "transform"); mFieldPointIndex = glGetAttribLocation (mFieldProgram, "point"); mFieldColorIndex = glGetAttribLocation (mFieldProgram, "color"); mFieldAlphaIndex = glGetAttribLocation (mFieldProgram, "alpha"); mFieldDirectionIndex = glGetAttribLocation (mFieldProgram, "direction"); + mFieldPVMatIndex = glGetUniformLocation(mFieldProgram, "viewMat"); + mFieldModelMatIndex = glGetUniformLocation(mFieldProgram, "modelMat"); + mFieldAScaleMatIndex = glGetUniformLocation(mFieldProgram, "arrowScaleMat"); + mFieldPVCOnIndex = glGetUniformLocation(mFieldProgram, "isPVCOn"); mFieldPVAOnIndex = glGetUniformLocation(mFieldProgram, "isPVAOn"); mFieldUColorIndex = glGetUniformLocation(mFieldProgram, "barColor"); @@ -181,79 +180,53 @@ size_t vector_field_impl::directionsSize() const void vector_field_impl::render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform) + const glm::mat4& pView) { + static const glm::mat4 ArrowScaleMat = glm::scale(glm::mat4(1), glm::vec3(0.1,0.1,0.1)); + CheckGL("Begin vector_field_impl::render"); - glEnable(GL_SCISSOR_TEST); - glDepthMask(GL_FALSE); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + if (mIsPVAOn) { + glDepthMask(GL_FALSE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } - glm::mat4 mvp(1.0); - this->computeTransformMat(mvp, pTransform, pX, pY, pVPW, pVPH); + glm::mat4 model = this->computeModelMatrix(); glUseProgram(mFieldProgram); - glUniformMatrix4fv(mFieldMatIndex, 1, GL_FALSE, glm::value_ptr(mvp)); + glUniformMatrix4fv(mFieldPVMatIndex, 1, GL_FALSE, glm::value_ptr(pView)); + glUniformMatrix4fv(mFieldModelMatIndex, 1, GL_FALSE, glm::value_ptr(model)); + glUniformMatrix4fv(mFieldAScaleMatIndex, 1, GL_FALSE, glm::value_ptr(ArrowScaleMat)); glUniform1i(mFieldPVCOnIndex, mIsPVCOn); glUniform1i(mFieldPVAOnIndex, mIsPVAOn); glUniform4fv(mFieldUColorIndex, 1, mColor); - //FIXME remove debug polygon mode setting vector_field_impl::bindResources(pWindowId); glDrawArrays(GL_POINTS, 0, mNumPoints); vector_field_impl::unbindResources(); glUseProgram(0); - glDisable(GL_BLEND); - glDepthMask(GL_TRUE); - glDisable(GL_SCISSOR_TEST); + if (mIsPVAOn) { + glDisable(GL_BLEND); + glDepthMask(GL_TRUE); + } CheckGL("End vector_field_impl::render"); } -void vector_field2d_impl::computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH) +glm::mat4 vector_field2d_impl::computeModelMatrix() { - float range_x = mRange[1] - mRange[0]; - float range_y = mRange[3] - mRange[2]; - // set scale to zero if input is constant array - // otherwise compute scale factor by standard equation - float graph_scale_x = std::abs(range_x) < 1.0e-3 ? 0.0f : 2/(range_x); - float graph_scale_y = std::abs(range_y) < 1.0e-3 ? 0.0f : 2/(range_y); - - float coor_offset_x = (-mRange[0] * graph_scale_x); - float coor_offset_y = (-mRange[2] * graph_scale_y); - - //FIXME: Using hard constants for now, find a way to get chart values - const float lMargin = 68; - const float rMargin = 8; - const float tMargin = 8; - const float bMargin = 44; - const float tickSize = 10; - - float viewWidth = pVPW - (lMargin + rMargin + tickSize/2); - float viewHeight = pVPH - (bMargin + tMargin + tickSize/2); - float view_scale_x = viewWidth/pVPW; - float view_scale_y = viewHeight/pVPH; - - coor_offset_x *= view_scale_x; - coor_offset_y *= view_scale_y; - - float view_offset_x = (2.0f * (lMargin + tickSize/2 )/ pVPW ) ; - float view_offset_y = (2.0f * (bMargin + tickSize )/ pVPH ) ; - - glm::mat4 tMat = glm::translate(I, - glm::vec3(-1 + view_offset_x + coor_offset_x , -1 + view_offset_y + coor_offset_y, 0)); - pOut = glm::scale(tMat, - glm::vec3(graph_scale_x * view_scale_x , graph_scale_y * view_scale_y ,1)); - - pOut = pInput * pOut; - - glScissor(pX + lMargin + tickSize/2, pY+bMargin + tickSize/2, - pVPW - lMargin - rMargin - tickSize/2, - pVPH - bMargin - tMargin - tickSize/2); + float xRange = mRange[1] - mRange[0]; + float yRange = mRange[3] - mRange[2]; + + float xDataScale = std::abs(xRange) < 1.0e-3 ? 1.0f : 4/(xRange); + float yDataScale = std::abs(yRange) < 1.0e-3 ? 1.0f : 4/(yRange); + + glm::vec3 shiftVector(-(mRange[0]+mRange[1])/2.0f, -(mRange[2]+mRange[3])/2.0f, 0.0f); + glm::vec3 scaleVector(xDataScale, yDataScale, 1); + + return glm::translate(glm::scale(IDENTITY, scaleVector), shiftVector); } } diff --git a/src/backend/opengl/vector_field_impl.hpp b/src/backend/opengl/vector_field_impl.hpp index 5300443a..2f215f86 100644 --- a/src/backend/opengl/vector_field_impl.hpp +++ b/src/backend/opengl/vector_field_impl.hpp @@ -35,11 +35,14 @@ class vector_field_impl : public AbstractRenderable { size_t mDBOSize; /* shader variable index locations */ /* vertex shader */ - GLuint mFieldMatIndex; GLuint mFieldPointIndex; GLuint mFieldColorIndex; GLuint mFieldAlphaIndex; GLuint mFieldDirectionIndex; + /* geometry shader */ + GLuint mFieldPVMatIndex; + GLuint mFieldModelMatIndex; + GLuint mFieldAScaleMatIndex; /* fragment shader */ GLuint mFieldPVCOnIndex; GLuint mFieldPVAOnIndex; @@ -52,9 +55,7 @@ class vector_field_impl : public AbstractRenderable { void bindResources(const int pWindowId); void unbindResources() const; - virtual void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH); + virtual glm::mat4 computeModelMatrix(); public: vector_field_impl(const uint pNumPoints, const fg::dtype pDataType, @@ -66,14 +67,12 @@ class vector_field_impl : public AbstractRenderable { virtual void render(const int pWindowId, const int pX, const int pY, const int pVPW, const int pVPH, - const glm::mat4& pTransform); + const glm::mat4 &pView); }; class vector_field2d_impl : public vector_field_impl { protected: - void computeTransformMat(glm::mat4& pOut, const glm::mat4 pInput, - const int pX, const int pY, - const int pVPW, const int pVPH) override; + glm::mat4 computeModelMatrix() override; public: vector_field2d_impl(const uint pNumPoints, const fg::dtype pDataType) : vector_field_impl(pNumPoints, pDataType, 2) {} diff --git a/src/backend/opengl/window_impl.cpp b/src/backend/opengl/window_impl.cpp index c24c11e7..5fe8f798 100644 --- a/src/backend/opengl/window_impl.cpp +++ b/src/backend/opengl/window_impl.cpp @@ -160,7 +160,7 @@ window_impl::window_impl(int pWidth, int pHeight, const char* pTitle, mUBOSize = mCMap->defaultLen(); glEnable(GL_MULTISAMPLE); - std::vector& mats = mWindow->mMVPs; + std::vector& mats = mWindow->mViewMatrices; mats.resize(mWindow->mRows*mWindow->mCols); std::fill(mats.begin(), mats.end(), glm::mat4(1)); @@ -297,14 +297,14 @@ void window_impl::draw(const std::shared_ptr& pRenderable) mWindow->resetCloseFlag(); glViewport(0, 0, mWindow->mWidth, mWindow->mHeight); - const glm::mat4& mvp = mWindow->mMVPs[0]; + const glm::mat4& viewMatrix = mWindow->mViewMatrices[0]; // clear color and depth buffers glClearColor(WHITE[0], WHITE[1], WHITE[2], WHITE[3]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set colormap call is equivalent to noop for non-image renderables pRenderable->setColorMapUBOParams(mColorMapUBO, mUBOSize); - pRenderable->render(mID, 0, 0, mWindow->mWidth, mWindow->mHeight, mvp); + pRenderable->render(mID, 0, 0, mWindow->mWidth, mWindow->mHeight, viewMatrix); mWindow->swapBuffers(); mWindow->pollEvents(); @@ -322,8 +322,8 @@ void window_impl::grid(int pRows, int pCols) mWindow->mCellWidth = mWindow->mWidth / mWindow->mCols; mWindow->mCellHeight = mWindow->mHeight / mWindow->mRows; - // resize mvp array for views to appropriate size - std::vector& mats = mWindow->mMVPs; + // resize viewMatrix array for views to appropriate size + std::vector& mats = mWindow->mViewMatrices; mats.resize(mWindow->mRows*mWindow->mCols); std::fill(mats.begin(), mats.end(), glm::mat4(1)); } @@ -342,7 +342,7 @@ void window_impl::draw(int pColId, int pRowId, int x_off = c * mWindow->mCellWidth; int y_off = (mWindow->mRows - 1 - r) * mWindow->mCellHeight; - const glm::mat4& mvp = mWindow->mMVPs[r+c*mWindow->mRows]; + const glm::mat4& viewMatrix = mWindow->mViewMatrices[r+c*mWindow->mRows]; /* following margins are tested out for various * aspect ratios and are working fine. DO NOT CHANGE. * */ @@ -359,7 +359,7 @@ void window_impl::draw(int pColId, int pRowId, // set colormap call is equivalent to noop for non-image renderables pRenderable->setColorMapUBOParams(mColorMapUBO, mUBOSize); - pRenderable->render(mID, x_off, y_off, mWindow->mCellWidth, mWindow->mCellHeight, mvp); + pRenderable->render(mID, x_off, y_off, mWindow->mCellWidth, mWindow->mCellHeight, viewMatrix); glDisable(GL_SCISSOR_TEST); glViewport(x_off, y_off, mWindow->mCellWidth, mWindow->mCellHeight); From 7eb68760dca716bb5904371dc9b0774d58807c8c Mon Sep 17 00:00:00 2001 From: pradeep Date: Tue, 21 Jun 2016 22:29:04 +0530 Subject: [PATCH 09/16] Modify cuda/opencl plotting examples to be similar to cpu --- examples/cuda/plotting.cu | 20 +++++++++++--------- examples/opencl/plotting.cpp | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/cuda/plotting.cu b/examples/cuda/plotting.cu index 0882e246..c56e3f7f 100644 --- a/examples/cuda/plotting.cu +++ b/examples/cuda/plotting.cu @@ -29,10 +29,9 @@ int main(void) */ fg::Window wnd(DIMX, DIMY, "Plotting Demo"); wnd.makeCurrent(); - wnd.grid(1,2); fg::Chart chart(FG_CHART_2D); - chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.1f, 1.1f); + chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.0f, 1.0f); /* Create several plot objects which creates the necessary * vertex buffer objects to hold the different plot types @@ -91,22 +90,25 @@ int main(void) __global__ void simple_sinf(float* out, const size_t DATA_SIZE, int fnCode) { - int x = blockIdx.x * blockDim.x + threadIdx.x; + int i = blockIdx.x * blockDim.x + threadIdx.x; + + if (i Date: Tue, 21 Jun 2016 22:44:06 +0530 Subject: [PATCH 10/16] Fix 2d graph OpenGL clip coordinates --- src/backend/opengl/chart_impl.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/backend/opengl/chart_impl.cpp b/src/backend/opengl/chart_impl.cpp index a4007aa2..366684f1 100644 --- a/src/backend/opengl/chart_impl.cpp +++ b/src/backend/opengl/chart_impl.cpp @@ -386,10 +386,12 @@ void chart2d_impl::render(const int pWindowId, float lgap = mLeftMargin + mTickSize/2; float bgap = mBottomMargin + mTickSize/2; - float w = pVPW - (lgap + mRightMargin + mTickSize/2); - float h = pVPH - (bgap + mTopMargin + mTickSize/2); + float offset_x = (lgap-mRightMargin) / pVPW; float offset_y = (bgap-mTopMargin) / pVPH; + + float w = pVPW - (lgap + mRightMargin); + float h = pVPH - (bgap + mTopMargin); float scale_x = w / pVPW; float scale_y = h / pVPH; @@ -409,7 +411,7 @@ void chart2d_impl::render(const int pWindowId, chart2d_impl::unbindResources(); glEnable(GL_SCISSOR_TEST); - glScissor(pX+mLeftMargin, pY+mBottomMargin, w, h); + glScissor(pX+mLeftMargin, pY+mBottomMargin+mTickSize/2, w, h); /* render all renderables */ for (auto renderable : mRenderables) { renderable->setRanges(mXMin, mXMax, mYMin, mYMax, mZMin, mZMax); From c5111da66f7acdb79725e2b84c24b3191173904b Mon Sep 17 00:00:00 2001 From: pradeep Date: Wed, 29 Jun 2016 18:19:15 +0530 Subject: [PATCH 11/16] Add support for 3d vector fields Orientation of 2d and 3d fields is not per expection due a bug, working on it now. Other than that, everything else related to fields seems to be working fine now. --- examples/cpu/stream.cpp | 123 ++++++++++++++++++ src/backend/chart_renderables.hpp | 6 +- src/backend/opengl/chart_impl.cpp | 2 +- .../opengl/shaders/vector_field_gs.glsl | 109 ++++++---------- src/backend/opengl/vector_field_impl.cpp | 4 + 5 files changed, 170 insertions(+), 74 deletions(-) create mode 100644 examples/cpu/stream.cpp diff --git a/examples/cpu/stream.cpp b/examples/cpu/stream.cpp new file mode 100644 index 00000000..8ea6fb96 --- /dev/null +++ b/examples/cpu/stream.cpp @@ -0,0 +1,123 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#include +#include +#include +#include +#include +#include + +const unsigned DIMX = 640; +const unsigned DIMY = 480; +const float PI = 3.14159265359; +const float MINIMUM = 1.0f; +const float MAXIMUM = 20.f; +const float STEP = 2.0f; +const int NELEMS = (MAXIMUM-MINIMUM+1)/STEP; + +using namespace std; + +void generateColors(std::vector& colors) +{ + static const float AF_BLUE[] = {0.0588f , 0.1137f , 0.2745f , 1.0f}; + static const float AF_ORANGE[] = {0.8588f , 0.6137f , 0.0745f , 1.0f}; + + int numElems = NELEMS*NELEMS*NELEMS; + colors.clear(); + for (int i=0; i& colors) +{ + int end = colors.size()-3; + float r = colors[end+2]; + float g = colors[end+1]; + float b = colors[end]; + + for (int i=0; i &points, std::vector &dirs) +{ + points.clear(); + + for (int k=0; k points; + std::vector colors; + std::vector dirs; + generatePoints(points, dirs); + generateColors(colors); + + fg::copy(field.vertices(), field.verticesSize(), (const void*)points.data()); + fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); + fg::copy(field.directions(), field.directionsSize(), (const void*)dirs.data()); + + do { + moveColors(colors); + fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); + wnd.draw(chart); + } while(!wnd.close()); + + return 0; +} diff --git a/src/backend/chart_renderables.hpp b/src/backend/chart_renderables.hpp index b0131f6f..6e635642 100644 --- a/src/backend/chart_renderables.hpp +++ b/src/backend/chart_renderables.hpp @@ -97,11 +97,7 @@ class VectorField : public ChartRenderableBase { if (pChartType == FG_CHART_2D) { mShrdPtr = std::make_shared< detail::vector_field2d_impl >(pNumPoints, pDataType); } else { - throw fg::Error(__PRETTY_FUNCTION__, __LINE__, - "3d fields not yet supported", - FG_ERR_NOT_SUPPORTED); - // FIXME Uncomment it once 3d fields are working properly - //mShrdPtr = std::make_shared< detail::vector_field_impl >(pNumPoints, pDataType); + mShrdPtr = std::make_shared< detail::vector_field_impl >(pNumPoints, pDataType); } } diff --git a/src/backend/opengl/chart_impl.cpp b/src/backend/opengl/chart_impl.cpp index 366684f1..9fee0ee3 100644 --- a/src/backend/opengl/chart_impl.cpp +++ b/src/backend/opengl/chart_impl.cpp @@ -718,7 +718,7 @@ void chart3d_impl::render(const int pWindowId, static const glm::mat4 VIEW = glm::lookAt(glm::vec3(-1.f,0.5f, 1.f), glm::vec3( 1.f,-1.f,-1.f), glm::vec3( 0.f, 1.f, 0.f)); - static const glm::mat4 PROJECTION = glm::ortho(-1.75f, 1.75f, -1.75f, 1.75f, -0.001f, 100.f); + static const glm::mat4 PROJECTION = glm::ortho(-1.75f, 1.75f, -1.75f, 1.75f, -0.001f, 1000.f); static const glm::mat4 MODEL = glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(0,1,0)) * glm::rotate(glm::mat4(1.0f), -glm::radians(90.f), glm::vec3(1,0,0)); static const glm::mat4 PV = PROJECTION * VIEW; diff --git a/src/backend/opengl/shaders/vector_field_gs.glsl b/src/backend/opengl/shaders/vector_field_gs.glsl index 48335d5d..107bbf0e 100644 --- a/src/backend/opengl/shaders/vector_field_gs.glsl +++ b/src/backend/opengl/shaders/vector_field_gs.glsl @@ -1,6 +1,6 @@ #version 330 layout(points) in; -layout(triangle_strip, max_vertices = 25) out; +layout(triangle_strip, max_vertices = 32) out; uniform mat4 arrowScaleMat; uniform mat4 viewMat; @@ -34,6 +34,15 @@ mat4 rotationMatrix(vec3 axis, float angle) 0.0, 0.0, 0.0, 1.0); } +void emitQuad(vec4 a, vec4 b, vec4 c, vec4 d) +{ + gl_Position = a; EmitVertex(); + gl_Position = b; EmitVertex(); + gl_Position = c; EmitVertex(); + gl_Position = d; EmitVertex(); + EndPrimitive(); +} + void main() { vec4 pos = gl_in[0].gl_Position; @@ -47,77 +56,41 @@ void main() mat4 arot = rotationMatrix(sndAxs.xyz, theta); mat4 trans = arrowScaleMat * arot * zrot; - vec4 origin = viewMat * (pos + trans * vec4(0,0,0,1)); - vec4 ltop = viewMat * (pos + trans * vec4(-0.333, 0.333, 0.333, 1.0)); - vec4 lbot = viewMat * (pos + trans * vec4(-0.333,-0.333, 0.333, 1.0)); - vec4 rtop = viewMat * (pos + trans * vec4(-0.333,-0.333,-0.333, 1.0)); - vec4 rbot = viewMat * (pos + trans * vec4(-0.333, 0.333,-0.333, 1.0)); + vec4 t = viewMat * (pos + trans*vec4( 0.000, 0.300, 0.000, 1.000)); - vec4 iltop = viewMat * (pos + trans * vec4(-0.333, 0.167, 0.167, 1.0)); - vec4 ilbot = viewMat * (pos + trans * vec4(-0.333,-0.167, 0.167, 1.0)); - vec4 irtop = viewMat * (pos + trans * vec4(-0.333,-0.167,-0.167, 1.0)); - vec4 irbot = viewMat * (pos + trans * vec4(-0.333, 0.167,-0.167, 1.0)); + vec4 a = viewMat * (pos + trans*vec4(-0.167, -1.000, -0.167, 1.000)); + vec4 b = viewMat * (pos + trans*vec4( 0.167, -1.000, -0.167, 1.000)); + vec4 c = viewMat * (pos + trans*vec4(-0.167, -1.000, 0.167, 1.000)); + vec4 d = viewMat * (pos + trans*vec4( 0.167, -1.000, 0.167, 1.000)); - vec4 iltop2 = viewMat * (pos + trans * vec4(-1.000, 0.167, 0.167, 1.0)); - vec4 ilbot2 = viewMat * (pos + trans * vec4(-1.000,-0.167, 0.167, 1.0)); - vec4 irtop2 = viewMat * (pos + trans * vec4(-1.000,-0.167,-0.167, 1.0)); - vec4 irbot2 = viewMat * (pos + trans * vec4(-1.000, 0.167,-0.167, 1.0)); + vec4 i0 = viewMat * (pos + trans*vec4(-0.167, -0.333, -0.167, 1.000)); + vec4 i1 = viewMat * (pos + trans*vec4( 0.167, -0.333, -0.167, 1.000)); + vec4 i2 = viewMat * (pos + trans*vec4(-0.167, -0.333, 0.167, 1.000)); + vec4 i3 = viewMat * (pos + trans*vec4( 0.167, -0.333, 0.167, 1.000)); + + vec4 e = viewMat * (pos + trans*vec4(-0.333, -0.333, -0.333, 1.000)); + vec4 f = viewMat * (pos + trans*vec4( 0.333, -0.333, -0.333, 1.000)); + vec4 g = viewMat * (pos + trans*vec4(-0.333, -0.333, 0.333, 1.000)); + vec4 h = viewMat * (pos + trans*vec4( 0.333, -0.333, 0.333, 1.000)); pervcol = gs_in[0].color; - // Roof top - gl_Position = ltop; - EmitVertex(); - gl_Position = origin; - EmitVertex(); - gl_Position = lbot; - EmitVertex(); - gl_Position = rbot; - EmitVertex(); - gl_Position = origin; - EmitVertex(); - gl_Position = rtop; - EmitVertex(); - gl_Position = ltop; - EmitVertex(); - // over the edge areas of roof - gl_Position = iltop; - EmitVertex(); - gl_Position = lbot; - EmitVertex(); - gl_Position = ilbot; - EmitVertex(); - gl_Position = rbot; - EmitVertex(); - gl_Position = irbot; - EmitVertex(); - gl_Position = rtop; - EmitVertex(); - gl_Position = irtop; - EmitVertex(); - gl_Position = iltop; - EmitVertex(); - // Pillar - gl_Position = iltop2; - EmitVertex(); - gl_Position = ilbot; - EmitVertex(); - gl_Position = ilbot2; - EmitVertex(); - gl_Position = irbot; - EmitVertex(); - gl_Position = irbot2; - EmitVertex(); - gl_Position = irtop; - EmitVertex(); - gl_Position = irtop2; - EmitVertex(); - gl_Position = iltop2; - EmitVertex(); - // Pillar base - gl_Position = irbot2; - EmitVertex(); - gl_Position = ilbot2; - EmitVertex(); + emitQuad(a, b, c, d); + emitQuad(a, i0, b, i1); + emitQuad(b, i1, d, i3); + emitQuad(c, d, i2, i3); + emitQuad(a, c, i0, i2); + emitQuad(e, f, g, h); + + gl_Position = g; EmitVertex(); + gl_Position = h; EmitVertex(); + gl_Position = t; EmitVertex(); + gl_Position = f; EmitVertex(); + gl_Position = e; EmitVertex(); + EndPrimitive(); + + gl_Position = t; EmitVertex(); + gl_Position = e; EmitVertex(); + gl_Position = g; EmitVertex(); EndPrimitive(); } diff --git a/src/backend/opengl/vector_field_impl.cpp b/src/backend/opengl/vector_field_impl.cpp index 5cf96895..ab65ee83 100644 --- a/src/backend/opengl/vector_field_impl.cpp +++ b/src/backend/opengl/vector_field_impl.cpp @@ -202,9 +202,13 @@ void vector_field_impl::render(const int pWindowId, glUniform1i(mFieldPVAOnIndex, mIsPVAOn); glUniform4fv(mFieldUColorIndex, 1, mColor); + if (mDimension==3) + glEnable(GL_CULL_FACE); vector_field_impl::bindResources(pWindowId); glDrawArrays(GL_POINTS, 0, mNumPoints); vector_field_impl::unbindResources(); + if (mDimension==3) + glDisable(GL_CULL_FACE); glUseProgram(0); From 93b2919fd8ae56328e769e8d94f4cd626ed9d880 Mon Sep 17 00:00:00 2001 From: pradeep Date: Wed, 29 Jun 2016 21:57:25 +0530 Subject: [PATCH 12/16] Fix 2d vector field elements orientation --- examples/cpu/field.cpp | 9 ++++++++- src/backend/opengl/shaders/vector_field2d_gs.glsl | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/examples/cpu/field.cpp b/examples/cpu/field.cpp index 2e1d35ce..6246fc3a 100644 --- a/examples/cpu/field.cpp +++ b/examples/cpu/field.cpp @@ -21,6 +21,7 @@ const float MINIMUM = 1.0f; const float MAXIMUM = 20.f; const float STEP = 2.0f; const float NELEMS = (MAXIMUM-MINIMUM+1)/STEP; +const unsigned DPOINTS[] = {5, 5, 5, 15, 15, 5, 15, 15}; using namespace std; @@ -54,17 +55,23 @@ int main(void) chart.setAxesLimits(MINIMUM-1.0f, MAXIMUM, MINIMUM-1.0f, MAXIMUM); chart.setAxesTitles("x-axis", "y-axis"); + fg::Plot divPoints = chart.plot(4, fg::u32, FG_PLOT_SCATTER, FG_MARKER_CIRCLE); + divPoints.setColor(0.9f, 0.9f, 0.0f, 1.f); + divPoints.setLegend("Convergence Points"); + divPoints.setMarkerSize(24); + fg::VectorField field = chart.vectorField(NELEMS*NELEMS, fg::f32); field.setColor(0.f, 0.6f, 0.3f, 1.f); std::vector points; - std::vector colors; std::vector dirs; generatePoints(points, dirs); fg::copy(field.vertices(), field.verticesSize(), (const void*)points.data()); fg::copy(field.directions(), field.directionsSize(), (const void*)dirs.data()); + fg::copy(divPoints.vertices(), divPoints.verticesSize(), (const void*)DPOINTS); + do { wnd.draw(chart); } while(!wnd.close()); diff --git a/src/backend/opengl/shaders/vector_field2d_gs.glsl b/src/backend/opengl/shaders/vector_field2d_gs.glsl index 85ae8674..b792ee8d 100644 --- a/src/backend/opengl/shaders/vector_field2d_gs.glsl +++ b/src/backend/opengl/shaders/vector_field2d_gs.glsl @@ -2,6 +2,8 @@ layout(points) in; layout(triangle_strip, max_vertices = 9) out; +const float PiBy2 = 1.57079632679; + uniform mat4 arrowScaleMat; uniform mat4 viewMat; @@ -27,7 +29,7 @@ void main() { vec2 dir = normalize(gs_in[0].dir); - mat4 arrowModelMat = arrowScaleMat * zrotate(atan(dir.y, dir.x)); + mat4 arrowModelMat = arrowScaleMat * zrotate(PiBy2 - atan(dir.y, dir.x)); vec4 pos = gl_in[0].gl_Position; From bfc17e891f7ccb31de23e9a2f782910a456e77b8 Mon Sep 17 00:00:00 2001 From: pradeep Date: Thu, 30 Jun 2016 13:39:36 +0530 Subject: [PATCH 13/16] Fix 3d vector field elements orientation --- examples/cpu/stream.cpp | 8 ++++---- src/backend/opengl/shaders/vector_field_gs.glsl | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/cpu/stream.cpp b/examples/cpu/stream.cpp index 8ea6fb96..29d228ee 100644 --- a/examples/cpu/stream.cpp +++ b/examples/cpu/stream.cpp @@ -19,7 +19,7 @@ const unsigned DIMY = 480; const float PI = 3.14159265359; const float MINIMUM = 1.0f; const float MAXIMUM = 20.f; -const float STEP = 2.0f; +const float STEP = 4.0f; const int NELEMS = (MAXIMUM-MINIMUM+1)/STEP; using namespace std; @@ -75,9 +75,9 @@ void generatePoints(std::vector &points, std::vector &dirs) points.push_back(x); points.push_back(y); points.push_back(z); - dirs.push_back(10.0f-x); - dirs.push_back(10.0f-y); - dirs.push_back(10.0f-z); + dirs.push_back(x-10.0f); + dirs.push_back(y-10.0f); + dirs.push_back(z-10.0f); } } } diff --git a/src/backend/opengl/shaders/vector_field_gs.glsl b/src/backend/opengl/shaders/vector_field_gs.glsl index 107bbf0e..6d66b918 100644 --- a/src/backend/opengl/shaders/vector_field_gs.glsl +++ b/src/backend/opengl/shaders/vector_field_gs.glsl @@ -2,6 +2,8 @@ layout(points) in; layout(triangle_strip, max_vertices = 32) out; +const float PiBy2 = 1.57079632679; + uniform mat4 arrowScaleMat; uniform mat4 viewMat; @@ -48,13 +50,12 @@ void main() vec4 pos = gl_in[0].gl_Position; vec3 dir = normalize(gs_in[0].dir); - float theta = acos(dir.z/ 1.0); - float phi = atan(dir.y/ dir.x); + vec3 originalAxis = vec3(0, 1, 0); + vec3 planeNormal = cross(originalAxis, dir); + float angle = acos(dot(originalAxis, dir)); + mat4 rot = rotationMatrix(planeNormal, angle); - mat4 zrot = rotationMatrix(vec3(0,0,1), phi); - vec4 sndAxs = zrot * vec4(0,1,0,1); - mat4 arot = rotationMatrix(sndAxs.xyz, theta); - mat4 trans = arrowScaleMat * arot * zrot; + mat4 trans = arrowScaleMat * rot; vec4 t = viewMat * (pos + trans*vec4( 0.000, 0.300, 0.000, 1.000)); From d99edb6734d60d4746cebe0e1475f2051417aa69 Mon Sep 17 00:00:00 2001 From: pradeep Date: Thu, 30 Jun 2016 15:31:06 +0530 Subject: [PATCH 14/16] Replace cl.hpp header with cl2.hpp as external cmake project --- CMakeModules/build_cl2hpp.cmake | 29 + examples/CMakeLists.txt | 7 + examples/opencl/cl.hpp | 12452 ------------------------------ examples/opencl/cl_helpers.h | 14 +- examples/opencl/fractal.cpp | 7 +- examples/opencl/histogram.cpp | 5 +- examples/opencl/plot3.cpp | 5 +- examples/opencl/plotting.cpp | 5 +- examples/opencl/surface.cpp | 5 +- include/OpenCLCopy.hpp | 5 + 10 files changed, 64 insertions(+), 12470 deletions(-) create mode 100644 CMakeModules/build_cl2hpp.cmake delete mode 100644 examples/opencl/cl.hpp diff --git a/CMakeModules/build_cl2hpp.cmake b/CMakeModules/build_cl2hpp.cmake new file mode 100644 index 00000000..bd834325 --- /dev/null +++ b/CMakeModules/build_cl2hpp.cmake @@ -0,0 +1,29 @@ +INCLUDE(ExternalProject) + +SET(prefix ${CMAKE_BINARY_DIR}/third_party/cl2hpp) + +ExternalProject_Add( + cl2hpp-ext + GIT_REPOSITORY https://github.com/KhronosGroup/OpenCL-CLHPP.git + GIT_TAG 75bb7d0d8b2ffc6aac0a3dcaa22f6622cab81f7c + PREFIX "${prefix}" + INSTALL_DIR "${prefix}/package" + UPDATE_COMMAND "" + CONFIGURE_COMMAND ${CMAKE_COMMAND} -Wno-dev "-G${CMAKE_GENERATOR}" + -DCMAKE_SOURCE_DIR:PATH= + -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} + -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} + -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} + -DCMAKE_INSTALL_PREFIX:PATH= + -DBUILD_DOCS:BOOL=OFF + -DBUILD_EXAMPLES:BOOL=OFF + -DBUILD_TESTS:BOOL=OFF + ) + +ExternalProject_Get_Property(cl2hpp-ext install_dir) + +ADD_CUSTOM_TARGET(cl2hpp DEPENDS "${prefix}/package/CL/cl2.hpp") + +ADD_DEPENDENCIES(cl2hpp cl2hpp-ext) + +SET(CL2HPP_INCLUDE_DIRECTORY ${install_dir}) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b438d6f0..524ddc8b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,6 +5,9 @@ SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules") OPTION(BUILD_EXAMPLES_CUDA "Turn off/on building cuda examples" ON) OPTION(BUILD_EXAMPLES_OPENCL "Turn off/on building opencl examples" ON) +OPTION(USE_SYSTEM_CL2HPP "Use cl2.hpp header installed on the system" OFF) + +INCLUDE(build_cl2hpp) MACRO(BUILD_EXAMPLE EX_NAME EX_SRC COMPUTE_NAME FG_LIBS COMPUTE_LIBS) IF(${COMPUTE_NAME} STREQUAL "cuda") @@ -18,6 +21,9 @@ MACRO(BUILD_EXAMPLE EX_NAME EX_SRC COMPUTE_NAME FG_LIBS COMPUTE_LIBS) OUTPUT_NAME ${EX_NAME}_${COMPUTE_NAME} RUNTIME_OUTPUT_DIRECTORY ${DIR_NAME} FOLDER "Examples/${COMPUTE_NAME}") + IF(${COMPUTE_NAME} STREQUAL "opencl") + ADD_DEPENDENCIES(example_${EX_NAME}_${COMPUTE_NAME} cl2hpp) + ENDIF() ENDMACRO() IF(TARGET forge) @@ -128,6 +134,7 @@ IF (BUILD_EXAMPLES_OPENCL) INCLUDE_DIRECTORIES( "${CMAKE_CURRENT_SOURCE_DIR}/opencl" ${OpenCL_INCLUDE_DIRS} + ${CL2HPP_INCLUDE_DIRECTORY} ) FOREACH(FILE ${OpenCL_EXAMPLE_SRC_FILES}) GET_FILENAME_COMPONENT(EXAMPLE ${FILE} NAME_WE) diff --git a/examples/opencl/cl.hpp b/examples/opencl/cl.hpp deleted file mode 100644 index bd7caf50..00000000 --- a/examples/opencl/cl.hpp +++ /dev/null @@ -1,12452 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008-2013 The Khronos Group Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and/or associated documentation files (the - * "Materials"), to deal in the Materials without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Materials, and to - * permit persons to whom the Materials are furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Materials. - * - * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. - ******************************************************************************/ - -/*! \file - * - * \brief C++ bindings for OpenCL 1.0 (rev 48), OpenCL 1.1 (rev 33) and - * OpenCL 1.2 (rev 15) - * \author Benedict R. Gaster, Laurent Morichetti and Lee Howes - * - * Additions and fixes from: - * Brian Cole, March 3rd 2010 and April 2012 - * Matt Gruenke, April 2012. - * Bruce Merry, February 2013. - * Tom Deakin and Simon McIntosh-Smith, July 2013 - * - * \version 1.2.6 - * \date August 2013 - * - * Optional extension support - * - * cl - * cl_ext_device_fission - * #define USE_CL_DEVICE_FISSION - */ - -/*! \mainpage - * \section intro Introduction - * For many large applications C++ is the language of choice and so it seems - * reasonable to define C++ bindings for OpenCL. - * - * - * The interface is contained with a single C++ header file \em cl.hpp and all - * definitions are contained within the namespace \em cl. There is no additional - * requirement to include \em cl.h and to use either the C++ or original C - * bindings it is enough to simply include \em cl.hpp. - * - * The bindings themselves are lightweight and correspond closely to the - * underlying C API. Using the C++ bindings introduces no additional execution - * overhead. - * - * For detail documentation on the bindings see: - * - * The OpenCL C++ Wrapper API 1.2 (revision 09) - * http://www.khronos.org/registry/cl/specs/opencl-cplusplus-1.2.pdf - * - * \section example Example - * - * The following example shows a general use case for the C++ - * bindings, including support for the optional exception feature and - * also the supplied vector and string classes, see following sections for - * decriptions of these features. - * - * \code - * #define __CL_ENABLE_EXCEPTIONS - * - * #if defined(__APPLE__) || defined(__MACOSX) - * #include - * #else - * #include - * #endif - * #include - * #include - * #include - * - * const char * helloStr = "__kernel void " - * "hello(void) " - * "{ " - * " " - * "} "; - * - * int - * main(void) - * { - * cl_int err = CL_SUCCESS; - * try { - * - * std::vector platforms; - * cl::Platform::get(&platforms); - * if (platforms.size() == 0) { - * std::cout << "Platform size 0\n"; - * return -1; - * } - * - * cl_context_properties properties[] = - * { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0}; - * cl::Context context(CL_DEVICE_TYPE_CPU, properties); - * - * std::vector devices = context.getInfo(); - * - * cl::Program::Sources source(1, - * std::make_pair(helloStr,strlen(helloStr))); - * cl::Program program_ = cl::Program(context, source); - * program_.build(devices); - * - * cl::Kernel kernel(program_, "hello", &err); - * - * cl::Event event; - * cl::CommandQueue queue(context, devices[0], 0, &err); - * queue.enqueueNDRangeKernel( - * kernel, - * cl::NullRange, - * cl::NDRange(4,4), - * cl::NullRange, - * NULL, - * &event); - * - * event.wait(); - * } - * catch (cl::Error err) { - * std::cerr - * << "ERROR: " - * << err.what() - * << "(" - * << err.err() - * << ")" - * << std::endl; - * } - * - * return EXIT_SUCCESS; - * } - * - * \endcode - * - */ -#ifndef CL_HPP_ -#define CL_HPP_ - -#ifdef _WIN32 - -#include -#include -#include -#include - -#if defined(__CL_ENABLE_EXCEPTIONS) -#include -#endif // #if defined(__CL_ENABLE_EXCEPTIONS) - -#pragma push_macro("max") -#undef max -#if defined(USE_DX_INTEROP) -#include -#include -#endif -#endif // _WIN32 - -// -#if defined(USE_CL_DEVICE_FISSION) -#include -#endif - -#if defined(__APPLE__) || defined(__MACOSX) -#include -#include -#include -#else -#include -#include -#endif // !__APPLE__ - -// To avoid accidentally taking ownership of core OpenCL types -// such as cl_kernel constructors are made explicit -// under OpenCL 1.2 -#if defined(CL_VERSION_1_2) && !defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) -#define __CL_EXPLICIT_CONSTRUCTORS explicit -#else // #if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) -#define __CL_EXPLICIT_CONSTRUCTORS -#endif // #if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - -// Define deprecated prefixes and suffixes to ensure compilation -// in case they are not pre-defined -#if !defined(CL_EXT_PREFIX__VERSION_1_1_DEPRECATED) -#define CL_EXT_PREFIX__VERSION_1_1_DEPRECATED -#endif // #if !defined(CL_EXT_PREFIX__VERSION_1_1_DEPRECATED) -#if !defined(CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED) -#define CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED -#endif // #if !defined(CL_EXT_PREFIX__VERSION_1_1_DEPRECATED) - -#if !defined(CL_CALLBACK) -#define CL_CALLBACK -#endif //CL_CALLBACK - -#include -#include - -#if !defined(__NO_STD_VECTOR) -#include -#endif - -#if !defined(__NO_STD_STRING) -#include -#endif - -#if defined(linux) || defined(__APPLE__) || defined(__MACOSX) -#include - -#include -#include -#endif // linux - -#include - - -/*! \namespace cl - * - * \brief The OpenCL C++ bindings are defined within this namespace. - * - */ -namespace cl { - -class Memory; - -/** - * Deprecated APIs for 1.2 - */ -#if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) || (defined(CL_VERSION_1_1) && !defined(CL_VERSION_1_2)) -#define __INIT_CL_EXT_FCN_PTR(name) \ - if(!pfn_##name) { \ - pfn_##name = (PFN_##name) \ - clGetExtensionFunctionAddress(#name); \ - if(!pfn_##name) { \ - } \ - } -#endif // #if defined(CL_VERSION_1_1) - -#if defined(CL_VERSION_1_2) -#define __INIT_CL_EXT_FCN_PTR_PLATFORM(platform, name) \ - if(!pfn_##name) { \ - pfn_##name = (PFN_##name) \ - clGetExtensionFunctionAddressForPlatform(platform, #name); \ - if(!pfn_##name) { \ - } \ - } -#endif // #if defined(CL_VERSION_1_1) - -class Program; -class Device; -class Context; -class CommandQueue; -class Memory; -class Buffer; - -#if defined(__CL_ENABLE_EXCEPTIONS) -/*! \brief Exception class - * - * This may be thrown by API functions when __CL_ENABLE_EXCEPTIONS is defined. - */ -class Error : public std::exception -{ -private: - cl_int err_; - const char * errStr_; -public: - /*! \brief Create a new CL error exception for a given error code - * and corresponding message. - * - * \param err error code value. - * - * \param errStr a descriptive string that must remain in scope until - * handling of the exception has concluded. If set, it - * will be returned by what(). - */ - Error(cl_int err, const char * errStr = NULL) : err_(err), errStr_(errStr) - {} - - ~Error() throw() {} - - /*! \brief Get error string associated with exception - * - * \return A memory pointer to the error message string. - */ - virtual const char * what() const throw () - { - if (errStr_ == NULL) { - return "empty"; - } - else { - return errStr_; - } - } - - /*! \brief Get error code associated with exception - * - * \return The error code. - */ - cl_int err(void) const { return err_; } -}; - -#define __ERR_STR(x) #x -#else -#define __ERR_STR(x) NULL -#endif // __CL_ENABLE_EXCEPTIONS - - -namespace detail -{ -#if defined(__CL_ENABLE_EXCEPTIONS) -static inline cl_int errHandler ( - cl_int err, - const char * errStr = NULL) -{ - if (err != CL_SUCCESS) { - throw Error(err, errStr); - } - return err; -} -#else -static inline cl_int errHandler (cl_int err, const char * errStr = NULL) -{ - (void) errStr; // suppress unused variable warning - return err; -} -#endif // __CL_ENABLE_EXCEPTIONS -} - - - -//! \cond DOXYGEN_DETAIL -#if !defined(__CL_USER_OVERRIDE_ERROR_STRINGS) -#define __GET_DEVICE_INFO_ERR __ERR_STR(clGetDeviceInfo) -#define __GET_PLATFORM_INFO_ERR __ERR_STR(clGetPlatformInfo) -#define __GET_DEVICE_IDS_ERR __ERR_STR(clGetDeviceIDs) -#define __GET_PLATFORM_IDS_ERR __ERR_STR(clGetPlatformIDs) -#define __GET_CONTEXT_INFO_ERR __ERR_STR(clGetContextInfo) -#define __GET_EVENT_INFO_ERR __ERR_STR(clGetEventInfo) -#define __GET_EVENT_PROFILE_INFO_ERR __ERR_STR(clGetEventProfileInfo) -#define __GET_MEM_OBJECT_INFO_ERR __ERR_STR(clGetMemObjectInfo) -#define __GET_IMAGE_INFO_ERR __ERR_STR(clGetImageInfo) -#define __GET_SAMPLER_INFO_ERR __ERR_STR(clGetSamplerInfo) -#define __GET_KERNEL_INFO_ERR __ERR_STR(clGetKernelInfo) -#if defined(CL_VERSION_1_2) -#define __GET_KERNEL_ARG_INFO_ERR __ERR_STR(clGetKernelArgInfo) -#endif // #if defined(CL_VERSION_1_2) -#define __GET_KERNEL_WORK_GROUP_INFO_ERR __ERR_STR(clGetKernelWorkGroupInfo) -#define __GET_PROGRAM_INFO_ERR __ERR_STR(clGetProgramInfo) -#define __GET_PROGRAM_BUILD_INFO_ERR __ERR_STR(clGetProgramBuildInfo) -#define __GET_COMMAND_QUEUE_INFO_ERR __ERR_STR(clGetCommandQueueInfo) - -#define __CREATE_CONTEXT_ERR __ERR_STR(clCreateContext) -#define __CREATE_CONTEXT_FROM_TYPE_ERR __ERR_STR(clCreateContextFromType) -#define __GET_SUPPORTED_IMAGE_FORMATS_ERR __ERR_STR(clGetSupportedImageFormats) - -#define __CREATE_BUFFER_ERR __ERR_STR(clCreateBuffer) -#define __COPY_ERR __ERR_STR(cl::copy) -#define __CREATE_SUBBUFFER_ERR __ERR_STR(clCreateSubBuffer) -#define __CREATE_GL_BUFFER_ERR __ERR_STR(clCreateFromGLBuffer) -#define __CREATE_GL_RENDER_BUFFER_ERR __ERR_STR(clCreateFromGLBuffer) -#define __GET_GL_OBJECT_INFO_ERR __ERR_STR(clGetGLObjectInfo) -#if defined(CL_VERSION_1_2) -#define __CREATE_IMAGE_ERR __ERR_STR(clCreateImage) -#define __CREATE_GL_TEXTURE_ERR __ERR_STR(clCreateFromGLTexture) -#define __IMAGE_DIMENSION_ERR __ERR_STR(Incorrect image dimensions) -#endif // #if defined(CL_VERSION_1_2) -#define __CREATE_SAMPLER_ERR __ERR_STR(clCreateSampler) -#define __SET_MEM_OBJECT_DESTRUCTOR_CALLBACK_ERR __ERR_STR(clSetMemObjectDestructorCallback) - -#define __CREATE_USER_EVENT_ERR __ERR_STR(clCreateUserEvent) -#define __SET_USER_EVENT_STATUS_ERR __ERR_STR(clSetUserEventStatus) -#define __SET_EVENT_CALLBACK_ERR __ERR_STR(clSetEventCallback) -#define __WAIT_FOR_EVENTS_ERR __ERR_STR(clWaitForEvents) - -#define __CREATE_KERNEL_ERR __ERR_STR(clCreateKernel) -#define __SET_KERNEL_ARGS_ERR __ERR_STR(clSetKernelArg) -#define __CREATE_PROGRAM_WITH_SOURCE_ERR __ERR_STR(clCreateProgramWithSource) -#define __CREATE_PROGRAM_WITH_BINARY_ERR __ERR_STR(clCreateProgramWithBinary) -#if defined(CL_VERSION_1_2) -#define __CREATE_PROGRAM_WITH_BUILT_IN_KERNELS_ERR __ERR_STR(clCreateProgramWithBuiltInKernels) -#endif // #if defined(CL_VERSION_1_2) -#define __BUILD_PROGRAM_ERR __ERR_STR(clBuildProgram) -#if defined(CL_VERSION_1_2) -#define __COMPILE_PROGRAM_ERR __ERR_STR(clCompileProgram) - -#endif // #if defined(CL_VERSION_1_2) -#define __CREATE_KERNELS_IN_PROGRAM_ERR __ERR_STR(clCreateKernelsInProgram) - -#define __CREATE_COMMAND_QUEUE_ERR __ERR_STR(clCreateCommandQueue) -#define __SET_COMMAND_QUEUE_PROPERTY_ERR __ERR_STR(clSetCommandQueueProperty) -#define __ENQUEUE_READ_BUFFER_ERR __ERR_STR(clEnqueueReadBuffer) -#define __ENQUEUE_READ_BUFFER_RECT_ERR __ERR_STR(clEnqueueReadBufferRect) -#define __ENQUEUE_WRITE_BUFFER_ERR __ERR_STR(clEnqueueWriteBuffer) -#define __ENQUEUE_WRITE_BUFFER_RECT_ERR __ERR_STR(clEnqueueWriteBufferRect) -#define __ENQEUE_COPY_BUFFER_ERR __ERR_STR(clEnqueueCopyBuffer) -#define __ENQEUE_COPY_BUFFER_RECT_ERR __ERR_STR(clEnqueueCopyBufferRect) -#define __ENQUEUE_FILL_BUFFER_ERR __ERR_STR(clEnqueueFillBuffer) -#define __ENQUEUE_READ_IMAGE_ERR __ERR_STR(clEnqueueReadImage) -#define __ENQUEUE_WRITE_IMAGE_ERR __ERR_STR(clEnqueueWriteImage) -#define __ENQUEUE_COPY_IMAGE_ERR __ERR_STR(clEnqueueCopyImage) -#define __ENQUEUE_FILL_IMAGE_ERR __ERR_STR(clEnqueueFillImage) -#define __ENQUEUE_COPY_IMAGE_TO_BUFFER_ERR __ERR_STR(clEnqueueCopyImageToBuffer) -#define __ENQUEUE_COPY_BUFFER_TO_IMAGE_ERR __ERR_STR(clEnqueueCopyBufferToImage) -#define __ENQUEUE_MAP_BUFFER_ERR __ERR_STR(clEnqueueMapBuffer) -#define __ENQUEUE_MAP_IMAGE_ERR __ERR_STR(clEnqueueMapImage) -#define __ENQUEUE_UNMAP_MEM_OBJECT_ERR __ERR_STR(clEnqueueUnMapMemObject) -#define __ENQUEUE_NDRANGE_KERNEL_ERR __ERR_STR(clEnqueueNDRangeKernel) -#define __ENQUEUE_TASK_ERR __ERR_STR(clEnqueueTask) -#define __ENQUEUE_NATIVE_KERNEL __ERR_STR(clEnqueueNativeKernel) -#if defined(CL_VERSION_1_2) -#define __ENQUEUE_MIGRATE_MEM_OBJECTS_ERR __ERR_STR(clEnqueueMigrateMemObjects) -#endif // #if defined(CL_VERSION_1_2) - -#define __ENQUEUE_ACQUIRE_GL_ERR __ERR_STR(clEnqueueAcquireGLObjects) -#define __ENQUEUE_RELEASE_GL_ERR __ERR_STR(clEnqueueReleaseGLObjects) - - -#define __RETAIN_ERR __ERR_STR(Retain Object) -#define __RELEASE_ERR __ERR_STR(Release Object) -#define __FLUSH_ERR __ERR_STR(clFlush) -#define __FINISH_ERR __ERR_STR(clFinish) -#define __VECTOR_CAPACITY_ERR __ERR_STR(Vector capacity error) - -/** - * CL 1.2 version that uses device fission. - */ -#if defined(CL_VERSION_1_2) -#define __CREATE_SUB_DEVICES __ERR_STR(clCreateSubDevices) -#else -#define __CREATE_SUB_DEVICES __ERR_STR(clCreateSubDevicesEXT) -#endif // #if defined(CL_VERSION_1_2) - -/** - * Deprecated APIs for 1.2 - */ -#if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) || (defined(CL_VERSION_1_1) && !defined(CL_VERSION_1_2)) -#define __ENQUEUE_MARKER_ERR __ERR_STR(clEnqueueMarker) -#define __ENQUEUE_WAIT_FOR_EVENTS_ERR __ERR_STR(clEnqueueWaitForEvents) -#define __ENQUEUE_BARRIER_ERR __ERR_STR(clEnqueueBarrier) -#define __UNLOAD_COMPILER_ERR __ERR_STR(clUnloadCompiler) -#define __CREATE_GL_TEXTURE_2D_ERR __ERR_STR(clCreateFromGLTexture2D) -#define __CREATE_GL_TEXTURE_3D_ERR __ERR_STR(clCreateFromGLTexture3D) -#define __CREATE_IMAGE2D_ERR __ERR_STR(clCreateImage2D) -#define __CREATE_IMAGE3D_ERR __ERR_STR(clCreateImage3D) -#endif // #if defined(CL_VERSION_1_1) - -#endif // __CL_USER_OVERRIDE_ERROR_STRINGS -//! \endcond - -/** - * CL 1.2 marker and barrier commands - */ -#if defined(CL_VERSION_1_2) -#define __ENQUEUE_MARKER_WAIT_LIST_ERR __ERR_STR(clEnqueueMarkerWithWaitList) -#define __ENQUEUE_BARRIER_WAIT_LIST_ERR __ERR_STR(clEnqueueBarrierWithWaitList) -#endif // #if defined(CL_VERSION_1_2) - -#if !defined(__USE_DEV_STRING) && !defined(__NO_STD_STRING) -typedef std::string STRING_CLASS; -#elif !defined(__USE_DEV_STRING) - -/*! \class string - * \brief Simple string class, that provides a limited subset of std::string - * functionality but avoids many of the issues that come with that class. - - * \note Deprecated. Please use std::string as default or - * re-define the string class to match the std::string - * interface by defining STRING_CLASS - */ -class CL_EXT_PREFIX__VERSION_1_1_DEPRECATED string CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED -{ -private: - ::size_t size_; - char * str_; -public: - //! \brief Constructs an empty string, allocating no memory. - string(void) : size_(0), str_(NULL) - { - } - - /*! \brief Constructs a string populated from an arbitrary value of - * specified size. - * - * An extra '\0' is added, in case none was contained in str. - * - * \param str the initial value of the string instance. Note that '\0' - * characters receive no special treatment. If NULL, - * the string is left empty, with a size of 0. - * - * \param size the number of characters to copy from str. - */ - string(const char * str, ::size_t size) : - size_(size), - str_(NULL) - { - if( size > 0 ) { - str_ = new char[size_+1]; - if (str_ != NULL) { - memcpy(str_, str, size_ * sizeof(char)); - str_[size_] = '\0'; - } - else { - size_ = 0; - } - } - } - - /*! \brief Constructs a string populated from a null-terminated value. - * - * \param str the null-terminated initial value of the string instance. - * If NULL, the string is left empty, with a size of 0. - */ - string(const char * str) : - size_(0), - str_(NULL) - { - if( str ) { - size_= ::strlen(str); - } - if( size_ > 0 ) { - str_ = new char[size_ + 1]; - if (str_ != NULL) { - memcpy(str_, str, (size_ + 1) * sizeof(char)); - } - } - } - - void resize( ::size_t n ) - { - if( size_ == n ) { - return; - } - if (n == 0) { - if( str_ ) { - delete [] str_; - } - str_ = NULL; - size_ = 0; - } - else { - char *newString = new char[n + 1]; - int copySize = n; - if( size_ < n ) { - copySize = size_; - } - size_ = n; - - if(str_) { - memcpy(newString, str_, (copySize + 1) * sizeof(char)); - } - if( copySize < size_ ) { - memset(newString + copySize, 0, size_ - copySize); - } - newString[size_] = '\0'; - - delete [] str_; - str_ = newString; - } - } - - const char& operator[] ( ::size_t pos ) const - { - return str_[pos]; - } - - char& operator[] ( ::size_t pos ) - { - return str_[pos]; - } - - /*! \brief Copies the value of another string to this one. - * - * \param rhs the string to copy. - * - * \returns a reference to the modified instance. - */ - string& operator=(const string& rhs) - { - if (this == &rhs) { - return *this; - } - - if( str_ != NULL ) { - delete [] str_; - str_ = NULL; - size_ = 0; - } - - if (rhs.size_ == 0 || rhs.str_ == NULL) { - str_ = NULL; - size_ = 0; - } - else { - str_ = new char[rhs.size_ + 1]; - size_ = rhs.size_; - - if (str_ != NULL) { - memcpy(str_, rhs.str_, (size_ + 1) * sizeof(char)); - } - else { - size_ = 0; - } - } - - return *this; - } - - /*! \brief Constructs a string by copying the value of another instance. - * - * \param rhs the string to copy. - */ - string(const string& rhs) : - size_(0), - str_(NULL) - { - *this = rhs; - } - - //! \brief Destructor - frees memory used to hold the current value. - ~string() - { - delete[] str_; - str_ = NULL; - } - - //! \brief Queries the length of the string, excluding any added '\0's. - ::size_t size(void) const { return size_; } - - //! \brief Queries the length of the string, excluding any added '\0's. - ::size_t length(void) const { return size(); } - - /*! \brief Returns a pointer to the private copy held by this instance, - * or "" if empty/unset. - */ - const char * c_str(void) const { return (str_) ? str_ : "";} -}; -typedef cl::string STRING_CLASS; -#endif // #elif !defined(__USE_DEV_STRING) - -#if !defined(__USE_DEV_VECTOR) && !defined(__NO_STD_VECTOR) -#define VECTOR_CLASS std::vector -#elif !defined(__USE_DEV_VECTOR) -#define VECTOR_CLASS cl::vector - -#if !defined(__MAX_DEFAULT_VECTOR_SIZE) -#define __MAX_DEFAULT_VECTOR_SIZE 10 -#endif - -/*! \class vector - * \brief Fixed sized vector implementation that mirroring - * - * \note Deprecated. Please use std::vector as default or - * re-define the vector class to match the std::vector - * interface by defining VECTOR_CLASS - - * \note Not recommended for use with custom objects as - * current implementation will construct N elements - * - * std::vector functionality. - * \brief Fixed sized vector compatible with std::vector. - * - * \note - * This differs from std::vector<> not just in memory allocation, - * but also in terms of when members are constructed, destroyed, - * and assigned instead of being copy constructed. - * - * \param T type of element contained in the vector. - * - * \param N maximum size of the vector. - */ -template -class CL_EXT_PREFIX__VERSION_1_1_DEPRECATED vector CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED -{ -private: - T data_[N]; - unsigned int size_; - -public: - //! \brief Constructs an empty vector with no memory allocated. - vector() : - size_(static_cast(0)) - {} - - //! \brief Deallocates the vector's memory and destroys all of its elements. - ~vector() - { - clear(); - } - - //! \brief Returns the number of elements currently contained. - unsigned int size(void) const - { - return size_; - } - - /*! \brief Empties the vector of all elements. - * \note - * This does not deallocate memory but will invoke destructors - * on contained elements. - */ - void clear() - { - while(!empty()) { - pop_back(); - } - } - - /*! \brief Appends an element after the last valid element. - * Calling this on a vector that has reached capacity will throw an - * exception if exceptions are enabled. - */ - void push_back (const T& x) - { - if (size() < N) { - new (&data_[size_]) T(x); - size_++; - } else { - detail::errHandler(CL_MEM_OBJECT_ALLOCATION_FAILURE, __VECTOR_CAPACITY_ERR); - } - } - - /*! \brief Removes the last valid element from the vector. - * Calling this on an empty vector will throw an exception - * if exceptions are enabled. - */ - void pop_back(void) - { - if (size_ != 0) { - --size_; - data_[size_].~T(); - } else { - detail::errHandler(CL_MEM_OBJECT_ALLOCATION_FAILURE, __VECTOR_CAPACITY_ERR); - } - } - - /*! \brief Constructs with a value copied from another. - * - * \param vec the vector to copy. - */ - vector(const vector& vec) : - size_(vec.size_) - { - if (size_ != 0) { - assign(vec.begin(), vec.end()); - } - } - - /*! \brief Constructs with a specified number of initial elements. - * - * \param size number of initial elements. - * - * \param val value of initial elements. - */ - vector(unsigned int size, const T& val = T()) : - size_(0) - { - for (unsigned int i = 0; i < size; i++) { - push_back(val); - } - } - - /*! \brief Overwrites the current content with that copied from another - * instance. - * - * \param rhs vector to copy. - * - * \returns a reference to this. - */ - vector& operator=(const vector& rhs) - { - if (this == &rhs) { - return *this; - } - - if (rhs.size_ != 0) { - assign(rhs.begin(), rhs.end()); - } else { - clear(); - } - - return *this; - } - - /*! \brief Tests equality against another instance. - * - * \param vec the vector against which to compare. - */ - bool operator==(vector &vec) - { - if (size() != vec.size()) { - return false; - } - - for( unsigned int i = 0; i < size(); ++i ) { - if( operator[](i) != vec[i] ) { - return false; - } - } - return true; - } - - //! \brief Conversion operator to T*. - operator T* () { return data_; } - - //! \brief Conversion operator to const T*. - operator const T* () const { return data_; } - - //! \brief Tests whether this instance has any elements. - bool empty (void) const - { - return size_==0; - } - - //! \brief Returns the maximum number of elements this instance can hold. - unsigned int max_size (void) const - { - return N; - } - - //! \brief Returns the maximum number of elements this instance can hold. - unsigned int capacity () const - { - return N; - } - - /*! \brief Returns a reference to a given element. - * - * \param index which element to access. * - * \note - * The caller is responsible for ensuring index is >= 0 and < size(). - */ - T& operator[](int index) - { - return data_[index]; - } - - /*! \brief Returns a const reference to a given element. - * - * \param index which element to access. - * - * \note - * The caller is responsible for ensuring index is >= 0 and < size(). - */ - const T& operator[](int index) const - { - return data_[index]; - } - - /*! \brief Assigns elements of the vector based on a source iterator range. - * - * \param start Beginning iterator of source range - * \param end Enditerator of source range - * - * \note - * Will throw an exception if exceptions are enabled and size exceeded. - */ - template - void assign(I start, I end) - { - clear(); - while(start != end) { - push_back(*start); - start++; - } - } - - /*! \class iterator - * \brief Const iterator class for vectors - */ - class iterator - { - private: - const vector *vec_; - int index_; - - /** - * Internal iterator constructor to capture reference - * to the vector it iterates over rather than taking - * the vector by copy. - */ - iterator (const vector &vec, int index) : - vec_(&vec) - { - if( !vec.empty() ) { - index_ = index; - } else { - index_ = -1; - } - } - - public: - iterator(void) : - index_(-1), - vec_(NULL) - { - } - - iterator(const iterator& rhs) : - vec_(rhs.vec_), - index_(rhs.index_) - { - } - - ~iterator(void) {} - - static iterator begin(const cl::vector &vec) - { - iterator i(vec, 0); - - return i; - } - - static iterator end(const cl::vector &vec) - { - iterator i(vec, vec.size()); - - return i; - } - - bool operator==(iterator i) - { - return ((vec_ == i.vec_) && - (index_ == i.index_)); - } - - bool operator!=(iterator i) - { - return (!(*this==i)); - } - - iterator& operator++() - { - ++index_; - return *this; - } - - iterator operator++(int) - { - iterator retVal(*this); - ++index_; - return retVal; - } - - iterator& operator--() - { - --index_; - return *this; - } - - iterator operator--(int) - { - iterator retVal(*this); - --index_; - return retVal; - } - - const T& operator *() const - { - return (*vec_)[index_]; - } - }; - - iterator begin(void) - { - return iterator::begin(*this); - } - - iterator begin(void) const - { - return iterator::begin(*this); - } - - iterator end(void) - { - return iterator::end(*this); - } - - iterator end(void) const - { - return iterator::end(*this); - } - - T& front(void) - { - return data_[0]; - } - - T& back(void) - { - return data_[size_]; - } - - const T& front(void) const - { - return data_[0]; - } - - const T& back(void) const - { - return data_[size_-1]; - } -}; -#endif // #if !defined(__USE_DEV_VECTOR) && !defined(__NO_STD_VECTOR) - - - - - -namespace detail { -#define __DEFAULT_NOT_INITIALIZED 1 -#define __DEFAULT_BEING_INITIALIZED 2 -#define __DEFAULT_INITIALIZED 4 - - /* - * Compare and exchange primitives are needed for handling of defaults - */ - inline int compare_exchange(volatile int * dest, int exchange, int comparand) - { -#ifdef _WIN32 - return (int)(InterlockedCompareExchange( - (volatile long*)dest, - (long)exchange, - (long)comparand)); -#elif defined(__APPLE__) || defined(__MACOSX) - return OSAtomicOr32Orig((uint32_t)exchange, (volatile uint32_t*)dest); -#else // !_WIN32 || defined(__APPLE__) || defined(__MACOSX) - return (__sync_val_compare_and_swap( - dest, - comparand, - exchange)); -#endif // !_WIN32 - } - - inline void fence() { _mm_mfence(); } -}; // namespace detail - - -/*! \brief class used to interface between C++ and - * OpenCL C calls that require arrays of size_t values, whose - * size is known statically. - */ -template -class size_t -{ -private: - ::size_t data_[N]; - -public: - //! \brief Initialize size_t to all 0s - size_t() - { - for( int i = 0; i < N; ++i ) { - data_[i] = 0; - } - } - - ::size_t& operator[](int index) - { - return data_[index]; - } - - const ::size_t& operator[](int index) const - { - return data_[index]; - } - - //! \brief Conversion operator to T*. - operator ::size_t* () { return data_; } - - //! \brief Conversion operator to const T*. - operator const ::size_t* () const { return data_; } -}; - -namespace detail { - -// Generic getInfoHelper. The final parameter is used to guide overload -// resolution: the actual parameter passed is an int, which makes this -// a worse conversion sequence than a specialization that declares the -// parameter as an int. -template -inline cl_int getInfoHelper(Functor f, cl_uint name, T* param, long) -{ - return f(name, sizeof(T), param, NULL); -} - -// Specialized getInfoHelper for VECTOR_CLASS params -template -inline cl_int getInfoHelper(Func f, cl_uint name, VECTOR_CLASS* param, long) -{ - ::size_t required; - cl_int err = f(name, 0, NULL, &required); - if (err != CL_SUCCESS) { - return err; - } - - T* value = (T*) alloca(required); - err = f(name, required, value, NULL); - if (err != CL_SUCCESS) { - return err; - } - - param->assign(&value[0], &value[required/sizeof(T)]); - return CL_SUCCESS; -} - -/* Specialization for reference-counted types. This depends on the - * existence of Wrapper::cl_type, and none of the other types having the - * cl_type member. Note that simplify specifying the parameter as Wrapper - * does not work, because when using a derived type (e.g. Context) the generic - * template will provide a better match. - */ -template -inline cl_int getInfoHelper(Func f, cl_uint name, VECTOR_CLASS* param, int, typename T::cl_type = 0) -{ - ::size_t required; - cl_int err = f(name, 0, NULL, &required); - if (err != CL_SUCCESS) { - return err; - } - - typename T::cl_type * value = (typename T::cl_type *) alloca(required); - err = f(name, required, value, NULL); - if (err != CL_SUCCESS) { - return err; - } - - ::size_t elements = required / sizeof(typename T::cl_type); - param->assign(&value[0], &value[elements]); - for (::size_t i = 0; i < elements; i++) - { - if (value[i] != NULL) - { - err = (*param)[i].retain(); - if (err != CL_SUCCESS) { - return err; - } - } - } - return CL_SUCCESS; -} - -// Specialized for getInfo -template -inline cl_int getInfoHelper(Func f, cl_uint name, VECTOR_CLASS* param, int) -{ - cl_int err = f(name, param->size() * sizeof(char *), &(*param)[0], NULL); - - if (err != CL_SUCCESS) { - return err; - } - - return CL_SUCCESS; -} - -// Specialized GetInfoHelper for STRING_CLASS params -template -inline cl_int getInfoHelper(Func f, cl_uint name, STRING_CLASS* param, long) -{ - ::size_t required; - cl_int err = f(name, 0, NULL, &required); - if (err != CL_SUCCESS) { - return err; - } - - char* value = (char*) alloca(required); - err = f(name, required, value, NULL); - if (err != CL_SUCCESS) { - return err; - } - - *param = value; - return CL_SUCCESS; -} - -// Specialized GetInfoHelper for cl::size_t params -template -inline cl_int getInfoHelper(Func f, cl_uint name, size_t* param, long) -{ - ::size_t required; - cl_int err = f(name, 0, NULL, &required); - if (err != CL_SUCCESS) { - return err; - } - - ::size_t* value = (::size_t*) alloca(required); - err = f(name, required, value, NULL); - if (err != CL_SUCCESS) { - return err; - } - - for(int i = 0; i < N; ++i) { - (*param)[i] = value[i]; - } - - return CL_SUCCESS; -} - -template struct ReferenceHandler; - -/* Specialization for reference-counted types. This depends on the - * existence of Wrapper::cl_type, and none of the other types having the - * cl_type member. Note that simplify specifying the parameter as Wrapper - * does not work, because when using a derived type (e.g. Context) the generic - * template will provide a better match. - */ -template -inline cl_int getInfoHelper(Func f, cl_uint name, T* param, int, typename T::cl_type = 0) -{ - typename T::cl_type value; - cl_int err = f(name, sizeof(value), &value, NULL); - if (err != CL_SUCCESS) { - return err; - } - *param = value; - if (value != NULL) - { - err = param->retain(); - if (err != CL_SUCCESS) { - return err; - } - } - return CL_SUCCESS; -} - -#define __PARAM_NAME_INFO_1_0(F) \ - F(cl_platform_info, CL_PLATFORM_PROFILE, STRING_CLASS) \ - F(cl_platform_info, CL_PLATFORM_VERSION, STRING_CLASS) \ - F(cl_platform_info, CL_PLATFORM_NAME, STRING_CLASS) \ - F(cl_platform_info, CL_PLATFORM_VENDOR, STRING_CLASS) \ - F(cl_platform_info, CL_PLATFORM_EXTENSIONS, STRING_CLASS) \ - \ - F(cl_device_info, CL_DEVICE_TYPE, cl_device_type) \ - F(cl_device_info, CL_DEVICE_VENDOR_ID, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_COMPUTE_UNITS, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_WORK_GROUP_SIZE, ::size_t) \ - F(cl_device_info, CL_DEVICE_MAX_WORK_ITEM_SIZES, VECTOR_CLASS< ::size_t>) \ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, cl_uint) \ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT, cl_uint) \ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, cl_uint) \ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG, cl_uint) \ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT, cl_uint) \ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_CLOCK_FREQUENCY, cl_uint) \ - F(cl_device_info, CL_DEVICE_ADDRESS_BITS, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_READ_IMAGE_ARGS, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_WRITE_IMAGE_ARGS, cl_uint) \ - F(cl_device_info, CL_DEVICE_MAX_MEM_ALLOC_SIZE, cl_ulong) \ - F(cl_device_info, CL_DEVICE_IMAGE2D_MAX_WIDTH, ::size_t) \ - F(cl_device_info, CL_DEVICE_IMAGE2D_MAX_HEIGHT, ::size_t) \ - F(cl_device_info, CL_DEVICE_IMAGE3D_MAX_WIDTH, ::size_t) \ - F(cl_device_info, CL_DEVICE_IMAGE3D_MAX_HEIGHT, ::size_t) \ - F(cl_device_info, CL_DEVICE_IMAGE3D_MAX_DEPTH, ::size_t) \ - F(cl_device_info, CL_DEVICE_IMAGE_SUPPORT, cl_bool) \ - F(cl_device_info, CL_DEVICE_MAX_PARAMETER_SIZE, ::size_t) \ - F(cl_device_info, CL_DEVICE_MAX_SAMPLERS, cl_uint) \ - F(cl_device_info, CL_DEVICE_MEM_BASE_ADDR_ALIGN, cl_uint) \ - F(cl_device_info, CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE, cl_uint) \ - F(cl_device_info, CL_DEVICE_SINGLE_FP_CONFIG, cl_device_fp_config) \ - F(cl_device_info, CL_DEVICE_GLOBAL_MEM_CACHE_TYPE, cl_device_mem_cache_type) \ - F(cl_device_info, CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, cl_uint)\ - F(cl_device_info, CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, cl_ulong) \ - F(cl_device_info, CL_DEVICE_GLOBAL_MEM_SIZE, cl_ulong) \ - F(cl_device_info, CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, cl_ulong) \ - F(cl_device_info, CL_DEVICE_MAX_CONSTANT_ARGS, cl_uint) \ - F(cl_device_info, CL_DEVICE_LOCAL_MEM_TYPE, cl_device_local_mem_type) \ - F(cl_device_info, CL_DEVICE_LOCAL_MEM_SIZE, cl_ulong) \ - F(cl_device_info, CL_DEVICE_ERROR_CORRECTION_SUPPORT, cl_bool) \ - F(cl_device_info, CL_DEVICE_PROFILING_TIMER_RESOLUTION, ::size_t) \ - F(cl_device_info, CL_DEVICE_ENDIAN_LITTLE, cl_bool) \ - F(cl_device_info, CL_DEVICE_AVAILABLE, cl_bool) \ - F(cl_device_info, CL_DEVICE_COMPILER_AVAILABLE, cl_bool) \ - F(cl_device_info, CL_DEVICE_EXECUTION_CAPABILITIES, cl_device_exec_capabilities) \ - F(cl_device_info, CL_DEVICE_QUEUE_PROPERTIES, cl_command_queue_properties) \ - F(cl_device_info, CL_DEVICE_PLATFORM, cl_platform_id) \ - F(cl_device_info, CL_DEVICE_NAME, STRING_CLASS) \ - F(cl_device_info, CL_DEVICE_VENDOR, STRING_CLASS) \ - F(cl_device_info, CL_DRIVER_VERSION, STRING_CLASS) \ - F(cl_device_info, CL_DEVICE_PROFILE, STRING_CLASS) \ - F(cl_device_info, CL_DEVICE_VERSION, STRING_CLASS) \ - F(cl_device_info, CL_DEVICE_EXTENSIONS, STRING_CLASS) \ - \ - F(cl_context_info, CL_CONTEXT_REFERENCE_COUNT, cl_uint) \ - F(cl_context_info, CL_CONTEXT_DEVICES, VECTOR_CLASS) \ - F(cl_context_info, CL_CONTEXT_PROPERTIES, VECTOR_CLASS) \ - \ - F(cl_event_info, CL_EVENT_COMMAND_QUEUE, cl::CommandQueue) \ - F(cl_event_info, CL_EVENT_COMMAND_TYPE, cl_command_type) \ - F(cl_event_info, CL_EVENT_REFERENCE_COUNT, cl_uint) \ - F(cl_event_info, CL_EVENT_COMMAND_EXECUTION_STATUS, cl_uint) \ - \ - F(cl_profiling_info, CL_PROFILING_COMMAND_QUEUED, cl_ulong) \ - F(cl_profiling_info, CL_PROFILING_COMMAND_SUBMIT, cl_ulong) \ - F(cl_profiling_info, CL_PROFILING_COMMAND_START, cl_ulong) \ - F(cl_profiling_info, CL_PROFILING_COMMAND_END, cl_ulong) \ - \ - F(cl_mem_info, CL_MEM_TYPE, cl_mem_object_type) \ - F(cl_mem_info, CL_MEM_FLAGS, cl_mem_flags) \ - F(cl_mem_info, CL_MEM_SIZE, ::size_t) \ - F(cl_mem_info, CL_MEM_HOST_PTR, void*) \ - F(cl_mem_info, CL_MEM_MAP_COUNT, cl_uint) \ - F(cl_mem_info, CL_MEM_REFERENCE_COUNT, cl_uint) \ - F(cl_mem_info, CL_MEM_CONTEXT, cl::Context) \ - \ - F(cl_image_info, CL_IMAGE_FORMAT, cl_image_format) \ - F(cl_image_info, CL_IMAGE_ELEMENT_SIZE, ::size_t) \ - F(cl_image_info, CL_IMAGE_ROW_PITCH, ::size_t) \ - F(cl_image_info, CL_IMAGE_SLICE_PITCH, ::size_t) \ - F(cl_image_info, CL_IMAGE_WIDTH, ::size_t) \ - F(cl_image_info, CL_IMAGE_HEIGHT, ::size_t) \ - F(cl_image_info, CL_IMAGE_DEPTH, ::size_t) \ - \ - F(cl_sampler_info, CL_SAMPLER_REFERENCE_COUNT, cl_uint) \ - F(cl_sampler_info, CL_SAMPLER_CONTEXT, cl::Context) \ - F(cl_sampler_info, CL_SAMPLER_NORMALIZED_COORDS, cl_addressing_mode) \ - F(cl_sampler_info, CL_SAMPLER_ADDRESSING_MODE, cl_filter_mode) \ - F(cl_sampler_info, CL_SAMPLER_FILTER_MODE, cl_bool) \ - \ - F(cl_program_info, CL_PROGRAM_REFERENCE_COUNT, cl_uint) \ - F(cl_program_info, CL_PROGRAM_CONTEXT, cl::Context) \ - F(cl_program_info, CL_PROGRAM_NUM_DEVICES, cl_uint) \ - F(cl_program_info, CL_PROGRAM_DEVICES, VECTOR_CLASS) \ - F(cl_program_info, CL_PROGRAM_SOURCE, STRING_CLASS) \ - F(cl_program_info, CL_PROGRAM_BINARY_SIZES, VECTOR_CLASS< ::size_t>) \ - F(cl_program_info, CL_PROGRAM_BINARIES, VECTOR_CLASS) \ - \ - F(cl_program_build_info, CL_PROGRAM_BUILD_STATUS, cl_build_status) \ - F(cl_program_build_info, CL_PROGRAM_BUILD_OPTIONS, STRING_CLASS) \ - F(cl_program_build_info, CL_PROGRAM_BUILD_LOG, STRING_CLASS) \ - \ - F(cl_kernel_info, CL_KERNEL_FUNCTION_NAME, STRING_CLASS) \ - F(cl_kernel_info, CL_KERNEL_NUM_ARGS, cl_uint) \ - F(cl_kernel_info, CL_KERNEL_REFERENCE_COUNT, cl_uint) \ - F(cl_kernel_info, CL_KERNEL_CONTEXT, cl::Context) \ - F(cl_kernel_info, CL_KERNEL_PROGRAM, cl::Program) \ - \ - F(cl_kernel_work_group_info, CL_KERNEL_WORK_GROUP_SIZE, ::size_t) \ - F(cl_kernel_work_group_info, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, cl::size_t<3>) \ - F(cl_kernel_work_group_info, CL_KERNEL_LOCAL_MEM_SIZE, cl_ulong) \ - \ - F(cl_command_queue_info, CL_QUEUE_CONTEXT, cl::Context) \ - F(cl_command_queue_info, CL_QUEUE_DEVICE, cl::Device) \ - F(cl_command_queue_info, CL_QUEUE_REFERENCE_COUNT, cl_uint) \ - F(cl_command_queue_info, CL_QUEUE_PROPERTIES, cl_command_queue_properties) - -#if defined(CL_VERSION_1_1) -#define __PARAM_NAME_INFO_1_1(F) \ - F(cl_context_info, CL_CONTEXT_NUM_DEVICES, cl_uint)\ - F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_INT, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE, cl_uint) \ - F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF, cl_uint) \ - F(cl_device_info, CL_DEVICE_DOUBLE_FP_CONFIG, cl_device_fp_config) \ - F(cl_device_info, CL_DEVICE_HALF_FP_CONFIG, cl_device_fp_config) \ - F(cl_device_info, CL_DEVICE_HOST_UNIFIED_MEMORY, cl_bool) \ - F(cl_device_info, CL_DEVICE_OPENCL_C_VERSION, STRING_CLASS) \ - \ - F(cl_mem_info, CL_MEM_ASSOCIATED_MEMOBJECT, cl::Memory) \ - F(cl_mem_info, CL_MEM_OFFSET, ::size_t) \ - \ - F(cl_kernel_work_group_info, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, ::size_t) \ - F(cl_kernel_work_group_info, CL_KERNEL_PRIVATE_MEM_SIZE, cl_ulong) \ - \ - F(cl_event_info, CL_EVENT_CONTEXT, cl::Context) -#endif // CL_VERSION_1_1 - - -#if defined(CL_VERSION_1_2) -#define __PARAM_NAME_INFO_1_2(F) \ - F(cl_image_info, CL_IMAGE_BUFFER, cl::Buffer) \ - \ - F(cl_program_info, CL_PROGRAM_NUM_KERNELS, ::size_t) \ - F(cl_program_info, CL_PROGRAM_KERNEL_NAMES, STRING_CLASS) \ - \ - F(cl_program_build_info, CL_PROGRAM_BINARY_TYPE, cl_program_binary_type) \ - \ - F(cl_kernel_info, CL_KERNEL_ATTRIBUTES, STRING_CLASS) \ - \ - F(cl_kernel_arg_info, CL_KERNEL_ARG_ADDRESS_QUALIFIER, cl_kernel_arg_address_qualifier) \ - F(cl_kernel_arg_info, CL_KERNEL_ARG_ACCESS_QUALIFIER, cl_kernel_arg_access_qualifier) \ - F(cl_kernel_arg_info, CL_KERNEL_ARG_TYPE_NAME, STRING_CLASS) \ - F(cl_kernel_arg_info, CL_KERNEL_ARG_NAME, STRING_CLASS) \ - \ - F(cl_device_info, CL_DEVICE_PARENT_DEVICE, cl_device_id) \ - F(cl_device_info, CL_DEVICE_PARTITION_PROPERTIES, VECTOR_CLASS) \ - F(cl_device_info, CL_DEVICE_PARTITION_TYPE, VECTOR_CLASS) \ - F(cl_device_info, CL_DEVICE_REFERENCE_COUNT, cl_uint) \ - F(cl_device_info, CL_DEVICE_PREFERRED_INTEROP_USER_SYNC, ::size_t) \ - F(cl_device_info, CL_DEVICE_PARTITION_AFFINITY_DOMAIN, cl_device_affinity_domain) \ - F(cl_device_info, CL_DEVICE_BUILT_IN_KERNELS, STRING_CLASS) -#endif // #if defined(CL_VERSION_1_2) - -#if defined(USE_CL_DEVICE_FISSION) -#define __PARAM_NAME_DEVICE_FISSION(F) \ - F(cl_device_info, CL_DEVICE_PARENT_DEVICE_EXT, cl_device_id) \ - F(cl_device_info, CL_DEVICE_PARTITION_TYPES_EXT, VECTOR_CLASS) \ - F(cl_device_info, CL_DEVICE_AFFINITY_DOMAINS_EXT, VECTOR_CLASS) \ - F(cl_device_info, CL_DEVICE_REFERENCE_COUNT_EXT , cl_uint) \ - F(cl_device_info, CL_DEVICE_PARTITION_STYLE_EXT, VECTOR_CLASS) -#endif // USE_CL_DEVICE_FISSION - -template -struct param_traits {}; - -#define __CL_DECLARE_PARAM_TRAITS(token, param_name, T) \ -struct token; \ -template<> \ -struct param_traits \ -{ \ - enum { value = param_name }; \ - typedef T param_type; \ -}; - -__PARAM_NAME_INFO_1_0(__CL_DECLARE_PARAM_TRAITS) -#if defined(CL_VERSION_1_1) -__PARAM_NAME_INFO_1_1(__CL_DECLARE_PARAM_TRAITS) -#endif // CL_VERSION_1_1 -#if defined(CL_VERSION_1_2) -__PARAM_NAME_INFO_1_2(__CL_DECLARE_PARAM_TRAITS) -#endif // CL_VERSION_1_1 - -#if defined(USE_CL_DEVICE_FISSION) -__PARAM_NAME_DEVICE_FISSION(__CL_DECLARE_PARAM_TRAITS); -#endif // USE_CL_DEVICE_FISSION - -#ifdef CL_PLATFORM_ICD_SUFFIX_KHR -__CL_DECLARE_PARAM_TRAITS(cl_platform_info, CL_PLATFORM_ICD_SUFFIX_KHR, STRING_CLASS) -#endif - -#ifdef CL_DEVICE_PROFILING_TIMER_OFFSET_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_PROFILING_TIMER_OFFSET_AMD, cl_ulong) -#endif - -#ifdef CL_DEVICE_GLOBAL_FREE_MEMORY_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_GLOBAL_FREE_MEMORY_AMD, VECTOR_CLASS< ::size_t>) -#endif -#ifdef CL_DEVICE_SIMD_PER_COMPUTE_UNIT_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_SIMD_PER_COMPUTE_UNIT_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_SIMD_WIDTH_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_SIMD_WIDTH_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_SIMD_INSTRUCTION_WIDTH_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_SIMD_INSTRUCTION_WIDTH_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_WAVEFRONT_WIDTH_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_WAVEFRONT_WIDTH_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_GLOBAL_MEM_CHANNELS_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_GLOBAL_MEM_CHANNELS_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_GLOBAL_MEM_CHANNEL_BANKS_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_GLOBAL_MEM_CHANNEL_BANKS_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_GLOBAL_MEM_CHANNEL_BANK_WIDTH_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_GLOBAL_MEM_CHANNEL_BANK_WIDTH_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_LOCAL_MEM_SIZE_PER_COMPUTE_UNIT_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_LOCAL_MEM_SIZE_PER_COMPUTE_UNIT_AMD, cl_uint) -#endif -#ifdef CL_DEVICE_LOCAL_MEM_BANKS_AMD -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_LOCAL_MEM_BANKS_AMD, cl_uint) -#endif - -#ifdef CL_DEVICE_COMPUTE_CAPABILITY_MAJOR_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_COMPUTE_CAPABILITY_MAJOR_NV, cl_uint) -#endif -#ifdef CL_DEVICE_COMPUTE_CAPABILITY_MINOR_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_COMPUTE_CAPABILITY_MINOR_NV, cl_uint) -#endif -#ifdef CL_DEVICE_REGISTERS_PER_BLOCK_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_REGISTERS_PER_BLOCK_NV, cl_uint) -#endif -#ifdef CL_DEVICE_WARP_SIZE_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_WARP_SIZE_NV, cl_uint) -#endif -#ifdef CL_DEVICE_GPU_OVERLAP_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_GPU_OVERLAP_NV, cl_bool) -#endif -#ifdef CL_DEVICE_KERNEL_EXEC_TIMEOUT_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_KERNEL_EXEC_TIMEOUT_NV, cl_bool) -#endif -#ifdef CL_DEVICE_INTEGRATED_MEMORY_NV -__CL_DECLARE_PARAM_TRAITS(cl_device_info, CL_DEVICE_INTEGRATED_MEMORY_NV, cl_bool) -#endif - -// Convenience functions - -template -inline cl_int -getInfo(Func f, cl_uint name, T* param) -{ - return getInfoHelper(f, name, param, 0); -} - -template -struct GetInfoFunctor0 -{ - Func f_; const Arg0& arg0_; - cl_int operator ()( - cl_uint param, ::size_t size, void* value, ::size_t* size_ret) - { return f_(arg0_, param, size, value, size_ret); } -}; - -template -struct GetInfoFunctor1 -{ - Func f_; const Arg0& arg0_; const Arg1& arg1_; - cl_int operator ()( - cl_uint param, ::size_t size, void* value, ::size_t* size_ret) - { return f_(arg0_, arg1_, param, size, value, size_ret); } -}; - -template -inline cl_int -getInfo(Func f, const Arg0& arg0, cl_uint name, T* param) -{ - GetInfoFunctor0 f0 = { f, arg0 }; - return getInfoHelper(f0, name, param, 0); -} - -template -inline cl_int -getInfo(Func f, const Arg0& arg0, const Arg1& arg1, cl_uint name, T* param) -{ - GetInfoFunctor1 f0 = { f, arg0, arg1 }; - return getInfoHelper(f0, name, param, 0); -} - -template -struct ReferenceHandler -{ }; - -#if defined(CL_VERSION_1_2) -/** - * OpenCL 1.2 devices do have retain/release. - */ -template <> -struct ReferenceHandler -{ - /** - * Retain the device. - * \param device A valid device created using createSubDevices - * \return - * CL_SUCCESS if the function executed successfully. - * CL_INVALID_DEVICE if device was not a valid subdevice - * CL_OUT_OF_RESOURCES - * CL_OUT_OF_HOST_MEMORY - */ - static cl_int retain(cl_device_id device) - { return ::clRetainDevice(device); } - /** - * Retain the device. - * \param device A valid device created using createSubDevices - * \return - * CL_SUCCESS if the function executed successfully. - * CL_INVALID_DEVICE if device was not a valid subdevice - * CL_OUT_OF_RESOURCES - * CL_OUT_OF_HOST_MEMORY - */ - static cl_int release(cl_device_id device) - { return ::clReleaseDevice(device); } -}; -#else // #if defined(CL_VERSION_1_2) -/** - * OpenCL 1.1 devices do not have retain/release. - */ -template <> -struct ReferenceHandler -{ - // cl_device_id does not have retain(). - static cl_int retain(cl_device_id) - { return CL_SUCCESS; } - // cl_device_id does not have release(). - static cl_int release(cl_device_id) - { return CL_SUCCESS; } -}; -#endif // #if defined(CL_VERSION_1_2) - -template <> -struct ReferenceHandler -{ - // cl_platform_id does not have retain(). - static cl_int retain(cl_platform_id) - { return CL_SUCCESS; } - // cl_platform_id does not have release(). - static cl_int release(cl_platform_id) - { return CL_SUCCESS; } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_context context) - { return ::clRetainContext(context); } - static cl_int release(cl_context context) - { return ::clReleaseContext(context); } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_command_queue queue) - { return ::clRetainCommandQueue(queue); } - static cl_int release(cl_command_queue queue) - { return ::clReleaseCommandQueue(queue); } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_mem memory) - { return ::clRetainMemObject(memory); } - static cl_int release(cl_mem memory) - { return ::clReleaseMemObject(memory); } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_sampler sampler) - { return ::clRetainSampler(sampler); } - static cl_int release(cl_sampler sampler) - { return ::clReleaseSampler(sampler); } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_program program) - { return ::clRetainProgram(program); } - static cl_int release(cl_program program) - { return ::clReleaseProgram(program); } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_kernel kernel) - { return ::clRetainKernel(kernel); } - static cl_int release(cl_kernel kernel) - { return ::clReleaseKernel(kernel); } -}; - -template <> -struct ReferenceHandler -{ - static cl_int retain(cl_event event) - { return ::clRetainEvent(event); } - static cl_int release(cl_event event) - { return ::clReleaseEvent(event); } -}; - - -// Extracts version number with major in the upper 16 bits, minor in the lower 16 -static cl_uint getVersion(const char *versionInfo) -{ - int highVersion = 0; - int lowVersion = 0; - int index = 7; - while(versionInfo[index] != '.' ) { - highVersion *= 10; - highVersion += versionInfo[index]-'0'; - ++index; - } - ++index; - while(versionInfo[index] != ' ' ) { - lowVersion *= 10; - lowVersion += versionInfo[index]-'0'; - ++index; - } - return (highVersion << 16) | lowVersion; -} - -static cl_uint getPlatformVersion(cl_platform_id platform) -{ - ::size_t size = 0; - clGetPlatformInfo(platform, CL_PLATFORM_VERSION, 0, NULL, &size); - char *versionInfo = (char *) alloca(size); - clGetPlatformInfo(platform, CL_PLATFORM_VERSION, size, &versionInfo[0], &size); - return getVersion(versionInfo); -} - -static cl_uint getDevicePlatformVersion(cl_device_id device) -{ - cl_platform_id platform; - clGetDeviceInfo(device, CL_DEVICE_PLATFORM, sizeof(platform), &platform, NULL); - return getPlatformVersion(platform); -} - -#if defined(CL_VERSION_1_2) && defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) -static cl_uint getContextPlatformVersion(cl_context context) -{ - // The platform cannot be queried directly, so we first have to grab a - // device and obtain its context - ::size_t size = 0; - clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &size); - if (size == 0) - return 0; - cl_device_id *devices = (cl_device_id *) alloca(size); - clGetContextInfo(context, CL_CONTEXT_DEVICES, size, devices, NULL); - return getDevicePlatformVersion(devices[0]); -} -#endif // #if defined(CL_VERSION_1_2) && defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - -template -class Wrapper -{ -public: - typedef T cl_type; - -protected: - cl_type object_; - -public: - Wrapper() : object_(NULL) { } - - Wrapper(const cl_type &obj) : object_(obj) { } - - ~Wrapper() - { - if (object_ != NULL) { release(); } - } - - Wrapper(const Wrapper& rhs) - { - object_ = rhs.object_; - if (object_ != NULL) { detail::errHandler(retain(), __RETAIN_ERR); } - } - - Wrapper& operator = (const Wrapper& rhs) - { - if (object_ != NULL) { detail::errHandler(release(), __RELEASE_ERR); } - object_ = rhs.object_; - if (object_ != NULL) { detail::errHandler(retain(), __RETAIN_ERR); } - return *this; - } - - Wrapper& operator = (const cl_type &rhs) - { - if (object_ != NULL) { detail::errHandler(release(), __RELEASE_ERR); } - object_ = rhs; - return *this; - } - - cl_type operator ()() const { return object_; } - - cl_type& operator ()() { return object_; } - -protected: - template - friend inline cl_int getInfoHelper(Func, cl_uint, U*, int, typename U::cl_type); - - cl_int retain() const - { - return ReferenceHandler::retain(object_); - } - - cl_int release() const - { - return ReferenceHandler::release(object_); - } -}; - -template <> -class Wrapper -{ -public: - typedef cl_device_id cl_type; - -protected: - cl_type object_; - bool referenceCountable_; - - static bool isReferenceCountable(cl_device_id device) - { - bool retVal = false; - if (device != NULL) { - int version = getDevicePlatformVersion(device); - if(version > ((1 << 16) + 1)) { - retVal = true; - } - } - return retVal; - } - -public: - Wrapper() : object_(NULL), referenceCountable_(false) - { - } - - Wrapper(const cl_type &obj) : object_(obj), referenceCountable_(false) - { - referenceCountable_ = isReferenceCountable(obj); - } - - ~Wrapper() - { - if (object_ != NULL) { release(); } - } - - Wrapper(const Wrapper& rhs) - { - object_ = rhs.object_; - referenceCountable_ = isReferenceCountable(object_); - if (object_ != NULL) { detail::errHandler(retain(), __RETAIN_ERR); } - } - - Wrapper& operator = (const Wrapper& rhs) - { - if (object_ != NULL) { detail::errHandler(release(), __RELEASE_ERR); } - object_ = rhs.object_; - referenceCountable_ = rhs.referenceCountable_; - if (object_ != NULL) { detail::errHandler(retain(), __RETAIN_ERR); } - return *this; - } - - Wrapper& operator = (const cl_type &rhs) - { - if (object_ != NULL) { detail::errHandler(release(), __RELEASE_ERR); } - object_ = rhs; - referenceCountable_ = isReferenceCountable(object_); - return *this; - } - - cl_type operator ()() const { return object_; } - - cl_type& operator ()() { return object_; } - -protected: - template - friend inline cl_int getInfoHelper(Func, cl_uint, U*, int, typename U::cl_type); - - template - friend inline cl_int getInfoHelper(Func, cl_uint, VECTOR_CLASS*, int, typename U::cl_type); - - cl_int retain() const - { - if( referenceCountable_ ) { - return ReferenceHandler::retain(object_); - } - else { - return CL_SUCCESS; - } - } - - cl_int release() const - { - if( referenceCountable_ ) { - return ReferenceHandler::release(object_); - } - else { - return CL_SUCCESS; - } - } -}; - -} // namespace detail -//! \endcond - -/*! \stuct ImageFormat - * \brief Adds constructors and member functions for cl_image_format. - * - * \see cl_image_format - */ -struct ImageFormat : public cl_image_format -{ - //! \brief Default constructor - performs no initialization. - ImageFormat(){} - - //! \brief Initializing constructor. - ImageFormat(cl_channel_order order, cl_channel_type type) - { - image_channel_order = order; - image_channel_data_type = type; - } - - //! \brief Assignment operator. - ImageFormat& operator = (const ImageFormat& rhs) - { - if (this != &rhs) { - this->image_channel_data_type = rhs.image_channel_data_type; - this->image_channel_order = rhs.image_channel_order; - } - return *this; - } -}; - -/*! \brief Class interface for cl_device_id. - * - * \note Copies of these objects are inexpensive, since they don't 'own' - * any underlying resources or data structures. - * - * \see cl_device_id - */ -class Device : public detail::Wrapper -{ -public: - //! \brief Default constructor - initializes to NULL. - Device() : detail::Wrapper() { } - - /*! \brief Copy constructor. - * - * This simply copies the device ID value, which is an inexpensive operation. - */ - Device(const Device& device) : detail::Wrapper(device) { } - - /*! \brief Constructor from cl_device_id. - * - * This simply copies the device ID value, which is an inexpensive operation. - */ - Device(const cl_device_id &device) : detail::Wrapper(device) { } - - /*! \brief Returns the first device on the default context. - * - * \see Context::getDefault() - */ - static Device getDefault(cl_int * err = NULL); - - /*! \brief Assignment operator from Device. - * - * This simply copies the device ID value, which is an inexpensive operation. - */ - Device& operator = (const Device& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_device_id. - * - * This simply copies the device ID value, which is an inexpensive operation. - */ - Device& operator = (const cl_device_id& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetDeviceInfo(). - template - cl_int getInfo(cl_device_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetDeviceInfo, object_, name, param), - __GET_DEVICE_INFO_ERR); - } - - //! \brief Wrapper for clGetDeviceInfo() that returns by value. - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_device_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - /** - * CL 1.2 version - */ -#if defined(CL_VERSION_1_2) - //! \brief Wrapper for clCreateSubDevicesEXT(). - cl_int createSubDevices( - const cl_device_partition_property * properties, - VECTOR_CLASS* devices) - { - cl_uint n = 0; - cl_int err = clCreateSubDevices(object_, properties, 0, NULL, &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_SUB_DEVICES); - } - - cl_device_id* ids = (cl_device_id*) alloca(n * sizeof(cl_device_id)); - err = clCreateSubDevices(object_, properties, n, ids, NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_SUB_DEVICES); - } - - devices->assign(&ids[0], &ids[n]); - return CL_SUCCESS; - } -#endif // #if defined(CL_VERSION_1_2) - -/** - * CL 1.1 version that uses device fission. - */ -#if defined(CL_VERSION_1_1) -#if defined(USE_CL_DEVICE_FISSION) - cl_int createSubDevices( - const cl_device_partition_property_ext * properties, - VECTOR_CLASS* devices) - { - typedef CL_API_ENTRY cl_int - ( CL_API_CALL * PFN_clCreateSubDevicesEXT)( - cl_device_id /*in_device*/, - const cl_device_partition_property_ext * /* properties */, - cl_uint /*num_entries*/, - cl_device_id * /*out_devices*/, - cl_uint * /*num_devices*/ ) CL_EXT_SUFFIX__VERSION_1_1; - - static PFN_clCreateSubDevicesEXT pfn_clCreateSubDevicesEXT = NULL; - __INIT_CL_EXT_FCN_PTR(clCreateSubDevicesEXT); - - cl_uint n = 0; - cl_int err = pfn_clCreateSubDevicesEXT(object_, properties, 0, NULL, &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_SUB_DEVICES); - } - - cl_device_id* ids = (cl_device_id*) alloca(n * sizeof(cl_device_id)); - err = pfn_clCreateSubDevicesEXT(object_, properties, n, ids, NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_SUB_DEVICES); - } - - devices->assign(&ids[0], &ids[n]); - return CL_SUCCESS; - } -#endif // #if defined(USE_CL_DEVICE_FISSION) -#endif // #if defined(CL_VERSION_1_1) -}; - -/*! \brief Class interface for cl_platform_id. - * - * \note Copies of these objects are inexpensive, since they don't 'own' - * any underlying resources or data structures. - * - * \see cl_platform_id - */ -class Platform : public detail::Wrapper -{ -public: - //! \brief Default constructor - initializes to NULL. - Platform() : detail::Wrapper() { } - - /*! \brief Copy constructor. - * - * This simply copies the platform ID value, which is an inexpensive operation. - */ - Platform(const Platform& platform) : detail::Wrapper(platform) { } - - /*! \brief Constructor from cl_platform_id. - * - * This simply copies the platform ID value, which is an inexpensive operation. - */ - Platform(const cl_platform_id &platform) : detail::Wrapper(platform) { } - - /*! \brief Assignment operator from Platform. - * - * This simply copies the platform ID value, which is an inexpensive operation. - */ - Platform& operator = (const Platform& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_platform_id. - * - * This simply copies the platform ID value, which is an inexpensive operation. - */ - Platform& operator = (const cl_platform_id& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetPlatformInfo(). - cl_int getInfo(cl_platform_info name, STRING_CLASS* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetPlatformInfo, object_, name, param), - __GET_PLATFORM_INFO_ERR); - } - - //! \brief Wrapper for clGetPlatformInfo() that returns by value. - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_platform_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - /*! \brief Gets a list of devices for this platform. - * - * Wraps clGetDeviceIDs(). - */ - cl_int getDevices( - cl_device_type type, - VECTOR_CLASS* devices) const - { - cl_uint n = 0; - if( devices == NULL ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); - } - cl_int err = ::clGetDeviceIDs(object_, type, 0, NULL, &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_DEVICE_IDS_ERR); - } - - cl_device_id* ids = (cl_device_id*) alloca(n * sizeof(cl_device_id)); - err = ::clGetDeviceIDs(object_, type, n, ids, NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_DEVICE_IDS_ERR); - } - - devices->assign(&ids[0], &ids[n]); - return CL_SUCCESS; - } - -#if defined(USE_DX_INTEROP) - /*! \brief Get the list of available D3D10 devices. - * - * \param d3d_device_source. - * - * \param d3d_object. - * - * \param d3d_device_set. - * - * \param devices returns a vector of OpenCL D3D10 devices found. The cl::Device - * values returned in devices can be used to identify a specific OpenCL - * device. If \a devices argument is NULL, this argument is ignored. - * - * \return One of the following values: - * - CL_SUCCESS if the function is executed successfully. - * - * The application can query specific capabilities of the OpenCL device(s) - * returned by cl::getDevices. This can be used by the application to - * determine which device(s) to use. - * - * \note In the case that exceptions are enabled and a return value - * other than CL_SUCCESS is generated, then cl::Error exception is - * generated. - */ - cl_int getDevices( - cl_d3d10_device_source_khr d3d_device_source, - void * d3d_object, - cl_d3d10_device_set_khr d3d_device_set, - VECTOR_CLASS* devices) const - { - typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clGetDeviceIDsFromD3D10KHR)( - cl_platform_id platform, - cl_d3d10_device_source_khr d3d_device_source, - void * d3d_object, - cl_d3d10_device_set_khr d3d_device_set, - cl_uint num_entries, - cl_device_id * devices, - cl_uint* num_devices); - - if( devices == NULL ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_DEVICE_IDS_ERR); - } - - static PFN_clGetDeviceIDsFromD3D10KHR pfn_clGetDeviceIDsFromD3D10KHR = NULL; - __INIT_CL_EXT_FCN_PTR_PLATFORM(object_, clGetDeviceIDsFromD3D10KHR); - - cl_uint n = 0; - cl_int err = pfn_clGetDeviceIDsFromD3D10KHR( - object_, - d3d_device_source, - d3d_object, - d3d_device_set, - 0, - NULL, - &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_DEVICE_IDS_ERR); - } - - cl_device_id* ids = (cl_device_id*) alloca(n * sizeof(cl_device_id)); - err = pfn_clGetDeviceIDsFromD3D10KHR( - object_, - d3d_device_source, - d3d_object, - d3d_device_set, - n, - ids, - NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_DEVICE_IDS_ERR); - } - - devices->assign(&ids[0], &ids[n]); - return CL_SUCCESS; - } -#endif - - /*! \brief Gets a list of available platforms. - * - * Wraps clGetPlatformIDs(). - */ - static cl_int get( - VECTOR_CLASS* platforms) - { - cl_uint n = 0; - - if( platforms == NULL ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_PLATFORM_IDS_ERR); - } - - cl_int err = ::clGetPlatformIDs(0, NULL, &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_PLATFORM_IDS_ERR); - } - - cl_platform_id* ids = (cl_platform_id*) alloca( - n * sizeof(cl_platform_id)); - err = ::clGetPlatformIDs(n, ids, NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_PLATFORM_IDS_ERR); - } - - platforms->assign(&ids[0], &ids[n]); - return CL_SUCCESS; - } - - /*! \brief Gets the first available platform. - * - * Wraps clGetPlatformIDs(), returning the first result. - */ - static cl_int get( - Platform * platform) - { - cl_uint n = 0; - - if( platform == NULL ) { - return detail::errHandler(CL_INVALID_ARG_VALUE, __GET_PLATFORM_IDS_ERR); - } - - cl_int err = ::clGetPlatformIDs(0, NULL, &n); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_PLATFORM_IDS_ERR); - } - - cl_platform_id* ids = (cl_platform_id*) alloca( - n * sizeof(cl_platform_id)); - err = ::clGetPlatformIDs(n, ids, NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_PLATFORM_IDS_ERR); - } - - *platform = ids[0]; - return CL_SUCCESS; - } - - /*! \brief Gets the first available platform, returning it by value. - * - * Wraps clGetPlatformIDs(), returning the first result. - */ - static Platform get( - cl_int * errResult = NULL) - { - Platform platform; - cl_uint n = 0; - cl_int err = ::clGetPlatformIDs(0, NULL, &n); - if (err != CL_SUCCESS) { - detail::errHandler(err, __GET_PLATFORM_IDS_ERR); - if (errResult != NULL) { - *errResult = err; - } - } - - cl_platform_id* ids = (cl_platform_id*) alloca( - n * sizeof(cl_platform_id)); - err = ::clGetPlatformIDs(n, ids, NULL); - - if (err != CL_SUCCESS) { - detail::errHandler(err, __GET_PLATFORM_IDS_ERR); - } - - if (errResult != NULL) { - *errResult = err; - } - - return ids[0]; - } - - static Platform getDefault( - cl_int *errResult = NULL ) - { - return get(errResult); - } - - -#if defined(CL_VERSION_1_2) - //! \brief Wrapper for clUnloadCompiler(). - cl_int - unloadCompiler() - { - return ::clUnloadPlatformCompiler(object_); - } -#endif // #if defined(CL_VERSION_1_2) -}; // class Platform - -/** - * Deprecated APIs for 1.2 - */ -#if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) || (defined(CL_VERSION_1_1) && !defined(CL_VERSION_1_2)) -/** - * Unload the OpenCL compiler. - * \note Deprecated for OpenCL 1.2. Use Platform::unloadCompiler instead. - */ -inline CL_EXT_PREFIX__VERSION_1_1_DEPRECATED cl_int -UnloadCompiler() CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED; -inline cl_int -UnloadCompiler() -{ - return ::clUnloadCompiler(); -} -#endif // #if defined(CL_VERSION_1_1) - -/*! \brief Class interface for cl_context. - * - * \note Copies of these objects are shallow, meaning that the copy will refer - * to the same underlying cl_context as the original. For details, see - * clRetainContext() and clReleaseContext(). - * - * \see cl_context - */ -class Context - : public detail::Wrapper -{ -private: - static volatile int default_initialized_; - static Context default_; - static volatile cl_int default_error_; -public: - /*! \brief Destructor. - * - * This calls clReleaseContext() on the value held by this instance. - */ - ~Context() { } - - /*! \brief Constructs a context including a list of specified devices. - * - * Wraps clCreateContext(). - */ - Context( - const VECTOR_CLASS& devices, - cl_context_properties* properties = NULL, - void (CL_CALLBACK * notifyFptr)( - const char *, - const void *, - ::size_t, - void *) = NULL, - void* data = NULL, - cl_int* err = NULL) - { - cl_int error; - - ::size_t numDevices = devices.size(); - cl_device_id* deviceIDs = (cl_device_id*) alloca(numDevices * sizeof(cl_device_id)); - for( ::size_t deviceIndex = 0; deviceIndex < numDevices; ++deviceIndex ) { - deviceIDs[deviceIndex] = (devices[deviceIndex])(); - } - - object_ = ::clCreateContext( - properties, (cl_uint) numDevices, - deviceIDs, - notifyFptr, data, &error); - - detail::errHandler(error, __CREATE_CONTEXT_ERR); - if (err != NULL) { - *err = error; - } - } - - Context( - const Device& device, - cl_context_properties* properties = NULL, - void (CL_CALLBACK * notifyFptr)( - const char *, - const void *, - ::size_t, - void *) = NULL, - void* data = NULL, - cl_int* err = NULL) - { - cl_int error; - - cl_device_id deviceID = device(); - - object_ = ::clCreateContext( - properties, 1, - &deviceID, - notifyFptr, data, &error); - - detail::errHandler(error, __CREATE_CONTEXT_ERR); - if (err != NULL) { - *err = error; - } - } - - /*! \brief Constructs a context including all or a subset of devices of a specified type. - * - * Wraps clCreateContextFromType(). - */ - Context( - cl_device_type type, - cl_context_properties* properties = NULL, - void (CL_CALLBACK * notifyFptr)( - const char *, - const void *, - ::size_t, - void *) = NULL, - void* data = NULL, - cl_int* err = NULL) - { - cl_int error; - -#if !defined(__APPLE__) || !defined(__MACOS) - cl_context_properties prop[4] = {CL_CONTEXT_PLATFORM, 0, 0, 0 }; - - if (properties == NULL) { - // Get a valid platform ID as we cannot send in a blank one - VECTOR_CLASS platforms; - error = Platform::get(&platforms); - if (error != CL_SUCCESS) { - detail::errHandler(error, __CREATE_CONTEXT_FROM_TYPE_ERR); - if (err != NULL) { - *err = error; - } - return; - } - - // Check the platforms we found for a device of our specified type - cl_context_properties platform_id = 0; - for (unsigned int i = 0; i < platforms.size(); i++) { - - VECTOR_CLASS devices; - -#if defined(__CL_ENABLE_EXCEPTIONS) - try { -#endif - - error = platforms[i].getDevices(type, &devices); - -#if defined(__CL_ENABLE_EXCEPTIONS) - } catch (Error) {} - // Catch if exceptions are enabled as we don't want to exit if first platform has no devices of type - // We do error checking next anyway, and can throw there if needed -#endif - - // Only squash CL_SUCCESS and CL_DEVICE_NOT_FOUND - if (error != CL_SUCCESS && error != CL_DEVICE_NOT_FOUND) { - detail::errHandler(error, __CREATE_CONTEXT_FROM_TYPE_ERR); - if (err != NULL) { - *err = error; - } - } - - if (devices.size() > 0) { - platform_id = (cl_context_properties)platforms[i](); - break; - } - } - - if (platform_id == 0) { - detail::errHandler(CL_DEVICE_NOT_FOUND, __CREATE_CONTEXT_FROM_TYPE_ERR); - if (err != NULL) { - *err = CL_DEVICE_NOT_FOUND; - } - return; - } - - prop[1] = platform_id; - properties = &prop[0]; - } -#endif - object_ = ::clCreateContextFromType( - properties, type, notifyFptr, data, &error); - - detail::errHandler(error, __CREATE_CONTEXT_FROM_TYPE_ERR); - if (err != NULL) { - *err = error; - } - } - - /*! \brief Returns a singleton context including all devices of CL_DEVICE_TYPE_DEFAULT. - * - * \note All calls to this function return the same cl_context as the first. - */ - static Context getDefault(cl_int * err = NULL) - { - int state = detail::compare_exchange( - &default_initialized_, - __DEFAULT_BEING_INITIALIZED, __DEFAULT_NOT_INITIALIZED); - - if (state & __DEFAULT_INITIALIZED) { - if (err != NULL) { - *err = default_error_; - } - return default_; - } - - if (state & __DEFAULT_BEING_INITIALIZED) { - // Assume writes will propagate eventually... - while(default_initialized_ != __DEFAULT_INITIALIZED) { - detail::fence(); - } - - if (err != NULL) { - *err = default_error_; - } - return default_; - } - - cl_int error; - default_ = Context( - CL_DEVICE_TYPE_DEFAULT, - NULL, - NULL, - NULL, - &error); - - detail::fence(); - - default_error_ = error; - // Assume writes will propagate eventually... - default_initialized_ = __DEFAULT_INITIALIZED; - - detail::fence(); - - if (err != NULL) { - *err = default_error_; - } - return default_; - - } - - //! \brief Default constructor - initializes to NULL. - Context() : detail::Wrapper() { } - - /*! \brief Copy constructor. - * - * This calls clRetainContext() on the parameter's cl_context. - */ - Context(const Context& context) : detail::Wrapper(context) { } - - /*! \brief Constructor from cl_context - takes ownership. - * - * This effectively transfers ownership of a refcount on the cl_context - * into the new Context object. - */ - __CL_EXPLICIT_CONSTRUCTORS Context(const cl_context& context) : detail::Wrapper(context) { } - - /*! \brief Assignment operator from Context. - * - * This calls clRetainContext() on the parameter and clReleaseContext() on - * the previous value held by this instance. - */ - Context& operator = (const Context& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_context - takes ownership. - * - * This effectively transfers ownership of a refcount on the rhs and calls - * clReleaseContext() on the value previously held by this instance. - */ - Context& operator = (const cl_context& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetContextInfo(). - template - cl_int getInfo(cl_context_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetContextInfo, object_, name, param), - __GET_CONTEXT_INFO_ERR); - } - - //! \brief Wrapper for clGetContextInfo() that returns by value. - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_context_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - /*! \brief Gets a list of supported image formats. - * - * Wraps clGetSupportedImageFormats(). - */ - cl_int getSupportedImageFormats( - cl_mem_flags flags, - cl_mem_object_type type, - VECTOR_CLASS* formats) const - { - cl_uint numEntries; - cl_int err = ::clGetSupportedImageFormats( - object_, - flags, - type, - 0, - NULL, - &numEntries); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_SUPPORTED_IMAGE_FORMATS_ERR); - } - - ImageFormat* value = (ImageFormat*) - alloca(numEntries * sizeof(ImageFormat)); - err = ::clGetSupportedImageFormats( - object_, - flags, - type, - numEntries, - (cl_image_format*) value, - NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __GET_SUPPORTED_IMAGE_FORMATS_ERR); - } - - formats->assign(&value[0], &value[numEntries]); - return CL_SUCCESS; - } -}; - -inline Device Device::getDefault(cl_int * err) -{ - cl_int error; - Device device; - - Context context = Context::getDefault(&error); - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - - if (error != CL_SUCCESS) { - if (err != NULL) { - *err = error; - } - } - else { - device = context.getInfo()[0]; - if (err != NULL) { - *err = CL_SUCCESS; - } - } - - return device; -} - - -#ifdef _WIN32 -__declspec(selectany) volatile int Context::default_initialized_ = __DEFAULT_NOT_INITIALIZED; -__declspec(selectany) Context Context::default_; -__declspec(selectany) volatile cl_int Context::default_error_ = CL_SUCCESS; -#else -__attribute__((weak)) volatile int Context::default_initialized_ = __DEFAULT_NOT_INITIALIZED; -__attribute__((weak)) Context Context::default_; -__attribute__((weak)) volatile cl_int Context::default_error_ = CL_SUCCESS; -#endif - -/*! \brief Class interface for cl_event. - * - * \note Copies of these objects are shallow, meaning that the copy will refer - * to the same underlying cl_event as the original. For details, see - * clRetainEvent() and clReleaseEvent(). - * - * \see cl_event - */ -class Event : public detail::Wrapper -{ -public: - /*! \brief Destructor. - * - * This calls clReleaseEvent() on the value held by this instance. - */ - ~Event() { } - - //! \brief Default constructor - initializes to NULL. - Event() : detail::Wrapper() { } - - /*! \brief Copy constructor. - * - * This calls clRetainEvent() on the parameter's cl_event. - */ - Event(const Event& event) : detail::Wrapper(event) { } - - /*! \brief Constructor from cl_event - takes ownership. - * - * This effectively transfers ownership of a refcount on the cl_event - * into the new Event object. - */ - Event(const cl_event& event) : detail::Wrapper(event) { } - - /*! \brief Assignment operator from cl_event - takes ownership. - * - * This effectively transfers ownership of a refcount on the rhs and calls - * clReleaseEvent() on the value previously held by this instance. - */ - Event& operator = (const Event& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_event. - * - * This calls clRetainEvent() on the parameter and clReleaseEvent() on - * the previous value held by this instance. - */ - Event& operator = (const cl_event& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetEventInfo(). - template - cl_int getInfo(cl_event_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetEventInfo, object_, name, param), - __GET_EVENT_INFO_ERR); - } - - //! \brief Wrapper for clGetEventInfo() that returns by value. - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_event_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - //! \brief Wrapper for clGetEventProfilingInfo(). - template - cl_int getProfilingInfo(cl_profiling_info name, T* param) const - { - return detail::errHandler(detail::getInfo( - &::clGetEventProfilingInfo, object_, name, param), - __GET_EVENT_PROFILE_INFO_ERR); - } - - //! \brief Wrapper for clGetEventProfilingInfo() that returns by value. - template typename - detail::param_traits::param_type - getProfilingInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_profiling_info, name>::param_type param; - cl_int result = getProfilingInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - /*! \brief Blocks the calling thread until this event completes. - * - * Wraps clWaitForEvents(). - */ - cl_int wait() const - { - return detail::errHandler( - ::clWaitForEvents(1, &object_), - __WAIT_FOR_EVENTS_ERR); - } - -#if defined(CL_VERSION_1_1) - /*! \brief Registers a user callback function for a specific command execution status. - * - * Wraps clSetEventCallback(). - */ - cl_int setCallback( - cl_int type, - void (CL_CALLBACK * pfn_notify)(cl_event, cl_int, void *), - void * user_data = NULL) - { - return detail::errHandler( - ::clSetEventCallback( - object_, - type, - pfn_notify, - user_data), - __SET_EVENT_CALLBACK_ERR); - } -#endif - - /*! \brief Blocks the calling thread until every event specified is complete. - * - * Wraps clWaitForEvents(). - */ - static cl_int - waitForEvents(const VECTOR_CLASS& events) - { - return detail::errHandler( - ::clWaitForEvents( - (cl_uint) events.size(), (cl_event*)&events.front()), - __WAIT_FOR_EVENTS_ERR); - } -}; - -#if defined(CL_VERSION_1_1) -/*! \brief Class interface for user events (a subset of cl_event's). - * - * See Event for details about copy semantics, etc. - */ -class UserEvent : public Event -{ -public: - /*! \brief Constructs a user event on a given context. - * - * Wraps clCreateUserEvent(). - */ - UserEvent( - const Context& context, - cl_int * err = NULL) - { - cl_int error; - object_ = ::clCreateUserEvent( - context(), - &error); - - detail::errHandler(error, __CREATE_USER_EVENT_ERR); - if (err != NULL) { - *err = error; - } - } - - //! \brief Default constructor - initializes to NULL. - UserEvent() : Event() { } - - //! \brief Copy constructor - performs shallow copy. - UserEvent(const UserEvent& event) : Event(event) { } - - //! \brief Assignment Operator - performs shallow copy. - UserEvent& operator = (const UserEvent& rhs) - { - if (this != &rhs) { - Event::operator=(rhs); - } - return *this; - } - - /*! \brief Sets the execution status of a user event object. - * - * Wraps clSetUserEventStatus(). - */ - cl_int setStatus(cl_int status) - { - return detail::errHandler( - ::clSetUserEventStatus(object_,status), - __SET_USER_EVENT_STATUS_ERR); - } -}; -#endif - -/*! \brief Blocks the calling thread until every event specified is complete. - * - * Wraps clWaitForEvents(). - */ -inline static cl_int -WaitForEvents(const VECTOR_CLASS& events) -{ - return detail::errHandler( - ::clWaitForEvents( - (cl_uint) events.size(), (cl_event*)&events.front()), - __WAIT_FOR_EVENTS_ERR); -} - -/*! \brief Class interface for cl_mem. - * - * \note Copies of these objects are shallow, meaning that the copy will refer - * to the same underlying cl_mem as the original. For details, see - * clRetainMemObject() and clReleaseMemObject(). - * - * \see cl_mem - */ -class Memory : public detail::Wrapper -{ -public: - - /*! \brief Destructor. - * - * This calls clReleaseMemObject() on the value held by this instance. - */ - ~Memory() {} - - //! \brief Default constructor - initializes to NULL. - Memory() : detail::Wrapper() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * This calls clRetainMemObject() on the parameter's cl_mem. - */ - Memory(const Memory& memory) : detail::Wrapper(memory) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * This effectively transfers ownership of a refcount on the cl_mem - * into the new Memory object. - */ - __CL_EXPLICIT_CONSTRUCTORS Memory(const cl_mem& memory) : detail::Wrapper(memory) { } - - /*! \brief Assignment operator from Memory. - * - * This calls clRetainMemObject() on the parameter and clReleaseMemObject() - * on the previous value held by this instance. - */ - Memory& operator = (const Memory& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_mem - takes ownership. - * - * This effectively transfers ownership of a refcount on the rhs and calls - * clReleaseMemObject() on the value previously held by this instance. - */ - Memory& operator = (const cl_mem& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetMemObjectInfo(). - template - cl_int getInfo(cl_mem_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetMemObjectInfo, object_, name, param), - __GET_MEM_OBJECT_INFO_ERR); - } - - //! \brief Wrapper for clGetMemObjectInfo() that returns by value. - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_mem_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - -#if defined(CL_VERSION_1_1) - /*! \brief Registers a callback function to be called when the memory object - * is no longer needed. - * - * Wraps clSetMemObjectDestructorCallback(). - * - * Repeated calls to this function, for a given cl_mem value, will append - * to the list of functions called (in reverse order) when memory object's - * resources are freed and the memory object is deleted. - * - * \note - * The registered callbacks are associated with the underlying cl_mem - * value - not the Memory class instance. - */ - cl_int setDestructorCallback( - void (CL_CALLBACK * pfn_notify)(cl_mem, void *), - void * user_data = NULL) - { - return detail::errHandler( - ::clSetMemObjectDestructorCallback( - object_, - pfn_notify, - user_data), - __SET_MEM_OBJECT_DESTRUCTOR_CALLBACK_ERR); - } -#endif - -}; - -// Pre-declare copy functions -class Buffer; -template< typename IteratorType > -cl_int copy( IteratorType startIterator, IteratorType endIterator, cl::Buffer &buffer ); -template< typename IteratorType > -cl_int copy( const cl::Buffer &buffer, IteratorType startIterator, IteratorType endIterator ); -template< typename IteratorType > -cl_int copy( const CommandQueue &queue, IteratorType startIterator, IteratorType endIterator, cl::Buffer &buffer ); -template< typename IteratorType > -cl_int copy( const CommandQueue &queue, const cl::Buffer &buffer, IteratorType startIterator, IteratorType endIterator ); - - -/*! \brief Class interface for Buffer Memory Objects. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class Buffer : public Memory -{ -public: - - /*! \brief Constructs a Buffer in a specified context. - * - * Wraps clCreateBuffer(). - * - * \param host_ptr Storage to be used if the CL_MEM_USE_HOST_PTR flag was - * specified. Note alignment & exclusivity requirements. - */ - Buffer( - const Context& context, - cl_mem_flags flags, - ::size_t size, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - object_ = ::clCreateBuffer(context(), flags, size, host_ptr, &error); - - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } - - /*! \brief Constructs a Buffer in the default context. - * - * Wraps clCreateBuffer(). - * - * \param host_ptr Storage to be used if the CL_MEM_USE_HOST_PTR flag was - * specified. Note alignment & exclusivity requirements. - * - * \see Context::getDefault() - */ - Buffer( - cl_mem_flags flags, - ::size_t size, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - - Context context = Context::getDefault(err); - - object_ = ::clCreateBuffer(context(), flags, size, host_ptr, &error); - - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } - - /*! - * \brief Construct a Buffer from a host container via iterators. - * IteratorType must be random access. - * If useHostPtr is specified iterators must represent contiguous data. - */ - template< typename IteratorType > - Buffer( - IteratorType startIterator, - IteratorType endIterator, - bool readOnly, - bool useHostPtr = false, - cl_int* err = NULL) - { - typedef typename std::iterator_traits::value_type DataType; - cl_int error; - - cl_mem_flags flags = 0; - if( readOnly ) { - flags |= CL_MEM_READ_ONLY; - } - else { - flags |= CL_MEM_READ_WRITE; - } - if( useHostPtr ) { - flags |= CL_MEM_USE_HOST_PTR; - } - - ::size_t size = sizeof(DataType)*(endIterator - startIterator); - - Context context = Context::getDefault(err); - - if( useHostPtr ) { - object_ = ::clCreateBuffer(context(), flags, size, static_cast(&*startIterator), &error); - } else { - object_ = ::clCreateBuffer(context(), flags, size, 0, &error); - } - - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - - if( !useHostPtr ) { - error = cl::copy(startIterator, endIterator, *this); - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } - } - - /*! - * \brief Construct a Buffer from a host container via iterators using a specified context. - * IteratorType must be random access. - * If useHostPtr is specified iterators must represent contiguous data. - */ - template< typename IteratorType > - Buffer(const Context &context, IteratorType startIterator, IteratorType endIterator, - bool readOnly, bool useHostPtr = false, cl_int* err = NULL); - - //! \brief Default constructor - initializes to NULL. - Buffer() : Memory() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Buffer(const Buffer& buffer) : Memory(buffer) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Buffer(const cl_mem& buffer) : Memory(buffer) { } - - /*! \brief Assignment from Buffer - performs shallow copy. - * - * See Memory for further details. - */ - Buffer& operator = (const Buffer& rhs) - { - if (this != &rhs) { - Memory::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Buffer& operator = (const cl_mem& rhs) - { - Memory::operator=(rhs); - return *this; - } - -#if defined(CL_VERSION_1_1) - /*! \brief Creates a new buffer object from this. - * - * Wraps clCreateSubBuffer(). - */ - Buffer createSubBuffer( - cl_mem_flags flags, - cl_buffer_create_type buffer_create_type, - const void * buffer_create_info, - cl_int * err = NULL) - { - Buffer result; - cl_int error; - result.object_ = ::clCreateSubBuffer( - object_, - flags, - buffer_create_type, - buffer_create_info, - &error); - - detail::errHandler(error, __CREATE_SUBBUFFER_ERR); - if (err != NULL) { - *err = error; - } - - return result; - } -#endif -}; - -#if defined (USE_DX_INTEROP) -/*! \brief Class interface for creating OpenCL buffers from ID3D10Buffer's. - * - * This is provided to facilitate interoperability with Direct3D. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class BufferD3D10 : public Buffer -{ -public: - typedef CL_API_ENTRY cl_mem (CL_API_CALL *PFN_clCreateFromD3D10BufferKHR)( - cl_context context, cl_mem_flags flags, ID3D10Buffer* buffer, - cl_int* errcode_ret); - - /*! \brief Constructs a BufferD3D10, in a specified context, from a - * given ID3D10Buffer. - * - * Wraps clCreateFromD3D10BufferKHR(). - */ - BufferD3D10( - const Context& context, - cl_mem_flags flags, - ID3D10Buffer* bufobj, - cl_int * err = NULL) - { - static PFN_clCreateFromD3D10BufferKHR pfn_clCreateFromD3D10BufferKHR = NULL; - -#if defined(CL_VERSION_1_2) - vector props = context.getInfo(); - cl_platform platform = -1; - for( int i = 0; i < props.size(); ++i ) { - if( props[i] == CL_CONTEXT_PLATFORM ) { - platform = props[i+1]; - } - } - __INIT_CL_EXT_FCN_PTR_PLATFORM(platform, clCreateFromD3D10BufferKHR); -#endif -#if defined(CL_VERSION_1_1) - __INIT_CL_EXT_FCN_PTR(clCreateFromD3D10BufferKHR); -#endif - - cl_int error; - object_ = pfn_clCreateFromD3D10BufferKHR( - context(), - flags, - bufobj, - &error); - - detail::errHandler(error, __CREATE_GL_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } - - //! \brief Default constructor - initializes to NULL. - BufferD3D10() : Buffer() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - BufferD3D10(const BufferD3D10& buffer) : Buffer(buffer) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS BufferD3D10(const cl_mem& buffer) : Buffer(buffer) { } - - /*! \brief Assignment from BufferD3D10 - performs shallow copy. - * - * See Memory for further details. - */ - BufferD3D10& operator = (const BufferD3D10& rhs) - { - if (this != &rhs) { - Buffer::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - BufferD3D10& operator = (const cl_mem& rhs) - { - Buffer::operator=(rhs); - return *this; - } -}; -#endif - -/*! \brief Class interface for GL Buffer Memory Objects. - * - * This is provided to facilitate interoperability with OpenGL. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class BufferGL : public Buffer -{ -public: - /*! \brief Constructs a BufferGL in a specified context, from a given - * GL buffer. - * - * Wraps clCreateFromGLBuffer(). - */ - BufferGL( - const Context& context, - cl_mem_flags flags, - GLuint bufobj, - cl_int * err = NULL) - { - cl_int error; - object_ = ::clCreateFromGLBuffer( - context(), - flags, - bufobj, - &error); - - detail::errHandler(error, __CREATE_GL_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } - - //! \brief Default constructor - initializes to NULL. - BufferGL() : Buffer() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - BufferGL(const BufferGL& buffer) : Buffer(buffer) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS BufferGL(const cl_mem& buffer) : Buffer(buffer) { } - - /*! \brief Assignment from BufferGL - performs shallow copy. - * - * See Memory for further details. - */ - BufferGL& operator = (const BufferGL& rhs) - { - if (this != &rhs) { - Buffer::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - BufferGL& operator = (const cl_mem& rhs) - { - Buffer::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetGLObjectInfo(). - cl_int getObjectInfo( - cl_gl_object_type *type, - GLuint * gl_object_name) - { - return detail::errHandler( - ::clGetGLObjectInfo(object_,type,gl_object_name), - __GET_GL_OBJECT_INFO_ERR); - } -}; - -/*! \brief Class interface for GL Render Buffer Memory Objects. - * - * This is provided to facilitate interoperability with OpenGL. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class BufferRenderGL : public Buffer -{ -public: - /*! \brief Constructs a BufferRenderGL in a specified context, from a given - * GL Renderbuffer. - * - * Wraps clCreateFromGLRenderbuffer(). - */ - BufferRenderGL( - const Context& context, - cl_mem_flags flags, - GLuint bufobj, - cl_int * err = NULL) - { - cl_int error; - object_ = ::clCreateFromGLRenderbuffer( - context(), - flags, - bufobj, - &error); - - detail::errHandler(error, __CREATE_GL_RENDER_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } - - //! \brief Default constructor - initializes to NULL. - BufferRenderGL() : Buffer() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - BufferRenderGL(const BufferGL& buffer) : Buffer(buffer) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS BufferRenderGL(const cl_mem& buffer) : Buffer(buffer) { } - - /*! \brief Assignment from BufferGL - performs shallow copy. - * - * See Memory for further details. - */ - BufferRenderGL& operator = (const BufferRenderGL& rhs) - { - if (this != &rhs) { - Buffer::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - BufferRenderGL& operator = (const cl_mem& rhs) - { - Buffer::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetGLObjectInfo(). - cl_int getObjectInfo( - cl_gl_object_type *type, - GLuint * gl_object_name) - { - return detail::errHandler( - ::clGetGLObjectInfo(object_,type,gl_object_name), - __GET_GL_OBJECT_INFO_ERR); - } -}; - -/*! \brief C++ base class for Image Memory objects. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class Image : public Memory -{ -protected: - //! \brief Default constructor - initializes to NULL. - Image() : Memory() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Image(const Image& image) : Memory(image) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Image(const cl_mem& image) : Memory(image) { } - - /*! \brief Assignment from Image - performs shallow copy. - * - * See Memory for further details. - */ - Image& operator = (const Image& rhs) - { - if (this != &rhs) { - Memory::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Image& operator = (const cl_mem& rhs) - { - Memory::operator=(rhs); - return *this; - } - -public: - //! \brief Wrapper for clGetImageInfo(). - template - cl_int getImageInfo(cl_image_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetImageInfo, object_, name, param), - __GET_IMAGE_INFO_ERR); - } - - //! \brief Wrapper for clGetImageInfo() that returns by value. - template typename - detail::param_traits::param_type - getImageInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_image_info, name>::param_type param; - cl_int result = getImageInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } -}; - -#if defined(CL_VERSION_1_2) -/*! \brief Class interface for 1D Image Memory objects. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class Image1D : public Image -{ -public: - /*! \brief Constructs a 1D Image in a specified context. - * - * Wraps clCreateImage(). - */ - Image1D( - const Context& context, - cl_mem_flags flags, - ImageFormat format, - ::size_t width, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - cl_image_desc desc = - { - CL_MEM_OBJECT_IMAGE1D, - width, - 0, 0, 0, 0, 0, 0, 0, 0 - }; - object_ = ::clCreateImage( - context(), - flags, - &format, - &desc, - host_ptr, - &error); - - detail::errHandler(error, __CREATE_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - } - - //! \brief Default constructor - initializes to NULL. - Image1D() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Image1D(const Image1D& image1D) : Image(image1D) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Image1D(const cl_mem& image1D) : Image(image1D) { } - - /*! \brief Assignment from Image1D - performs shallow copy. - * - * See Memory for further details. - */ - Image1D& operator = (const Image1D& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Image1D& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; - -/*! \class Image1DBuffer - * \brief Image interface for 1D buffer images. - */ -class Image1DBuffer : public Image -{ -public: - Image1DBuffer( - const Context& context, - cl_mem_flags flags, - ImageFormat format, - ::size_t width, - const Buffer &buffer, - cl_int* err = NULL) - { - cl_int error; - cl_image_desc desc = - { - CL_MEM_OBJECT_IMAGE1D_BUFFER, - width, - 0, 0, 0, 0, 0, 0, 0, - buffer() - }; - object_ = ::clCreateImage( - context(), - flags, - &format, - &desc, - NULL, - &error); - - detail::errHandler(error, __CREATE_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - } - - Image1DBuffer() { } - - Image1DBuffer(const Image1DBuffer& image1D) : Image(image1D) { } - - __CL_EXPLICIT_CONSTRUCTORS Image1DBuffer(const cl_mem& image1D) : Image(image1D) { } - - Image1DBuffer& operator = (const Image1DBuffer& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - Image1DBuffer& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; - -/*! \class Image1DArray - * \brief Image interface for arrays of 1D images. - */ -class Image1DArray : public Image -{ -public: - Image1DArray( - const Context& context, - cl_mem_flags flags, - ImageFormat format, - ::size_t arraySize, - ::size_t width, - ::size_t rowPitch, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - cl_image_desc desc = - { - CL_MEM_OBJECT_IMAGE1D_ARRAY, - width, - 0, 0, // height, depth (unused) - arraySize, - rowPitch, - 0, 0, 0, 0 - }; - object_ = ::clCreateImage( - context(), - flags, - &format, - &desc, - host_ptr, - &error); - - detail::errHandler(error, __CREATE_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - } - - Image1DArray() { } - - Image1DArray(const Image1DArray& imageArray) : Image(imageArray) { } - - __CL_EXPLICIT_CONSTRUCTORS Image1DArray(const cl_mem& imageArray) : Image(imageArray) { } - - Image1DArray& operator = (const Image1DArray& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - Image1DArray& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; -#endif // #if defined(CL_VERSION_1_2) - - -/*! \brief Class interface for 2D Image Memory objects. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class Image2D : public Image -{ -public: - /*! \brief Constructs a 1D Image in a specified context. - * - * Wraps clCreateImage(). - */ - Image2D( - const Context& context, - cl_mem_flags flags, - ImageFormat format, - ::size_t width, - ::size_t height, - ::size_t row_pitch = 0, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - bool useCreateImage; - -#if defined(CL_VERSION_1_2) && defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - // Run-time decision based on the actual platform - { - cl_uint version = detail::getContextPlatformVersion(context()); - useCreateImage = (version >= 0x10002); // OpenCL 1.2 or above - } -#elif defined(CL_VERSION_1_2) - useCreateImage = true; -#else - useCreateImage = false; -#endif - -#if defined(CL_VERSION_1_2) - if (useCreateImage) - { - cl_image_desc desc = - { - CL_MEM_OBJECT_IMAGE2D, - width, - height, - 0, 0, // depth, array size (unused) - row_pitch, - 0, 0, 0, 0 - }; - object_ = ::clCreateImage( - context(), - flags, - &format, - &desc, - host_ptr, - &error); - - detail::errHandler(error, __CREATE_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - } -#endif // #if defined(CL_VERSION_1_2) -#if !defined(CL_VERSION_1_2) || defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - if (!useCreateImage) - { - object_ = ::clCreateImage2D( - context(), flags,&format, width, height, row_pitch, host_ptr, &error); - - detail::errHandler(error, __CREATE_IMAGE2D_ERR); - if (err != NULL) { - *err = error; - } - } -#endif // #if !defined(CL_VERSION_1_2) || defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - } - - //! \brief Default constructor - initializes to NULL. - Image2D() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Image2D(const Image2D& image2D) : Image(image2D) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Image2D(const cl_mem& image2D) : Image(image2D) { } - - /*! \brief Assignment from Image2D - performs shallow copy. - * - * See Memory for further details. - */ - Image2D& operator = (const Image2D& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Image2D& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; - - -#if !defined(CL_VERSION_1_2) -/*! \brief Class interface for GL 2D Image Memory objects. - * - * This is provided to facilitate interoperability with OpenGL. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - * \note Deprecated for OpenCL 1.2. Please use ImageGL instead. - */ -class CL_EXT_PREFIX__VERSION_1_1_DEPRECATED Image2DGL CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED : public Image2D -{ -public: - /*! \brief Constructs an Image2DGL in a specified context, from a given - * GL Texture. - * - * Wraps clCreateFromGLTexture2D(). - */ - Image2DGL( - const Context& context, - cl_mem_flags flags, - GLenum target, - GLint miplevel, - GLuint texobj, - cl_int * err = NULL) - { - cl_int error; - object_ = ::clCreateFromGLTexture2D( - context(), - flags, - target, - miplevel, - texobj, - &error); - - detail::errHandler(error, __CREATE_GL_TEXTURE_2D_ERR); - if (err != NULL) { - *err = error; - } - - } - - //! \brief Default constructor - initializes to NULL. - Image2DGL() : Image2D() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Image2DGL(const Image2DGL& image) : Image2D(image) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Image2DGL(const cl_mem& image) : Image2D(image) { } - - /*! \brief Assignment from Image2DGL - performs shallow copy. - * - * See Memory for further details. - */ - Image2DGL& operator = (const Image2DGL& rhs) - { - if (this != &rhs) { - Image2D::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Image2DGL& operator = (const cl_mem& rhs) - { - Image2D::operator=(rhs); - return *this; - } -}; -#endif // #if !defined(CL_VERSION_1_2) - -#if defined(CL_VERSION_1_2) -/*! \class Image2DArray - * \brief Image interface for arrays of 2D images. - */ -class Image2DArray : public Image -{ -public: - Image2DArray( - const Context& context, - cl_mem_flags flags, - ImageFormat format, - ::size_t arraySize, - ::size_t width, - ::size_t height, - ::size_t rowPitch, - ::size_t slicePitch, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - cl_image_desc desc = - { - CL_MEM_OBJECT_IMAGE2D_ARRAY, - width, - height, - 0, // depth (unused) - arraySize, - rowPitch, - slicePitch, - 0, 0, 0 - }; - object_ = ::clCreateImage( - context(), - flags, - &format, - &desc, - host_ptr, - &error); - - detail::errHandler(error, __CREATE_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - } - - Image2DArray() { } - - Image2DArray(const Image2DArray& imageArray) : Image(imageArray) { } - - __CL_EXPLICIT_CONSTRUCTORS Image2DArray(const cl_mem& imageArray) : Image(imageArray) { } - - Image2DArray& operator = (const Image2DArray& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - Image2DArray& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; -#endif // #if defined(CL_VERSION_1_2) - -/*! \brief Class interface for 3D Image Memory objects. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class Image3D : public Image -{ -public: - /*! \brief Constructs a 3D Image in a specified context. - * - * Wraps clCreateImage(). - */ - Image3D( - const Context& context, - cl_mem_flags flags, - ImageFormat format, - ::size_t width, - ::size_t height, - ::size_t depth, - ::size_t row_pitch = 0, - ::size_t slice_pitch = 0, - void* host_ptr = NULL, - cl_int* err = NULL) - { - cl_int error; - bool useCreateImage; - -#if defined(CL_VERSION_1_2) && defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - // Run-time decision based on the actual platform - { - cl_uint version = detail::getContextPlatformVersion(context()); - useCreateImage = (version >= 0x10002); // OpenCL 1.2 or above - } -#elif defined(CL_VERSION_1_2) - useCreateImage = true; -#else - useCreateImage = false; -#endif - -#if defined(CL_VERSION_1_2) - if (useCreateImage) - { - cl_image_desc desc = - { - CL_MEM_OBJECT_IMAGE3D, - width, - height, - depth, - 0, // array size (unused) - row_pitch, - slice_pitch, - 0, 0, 0 - }; - object_ = ::clCreateImage( - context(), - flags, - &format, - &desc, - host_ptr, - &error); - - detail::errHandler(error, __CREATE_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - } -#endif // #if defined(CL_VERSION_1_2) -#if !defined(CL_VERSION_1_2) || defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - if (!useCreateImage) - { - object_ = ::clCreateImage3D( - context(), flags, &format, width, height, depth, row_pitch, - slice_pitch, host_ptr, &error); - - detail::errHandler(error, __CREATE_IMAGE3D_ERR); - if (err != NULL) { - *err = error; - } - } -#endif // #if !defined(CL_VERSION_1_2) || defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) - } - - //! \brief Default constructor - initializes to NULL. - Image3D() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Image3D(const Image3D& image3D) : Image(image3D) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Image3D(const cl_mem& image3D) : Image(image3D) { } - - /*! \brief Assignment from Image3D - performs shallow copy. - * - * See Memory for further details. - */ - Image3D& operator = (const Image3D& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Image3D& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; - -#if !defined(CL_VERSION_1_2) -/*! \brief Class interface for GL 3D Image Memory objects. - * - * This is provided to facilitate interoperability with OpenGL. - * - * See Memory for details about copy semantics, etc. - * - * \see Memory - */ -class Image3DGL : public Image3D -{ -public: - /*! \brief Constructs an Image3DGL in a specified context, from a given - * GL Texture. - * - * Wraps clCreateFromGLTexture3D(). - */ - Image3DGL( - const Context& context, - cl_mem_flags flags, - GLenum target, - GLint miplevel, - GLuint texobj, - cl_int * err = NULL) - { - cl_int error; - object_ = ::clCreateFromGLTexture3D( - context(), - flags, - target, - miplevel, - texobj, - &error); - - detail::errHandler(error, __CREATE_GL_TEXTURE_3D_ERR); - if (err != NULL) { - *err = error; - } - } - - //! \brief Default constructor - initializes to NULL. - Image3DGL() : Image3D() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * See Memory for further details. - */ - Image3DGL(const Image3DGL& image) : Image3D(image) { } - - /*! \brief Constructor from cl_mem - takes ownership. - * - * See Memory for further details. - */ - __CL_EXPLICIT_CONSTRUCTORS Image3DGL(const cl_mem& image) : Image3D(image) { } - - /*! \brief Assignment from Image3DGL - performs shallow copy. - * - * See Memory for further details. - */ - Image3DGL& operator = (const Image3DGL& rhs) - { - if (this != &rhs) { - Image3D::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment from cl_mem - performs shallow copy. - * - * See Memory for further details. - */ - Image3DGL& operator = (const cl_mem& rhs) - { - Image3D::operator=(rhs); - return *this; - } -}; -#endif // #if !defined(CL_VERSION_1_2) - -#if defined(CL_VERSION_1_2) -/*! \class ImageGL - * \brief general image interface for GL interop. - * We abstract the 2D and 3D GL images into a single instance here - * that wraps all GL sourced images on the grounds that setup information - * was performed by OpenCL anyway. - */ -class ImageGL : public Image -{ -public: - ImageGL( - const Context& context, - cl_mem_flags flags, - GLenum target, - GLint miplevel, - GLuint texobj, - cl_int * err = NULL) - { - cl_int error; - object_ = ::clCreateFromGLTexture( - context(), - flags, - target, - miplevel, - texobj, - &error); - - detail::errHandler(error, __CREATE_GL_TEXTURE_ERR); - if (err != NULL) { - *err = error; - } - } - - ImageGL() : Image() { } - - ImageGL(const ImageGL& image) : Image(image) { } - - __CL_EXPLICIT_CONSTRUCTORS ImageGL(const cl_mem& image) : Image(image) { } - - ImageGL& operator = (const ImageGL& rhs) - { - if (this != &rhs) { - Image::operator=(rhs); - } - return *this; - } - - ImageGL& operator = (const cl_mem& rhs) - { - Image::operator=(rhs); - return *this; - } -}; -#endif // #if defined(CL_VERSION_1_2) - -/*! \brief Class interface for cl_sampler. - * - * \note Copies of these objects are shallow, meaning that the copy will refer - * to the same underlying cl_sampler as the original. For details, see - * clRetainSampler() and clReleaseSampler(). - * - * \see cl_sampler - */ -class Sampler : public detail::Wrapper -{ -public: - /*! \brief Destructor. - * - * This calls clReleaseSampler() on the value held by this instance. - */ - ~Sampler() { } - - //! \brief Default constructor - initializes to NULL. - Sampler() { } - - /*! \brief Constructs a Sampler in a specified context. - * - * Wraps clCreateSampler(). - */ - Sampler( - const Context& context, - cl_bool normalized_coords, - cl_addressing_mode addressing_mode, - cl_filter_mode filter_mode, - cl_int* err = NULL) - { - cl_int error; - object_ = ::clCreateSampler( - context(), - normalized_coords, - addressing_mode, - filter_mode, - &error); - - detail::errHandler(error, __CREATE_SAMPLER_ERR); - if (err != NULL) { - *err = error; - } - } - - /*! \brief Copy constructor - performs shallow copy. - * - * This calls clRetainSampler() on the parameter's cl_sampler. - */ - Sampler(const Sampler& sampler) : detail::Wrapper(sampler) { } - - /*! \brief Constructor from cl_sampler - takes ownership. - * - * This effectively transfers ownership of a refcount on the cl_sampler - * into the new Sampler object. - */ - Sampler(const cl_sampler& sampler) : detail::Wrapper(sampler) { } - - /*! \brief Assignment operator from Sampler. - * - * This calls clRetainSampler() on the parameter and clReleaseSampler() - * on the previous value held by this instance. - */ - Sampler& operator = (const Sampler& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_sampler - takes ownership. - * - * This effectively transfers ownership of a refcount on the rhs and calls - * clReleaseSampler() on the value previously held by this instance. - */ - Sampler& operator = (const cl_sampler& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - //! \brief Wrapper for clGetSamplerInfo(). - template - cl_int getInfo(cl_sampler_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetSamplerInfo, object_, name, param), - __GET_SAMPLER_INFO_ERR); - } - - //! \brief Wrapper for clGetSamplerInfo() that returns by value. - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_sampler_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } -}; - -class Program; -class CommandQueue; -class Kernel; - -//! \brief Class interface for specifying NDRange values. -class NDRange -{ -private: - size_t<3> sizes_; - cl_uint dimensions_; - -public: - //! \brief Default constructor - resulting range has zero dimensions. - NDRange() - : dimensions_(0) - { } - - //! \brief Constructs one-dimensional range. - NDRange(::size_t size0) - : dimensions_(1) - { - sizes_[0] = size0; - } - - //! \brief Constructs two-dimensional range. - NDRange(::size_t size0, ::size_t size1) - : dimensions_(2) - { - sizes_[0] = size0; - sizes_[1] = size1; - } - - //! \brief Constructs three-dimensional range. - NDRange(::size_t size0, ::size_t size1, ::size_t size2) - : dimensions_(3) - { - sizes_[0] = size0; - sizes_[1] = size1; - sizes_[2] = size2; - } - - /*! \brief Conversion operator to const ::size_t *. - * - * \returns a pointer to the size of the first dimension. - */ - operator const ::size_t*() const { - return (const ::size_t*) sizes_; - } - - //! \brief Queries the number of dimensions in the range. - ::size_t dimensions() const { return dimensions_; } -}; - -//! \brief A zero-dimensional range. -static const NDRange NullRange; - -//! \brief Local address wrapper for use with Kernel::setArg -struct LocalSpaceArg -{ - ::size_t size_; -}; - -namespace detail { - -template -struct KernelArgumentHandler -{ - static ::size_t size(const T&) { return sizeof(T); } - static T* ptr(T& value) { return &value; } -}; - -template <> -struct KernelArgumentHandler -{ - static ::size_t size(const LocalSpaceArg& value) { return value.size_; } - static void* ptr(LocalSpaceArg&) { return NULL; } -}; - -} -//! \endcond - -/*! __local - * \brief Helper function for generating LocalSpaceArg objects. - * Deprecated. Replaced with Local. - */ -inline CL_EXT_PREFIX__VERSION_1_1_DEPRECATED LocalSpaceArg -__local(::size_t size) CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED; -inline LocalSpaceArg -__local(::size_t size) -{ - LocalSpaceArg ret = { size }; - return ret; -} - -/*! Local - * \brief Helper function for generating LocalSpaceArg objects. - */ -inline LocalSpaceArg -Local(::size_t size) -{ - LocalSpaceArg ret = { size }; - return ret; -} - -//class KernelFunctor; - -/*! \brief Class interface for cl_kernel. - * - * \note Copies of these objects are shallow, meaning that the copy will refer - * to the same underlying cl_kernel as the original. For details, see - * clRetainKernel() and clReleaseKernel(). - * - * \see cl_kernel - */ -class Kernel : public detail::Wrapper -{ -public: - inline Kernel(const Program& program, const char* name, cl_int* err = NULL); - - /*! \brief Destructor. - * - * This calls clReleaseKernel() on the value held by this instance. - */ - ~Kernel() { } - - //! \brief Default constructor - initializes to NULL. - Kernel() { } - - /*! \brief Copy constructor - performs shallow copy. - * - * This calls clRetainKernel() on the parameter's cl_kernel. - */ - Kernel(const Kernel& kernel) : detail::Wrapper(kernel) { } - - /*! \brief Constructor from cl_kernel - takes ownership. - * - * This effectively transfers ownership of a refcount on the cl_kernel - * into the new Kernel object. - */ - __CL_EXPLICIT_CONSTRUCTORS Kernel(const cl_kernel& kernel) : detail::Wrapper(kernel) { } - - /*! \brief Assignment operator from Kernel. - * - * This calls clRetainKernel() on the parameter and clReleaseKernel() - * on the previous value held by this instance. - */ - Kernel& operator = (const Kernel& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - /*! \brief Assignment operator from cl_kernel - takes ownership. - * - * This effectively transfers ownership of a refcount on the rhs and calls - * clReleaseKernel() on the value previously held by this instance. - */ - Kernel& operator = (const cl_kernel& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - template - cl_int getInfo(cl_kernel_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetKernelInfo, object_, name, param), - __GET_KERNEL_INFO_ERR); - } - - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_kernel_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - -#if defined(CL_VERSION_1_2) - template - cl_int getArgInfo(cl_uint argIndex, cl_kernel_arg_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetKernelArgInfo, object_, argIndex, name, param), - __GET_KERNEL_ARG_INFO_ERR); - } - - template typename - detail::param_traits::param_type - getArgInfo(cl_uint argIndex, cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_kernel_arg_info, name>::param_type param; - cl_int result = getArgInfo(argIndex, name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } -#endif // #if defined(CL_VERSION_1_2) - - template - cl_int getWorkGroupInfo( - const Device& device, cl_kernel_work_group_info name, T* param) const - { - return detail::errHandler( - detail::getInfo( - &::clGetKernelWorkGroupInfo, object_, device(), name, param), - __GET_KERNEL_WORK_GROUP_INFO_ERR); - } - - template typename - detail::param_traits::param_type - getWorkGroupInfo(const Device& device, cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_kernel_work_group_info, name>::param_type param; - cl_int result = getWorkGroupInfo(device, name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - template - cl_int setArg(cl_uint index, T value) - { - return detail::errHandler( - ::clSetKernelArg( - object_, - index, - detail::KernelArgumentHandler::size(value), - detail::KernelArgumentHandler::ptr(value)), - __SET_KERNEL_ARGS_ERR); - } - - cl_int setArg(cl_uint index, ::size_t size, void* argPtr) - { - return detail::errHandler( - ::clSetKernelArg(object_, index, size, argPtr), - __SET_KERNEL_ARGS_ERR); - } -}; - -/*! \class Program - * \brief Program interface that implements cl_program. - */ -class Program : public detail::Wrapper -{ -public: - typedef VECTOR_CLASS > Binaries; - typedef VECTOR_CLASS > Sources; - - Program( - const STRING_CLASS& source, - bool build = false, - cl_int* err = NULL) - { - cl_int error; - - const char * strings = source.c_str(); - const ::size_t length = source.size(); - - Context context = Context::getDefault(err); - - object_ = ::clCreateProgramWithSource( - context(), (cl_uint)1, &strings, &length, &error); - - detail::errHandler(error, __CREATE_PROGRAM_WITH_SOURCE_ERR); - - if (error == CL_SUCCESS && build) { - - error = ::clBuildProgram( - object_, - 0, - NULL, - "", - NULL, - NULL); - - detail::errHandler(error, __BUILD_PROGRAM_ERR); - } - - if (err != NULL) { - *err = error; - } - } - - Program( - const Context& context, - const STRING_CLASS& source, - bool build = false, - cl_int* err = NULL) - { - cl_int error; - - const char * strings = source.c_str(); - const ::size_t length = source.size(); - - object_ = ::clCreateProgramWithSource( - context(), (cl_uint)1, &strings, &length, &error); - - detail::errHandler(error, __CREATE_PROGRAM_WITH_SOURCE_ERR); - - if (error == CL_SUCCESS && build) { - - error = ::clBuildProgram( - object_, - 0, - NULL, - "", - NULL, - NULL); - - detail::errHandler(error, __BUILD_PROGRAM_ERR); - } - - if (err != NULL) { - *err = error; - } - } - - Program( - const Context& context, - const Sources& sources, - cl_int* err = NULL) - { - cl_int error; - - const ::size_t n = (::size_t)sources.size(); - ::size_t* lengths = (::size_t*) alloca(n * sizeof(::size_t)); - const char** strings = (const char**) alloca(n * sizeof(const char*)); - - for (::size_t i = 0; i < n; ++i) { - strings[i] = sources[(int)i].first; - lengths[i] = sources[(int)i].second; - } - - object_ = ::clCreateProgramWithSource( - context(), (cl_uint)n, strings, lengths, &error); - - detail::errHandler(error, __CREATE_PROGRAM_WITH_SOURCE_ERR); - if (err != NULL) { - *err = error; - } - } - - /** - * Construct a program object from a list of devices and a per-device list of binaries. - * \param context A valid OpenCL context in which to construct the program. - * \param devices A vector of OpenCL device objects for which the program will be created. - * \param binaries A vector of pairs of a pointer to a binary object and its length. - * \param binaryStatus An optional vector that on completion will be resized to - * match the size of binaries and filled with values to specify if each binary - * was successfully loaded. - * Set to CL_SUCCESS if the binary was successfully loaded. - * Set to CL_INVALID_VALUE if the length is 0 or the binary pointer is NULL. - * Set to CL_INVALID_BINARY if the binary provided is not valid for the matching device. - * \param err if non-NULL will be set to CL_SUCCESS on successful operation or one of the following errors: - * CL_INVALID_CONTEXT if context is not a valid context. - * CL_INVALID_VALUE if the length of devices is zero; or if the length of binaries does not match the length of devices; - * or if any entry in binaries is NULL or has length 0. - * CL_INVALID_DEVICE if OpenCL devices listed in devices are not in the list of devices associated with context. - * CL_INVALID_BINARY if an invalid program binary was encountered for any device. binaryStatus will return specific status for each device. - * CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host. - */ - Program( - const Context& context, - const VECTOR_CLASS& devices, - const Binaries& binaries, - VECTOR_CLASS* binaryStatus = NULL, - cl_int* err = NULL) - { - cl_int error; - - const ::size_t numDevices = devices.size(); - - // Catch size mismatch early and return - if(binaries.size() != numDevices) { - error = CL_INVALID_VALUE; - detail::errHandler(error, __CREATE_PROGRAM_WITH_BINARY_ERR); - if (err != NULL) { - *err = error; - } - return; - } - - ::size_t* lengths = (::size_t*) alloca(numDevices * sizeof(::size_t)); - const unsigned char** images = (const unsigned char**) alloca(numDevices * sizeof(const unsigned char**)); - - for (::size_t i = 0; i < numDevices; ++i) { - images[i] = (const unsigned char*)binaries[i].first; - lengths[i] = binaries[(int)i].second; - } - - cl_device_id* deviceIDs = (cl_device_id*) alloca(numDevices * sizeof(cl_device_id)); - for( ::size_t deviceIndex = 0; deviceIndex < numDevices; ++deviceIndex ) { - deviceIDs[deviceIndex] = (devices[deviceIndex])(); - } - - if(binaryStatus) { - binaryStatus->resize(numDevices); - } - - object_ = ::clCreateProgramWithBinary( - context(), (cl_uint) devices.size(), - deviceIDs, - lengths, images, binaryStatus != NULL - ? &binaryStatus->front() - : NULL, &error); - - detail::errHandler(error, __CREATE_PROGRAM_WITH_BINARY_ERR); - if (err != NULL) { - *err = error; - } - } - - -#if defined(CL_VERSION_1_2) - /** - * Create program using builtin kernels. - * \param kernelNames Semi-colon separated list of builtin kernel names - */ - Program( - const Context& context, - const VECTOR_CLASS& devices, - const STRING_CLASS& kernelNames, - cl_int* err = NULL) - { - cl_int error; - - - ::size_t numDevices = devices.size(); - cl_device_id* deviceIDs = (cl_device_id*) alloca(numDevices * sizeof(cl_device_id)); - for( ::size_t deviceIndex = 0; deviceIndex < numDevices; ++deviceIndex ) { - deviceIDs[deviceIndex] = (devices[deviceIndex])(); - } - - object_ = ::clCreateProgramWithBuiltInKernels( - context(), - (cl_uint) devices.size(), - deviceIDs, - kernelNames.c_str(), - &error); - - detail::errHandler(error, __CREATE_PROGRAM_WITH_BUILT_IN_KERNELS_ERR); - if (err != NULL) { - *err = error; - } - } -#endif // #if defined(CL_VERSION_1_2) - - Program() { } - - Program(const Program& program) : detail::Wrapper(program) { } - - __CL_EXPLICIT_CONSTRUCTORS Program(const cl_program& program) : detail::Wrapper(program) { } - - Program& operator = (const Program& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - Program& operator = (const cl_program& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - cl_int build( - const VECTOR_CLASS& devices, - const char* options = NULL, - void (CL_CALLBACK * notifyFptr)(cl_program, void *) = NULL, - void* data = NULL) const - { - ::size_t numDevices = devices.size(); - cl_device_id* deviceIDs = (cl_device_id*) alloca(numDevices * sizeof(cl_device_id)); - for( ::size_t deviceIndex = 0; deviceIndex < numDevices; ++deviceIndex ) { - deviceIDs[deviceIndex] = (devices[deviceIndex])(); - } - - return detail::errHandler( - ::clBuildProgram( - object_, - (cl_uint) - devices.size(), - deviceIDs, - options, - notifyFptr, - data), - __BUILD_PROGRAM_ERR); - } - - cl_int build( - const char* options = NULL, - void (CL_CALLBACK * notifyFptr)(cl_program, void *) = NULL, - void* data = NULL) const - { - return detail::errHandler( - ::clBuildProgram( - object_, - 0, - NULL, - options, - notifyFptr, - data), - __BUILD_PROGRAM_ERR); - } - -#if defined(CL_VERSION_1_2) - cl_int compile( - const char* options = NULL, - void (CL_CALLBACK * notifyFptr)(cl_program, void *) = NULL, - void* data = NULL) const - { - return detail::errHandler( - ::clCompileProgram( - object_, - 0, - NULL, - options, - 0, - NULL, - NULL, - notifyFptr, - data), - __COMPILE_PROGRAM_ERR); - } -#endif - - template - cl_int getInfo(cl_program_info name, T* param) const - { - return detail::errHandler( - detail::getInfo(&::clGetProgramInfo, object_, name, param), - __GET_PROGRAM_INFO_ERR); - } - - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_program_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - template - cl_int getBuildInfo( - const Device& device, cl_program_build_info name, T* param) const - { - return detail::errHandler( - detail::getInfo( - &::clGetProgramBuildInfo, object_, device(), name, param), - __GET_PROGRAM_BUILD_INFO_ERR); - } - - template typename - detail::param_traits::param_type - getBuildInfo(const Device& device, cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_program_build_info, name>::param_type param; - cl_int result = getBuildInfo(device, name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - cl_int createKernels(VECTOR_CLASS* kernels) - { - cl_uint numKernels; - cl_int err = ::clCreateKernelsInProgram(object_, 0, NULL, &numKernels); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_KERNELS_IN_PROGRAM_ERR); - } - - Kernel* value = (Kernel*) alloca(numKernels * sizeof(Kernel)); - err = ::clCreateKernelsInProgram( - object_, numKernels, (cl_kernel*) value, NULL); - if (err != CL_SUCCESS) { - return detail::errHandler(err, __CREATE_KERNELS_IN_PROGRAM_ERR); - } - - kernels->assign(&value[0], &value[numKernels]); - return CL_SUCCESS; - } -}; - -#if defined(CL_VERSION_1_2) -inline Program linkProgram( - Program input1, - Program input2, - const char* options = NULL, - void (CL_CALLBACK * notifyFptr)(cl_program, void *) = NULL, - void* data = NULL, - cl_int* err = NULL) -{ - cl_int err_local = CL_SUCCESS; - - cl_program programs[2] = { input1(), input2() }; - - Context ctx = input1.getInfo(); - - cl_program prog = ::clLinkProgram( - ctx(), - 0, - NULL, - options, - 2, - programs, - notifyFptr, - data, - &err_local); - - detail::errHandler(err_local,__COMPILE_PROGRAM_ERR); - if (err != NULL) { - *err = err_local; - } - - return Program(prog); -} - -inline Program linkProgram( - VECTOR_CLASS inputPrograms, - const char* options = NULL, - void (CL_CALLBACK * notifyFptr)(cl_program, void *) = NULL, - void* data = NULL, - cl_int* err = NULL) -{ - cl_int err_local = CL_SUCCESS; - - cl_program * programs = (cl_program*) alloca(inputPrograms.size() * sizeof(cl_program)); - - if (programs != NULL) { - for (unsigned int i = 0; i < inputPrograms.size(); i++) { - programs[i] = inputPrograms[i](); - } - } - - cl_program prog = ::clLinkProgram( - Context::getDefault()(), - 0, - NULL, - options, - (cl_uint)inputPrograms.size(), - programs, - notifyFptr, - data, - &err_local); - - detail::errHandler(err_local,__COMPILE_PROGRAM_ERR); - if (err != NULL) { - *err = err_local; - } - - return Program(prog); -} -#endif - -template<> -inline VECTOR_CLASS cl::Program::getInfo(cl_int* err) const -{ - VECTOR_CLASS< ::size_t> sizes = getInfo(); - VECTOR_CLASS binaries; - for (VECTOR_CLASS< ::size_t>::iterator s = sizes.begin(); s != sizes.end(); ++s) - { - char *ptr = NULL; - if (*s != 0) - ptr = new char[*s]; - binaries.push_back(ptr); - } - - cl_int result = getInfo(CL_PROGRAM_BINARIES, &binaries); - if (err != NULL) { - *err = result; - } - return binaries; -} - -inline Kernel::Kernel(const Program& program, const char* name, cl_int* err) -{ - cl_int error; - - object_ = ::clCreateKernel(program(), name, &error); - detail::errHandler(error, __CREATE_KERNEL_ERR); - - if (err != NULL) { - *err = error; - } - -} - -/*! \class CommandQueue - * \brief CommandQueue interface for cl_command_queue. - */ -class CommandQueue : public detail::Wrapper -{ -private: - static volatile int default_initialized_; - static CommandQueue default_; - static volatile cl_int default_error_; -public: - CommandQueue( - cl_command_queue_properties properties, - cl_int* err = NULL) - { - cl_int error; - - Context context = Context::getDefault(&error); - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - - if (error != CL_SUCCESS) { - if (err != NULL) { - *err = error; - } - } - else { - Device device = context.getInfo()[0]; - - object_ = ::clCreateCommandQueue( - context(), device(), properties, &error); - - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - if (err != NULL) { - *err = error; - } - } - } - /*! - * \brief Constructs a CommandQueue for an implementation defined device in the given context - */ - explicit CommandQueue( - const Context& context, - cl_command_queue_properties properties = 0, - cl_int* err = NULL) - { - cl_int error; - VECTOR_CLASS devices; - error = context.getInfo(CL_CONTEXT_DEVICES, &devices); - - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - - if (error != CL_SUCCESS) - { - if (err != NULL) { - *err = error; - } - return; - } - - object_ = ::clCreateCommandQueue(context(), devices[0](), properties, &error); - - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - - if (err != NULL) { - *err = error; - } - - } - - CommandQueue( - const Context& context, - const Device& device, - cl_command_queue_properties properties = 0, - cl_int* err = NULL) - { - cl_int error; - object_ = ::clCreateCommandQueue( - context(), device(), properties, &error); - - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - if (err != NULL) { - *err = error; - } - } - - static CommandQueue getDefault(cl_int * err = NULL) - { - int state = detail::compare_exchange( - &default_initialized_, - __DEFAULT_BEING_INITIALIZED, __DEFAULT_NOT_INITIALIZED); - - if (state & __DEFAULT_INITIALIZED) { - if (err != NULL) { - *err = default_error_; - } - return default_; - } - - if (state & __DEFAULT_BEING_INITIALIZED) { - // Assume writes will propagate eventually... - while(default_initialized_ != __DEFAULT_INITIALIZED) { - detail::fence(); - } - - if (err != NULL) { - *err = default_error_; - } - return default_; - } - - cl_int error; - - Context context = Context::getDefault(&error); - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - - if (error != CL_SUCCESS) { - if (err != NULL) { - *err = error; - } - } - else { - Device device = context.getInfo()[0]; - - default_ = CommandQueue(context, device, 0, &error); - - detail::errHandler(error, __CREATE_COMMAND_QUEUE_ERR); - if (err != NULL) { - *err = error; - } - } - - detail::fence(); - - default_error_ = error; - // Assume writes will propagate eventually... - default_initialized_ = __DEFAULT_INITIALIZED; - - detail::fence(); - - if (err != NULL) { - *err = default_error_; - } - return default_; - - } - - CommandQueue() { } - - CommandQueue(const CommandQueue& commandQueue) : detail::Wrapper(commandQueue) { } - - CommandQueue(const cl_command_queue& commandQueue) : detail::Wrapper(commandQueue) { } - - CommandQueue& operator = (const CommandQueue& rhs) - { - if (this != &rhs) { - detail::Wrapper::operator=(rhs); - } - return *this; - } - - CommandQueue& operator = (const cl_command_queue& rhs) - { - detail::Wrapper::operator=(rhs); - return *this; - } - - template - cl_int getInfo(cl_command_queue_info name, T* param) const - { - return detail::errHandler( - detail::getInfo( - &::clGetCommandQueueInfo, object_, name, param), - __GET_COMMAND_QUEUE_INFO_ERR); - } - - template typename - detail::param_traits::param_type - getInfo(cl_int* err = NULL) const - { - typename detail::param_traits< - detail::cl_command_queue_info, name>::param_type param; - cl_int result = getInfo(name, ¶m); - if (err != NULL) { - *err = result; - } - return param; - } - - cl_int enqueueReadBuffer( - const Buffer& buffer, - cl_bool blocking, - ::size_t offset, - ::size_t size, - void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueReadBuffer( - object_, buffer(), blocking, offset, size, - ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_READ_BUFFER_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueWriteBuffer( - const Buffer& buffer, - cl_bool blocking, - ::size_t offset, - ::size_t size, - const void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueWriteBuffer( - object_, buffer(), blocking, offset, size, - ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_WRITE_BUFFER_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueCopyBuffer( - const Buffer& src, - const Buffer& dst, - ::size_t src_offset, - ::size_t dst_offset, - ::size_t size, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueCopyBuffer( - object_, src(), dst(), src_offset, dst_offset, size, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQEUE_COPY_BUFFER_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueReadBufferRect( - const Buffer& buffer, - cl_bool blocking, - const size_t<3>& buffer_offset, - const size_t<3>& host_offset, - const size_t<3>& region, - ::size_t buffer_row_pitch, - ::size_t buffer_slice_pitch, - ::size_t host_row_pitch, - ::size_t host_slice_pitch, - void *ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueReadBufferRect( - object_, - buffer(), - blocking, - (const ::size_t *)buffer_offset, - (const ::size_t *)host_offset, - (const ::size_t *)region, - buffer_row_pitch, - buffer_slice_pitch, - host_row_pitch, - host_slice_pitch, - ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_READ_BUFFER_RECT_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueWriteBufferRect( - const Buffer& buffer, - cl_bool blocking, - const size_t<3>& buffer_offset, - const size_t<3>& host_offset, - const size_t<3>& region, - ::size_t buffer_row_pitch, - ::size_t buffer_slice_pitch, - ::size_t host_row_pitch, - ::size_t host_slice_pitch, - void *ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueWriteBufferRect( - object_, - buffer(), - blocking, - (const ::size_t *)buffer_offset, - (const ::size_t *)host_offset, - (const ::size_t *)region, - buffer_row_pitch, - buffer_slice_pitch, - host_row_pitch, - host_slice_pitch, - ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_WRITE_BUFFER_RECT_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueCopyBufferRect( - const Buffer& src, - const Buffer& dst, - const size_t<3>& src_origin, - const size_t<3>& dst_origin, - const size_t<3>& region, - ::size_t src_row_pitch, - ::size_t src_slice_pitch, - ::size_t dst_row_pitch, - ::size_t dst_slice_pitch, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueCopyBufferRect( - object_, - src(), - dst(), - (const ::size_t *)src_origin, - (const ::size_t *)dst_origin, - (const ::size_t *)region, - src_row_pitch, - src_slice_pitch, - dst_row_pitch, - dst_slice_pitch, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQEUE_COPY_BUFFER_RECT_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - -#if defined(CL_VERSION_1_2) - /** - * Enqueue a command to fill a buffer object with a pattern - * of a given size. The pattern is specified a as vector. - * \tparam PatternType The datatype of the pattern field. - * The pattern type must be an accepted OpenCL data type. - */ - template - cl_int enqueueFillBuffer( - const Buffer& buffer, - PatternType pattern, - ::size_t offset, - ::size_t size, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueFillBuffer( - object_, - buffer(), - static_cast(&pattern), - sizeof(PatternType), - offset, - size, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_FILL_BUFFER_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } -#endif // #if defined(CL_VERSION_1_2) - - cl_int enqueueReadImage( - const Image& image, - cl_bool blocking, - const size_t<3>& origin, - const size_t<3>& region, - ::size_t row_pitch, - ::size_t slice_pitch, - void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueReadImage( - object_, image(), blocking, (const ::size_t *) origin, - (const ::size_t *) region, row_pitch, slice_pitch, ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_READ_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueWriteImage( - const Image& image, - cl_bool blocking, - const size_t<3>& origin, - const size_t<3>& region, - ::size_t row_pitch, - ::size_t slice_pitch, - void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueWriteImage( - object_, image(), blocking, (const ::size_t *) origin, - (const ::size_t *) region, row_pitch, slice_pitch, ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_WRITE_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueCopyImage( - const Image& src, - const Image& dst, - const size_t<3>& src_origin, - const size_t<3>& dst_origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueCopyImage( - object_, src(), dst(), (const ::size_t *) src_origin, - (const ::size_t *)dst_origin, (const ::size_t *) region, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_COPY_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - -#if defined(CL_VERSION_1_2) - /** - * Enqueue a command to fill an image object with a specified color. - * \param fillColor is the color to use to fill the image. - * This is a four component RGBA floating-point color value if - * the image channel data type is not an unnormalized signed or - * unsigned data type. - */ - cl_int enqueueFillImage( - const Image& image, - cl_float4 fillColor, - const size_t<3>& origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueFillImage( - object_, - image(), - static_cast(&fillColor), - (const ::size_t *) origin, - (const ::size_t *) region, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_FILL_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - /** - * Enqueue a command to fill an image object with a specified color. - * \param fillColor is the color to use to fill the image. - * This is a four component RGBA signed integer color value if - * the image channel data type is an unnormalized signed integer - * type. - */ - cl_int enqueueFillImage( - const Image& image, - cl_int4 fillColor, - const size_t<3>& origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueFillImage( - object_, - image(), - static_cast(&fillColor), - (const ::size_t *) origin, - (const ::size_t *) region, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_FILL_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - /** - * Enqueue a command to fill an image object with a specified color. - * \param fillColor is the color to use to fill the image. - * This is a four component RGBA unsigned integer color value if - * the image channel data type is an unnormalized unsigned integer - * type. - */ - cl_int enqueueFillImage( - const Image& image, - cl_uint4 fillColor, - const size_t<3>& origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueFillImage( - object_, - image(), - static_cast(&fillColor), - (const ::size_t *) origin, - (const ::size_t *) region, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_FILL_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } -#endif // #if defined(CL_VERSION_1_2) - - cl_int enqueueCopyImageToBuffer( - const Image& src, - const Buffer& dst, - const size_t<3>& src_origin, - const size_t<3>& region, - ::size_t dst_offset, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueCopyImageToBuffer( - object_, src(), dst(), (const ::size_t *) src_origin, - (const ::size_t *) region, dst_offset, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_COPY_IMAGE_TO_BUFFER_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueCopyBufferToImage( - const Buffer& src, - const Image& dst, - ::size_t src_offset, - const size_t<3>& dst_origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueCopyBufferToImage( - object_, src(), dst(), src_offset, - (const ::size_t *) dst_origin, (const ::size_t *) region, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_COPY_BUFFER_TO_IMAGE_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - void* enqueueMapBuffer( - const Buffer& buffer, - cl_bool blocking, - cl_map_flags flags, - ::size_t offset, - ::size_t size, - const VECTOR_CLASS* events = NULL, - Event* event = NULL, - cl_int* err = NULL) const - { - cl_int error; - void * result = ::clEnqueueMapBuffer( - object_, buffer(), blocking, flags, offset, size, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (cl_event*) event, - &error); - - detail::errHandler(error, __ENQUEUE_MAP_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - return result; - } - - void* enqueueMapImage( - const Image& buffer, - cl_bool blocking, - cl_map_flags flags, - const size_t<3>& origin, - const size_t<3>& region, - ::size_t * row_pitch, - ::size_t * slice_pitch, - const VECTOR_CLASS* events = NULL, - Event* event = NULL, - cl_int* err = NULL) const - { - cl_int error; - void * result = ::clEnqueueMapImage( - object_, buffer(), blocking, flags, - (const ::size_t *) origin, (const ::size_t *) region, - row_pitch, slice_pitch, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (cl_event*) event, - &error); - - detail::errHandler(error, __ENQUEUE_MAP_IMAGE_ERR); - if (err != NULL) { - *err = error; - } - return result; - } - - cl_int enqueueUnmapMemObject( - const Memory& memory, - void* mapped_ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueUnmapMemObject( - object_, memory(), mapped_ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_UNMAP_MEM_OBJECT_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - -#if defined(CL_VERSION_1_2) - /** - * Enqueues a marker command which waits for either a list of events to complete, - * or all previously enqueued commands to complete. - * - * Enqueues a marker command which waits for either a list of events to complete, - * or if the list is empty it waits for all commands previously enqueued in command_queue - * to complete before it completes. This command returns an event which can be waited on, - * i.e. this event can be waited on to insure that all events either in the event_wait_list - * or all previously enqueued commands, queued before this command to command_queue, - * have completed. - */ - cl_int enqueueMarkerWithWaitList( - const VECTOR_CLASS *events = 0, - Event *event = 0) - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueMarkerWithWaitList( - object_, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_MARKER_WAIT_LIST_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - /** - * A synchronization point that enqueues a barrier operation. - * - * Enqueues a barrier command which waits for either a list of events to complete, - * or if the list is empty it waits for all commands previously enqueued in command_queue - * to complete before it completes. This command blocks command execution, that is, any - * following commands enqueued after it do not execute until it completes. This command - * returns an event which can be waited on, i.e. this event can be waited on to insure that - * all events either in the event_wait_list or all previously enqueued commands, queued - * before this command to command_queue, have completed. - */ - cl_int enqueueBarrierWithWaitList( - const VECTOR_CLASS *events = 0, - Event *event = 0) - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueBarrierWithWaitList( - object_, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_BARRIER_WAIT_LIST_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - /** - * Enqueues a command to indicate with which device a set of memory objects - * should be associated. - */ - cl_int enqueueMigrateMemObjects( - const VECTOR_CLASS &memObjects, - cl_mem_migration_flags flags, - const VECTOR_CLASS* events = NULL, - Event* event = NULL - ) - { - cl_event tmp; - - cl_mem* localMemObjects = static_cast(alloca(memObjects.size() * sizeof(cl_mem))); - for( int i = 0; i < (int)memObjects.size(); ++i ) { - localMemObjects[i] = memObjects[i](); - } - - - cl_int err = detail::errHandler( - ::clEnqueueMigrateMemObjects( - object_, - (cl_uint)memObjects.size(), - static_cast(localMemObjects), - flags, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_UNMAP_MEM_OBJECT_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } -#endif // #if defined(CL_VERSION_1_2) - - cl_int enqueueNDRangeKernel( - const Kernel& kernel, - const NDRange& offset, - const NDRange& global, - const NDRange& local = NullRange, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueNDRangeKernel( - object_, kernel(), (cl_uint) global.dimensions(), - offset.dimensions() != 0 ? (const ::size_t*) offset : NULL, - (const ::size_t*) global, - local.dimensions() != 0 ? (const ::size_t*) local : NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_NDRANGE_KERNEL_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueTask( - const Kernel& kernel, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueTask( - object_, kernel(), - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_TASK_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueNativeKernel( - void (CL_CALLBACK *userFptr)(void *), - std::pair args, - const VECTOR_CLASS* mem_objects = NULL, - const VECTOR_CLASS* mem_locs = NULL, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_mem * mems = (mem_objects != NULL && mem_objects->size() > 0) - ? (cl_mem*) alloca(mem_objects->size() * sizeof(cl_mem)) - : NULL; - - if (mems != NULL) { - for (unsigned int i = 0; i < mem_objects->size(); i++) { - mems[i] = ((*mem_objects)[i])(); - } - } - - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueNativeKernel( - object_, userFptr, args.first, args.second, - (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, - mems, - (mem_locs != NULL) ? (const void **) &mem_locs->front() : NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_NATIVE_KERNEL); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - -/** - * Deprecated APIs for 1.2 - */ -#if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) || (defined(CL_VERSION_1_1) && !defined(CL_VERSION_1_2)) - CL_EXT_PREFIX__VERSION_1_1_DEPRECATED - cl_int enqueueMarker(Event* event = NULL) const CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED - { - return detail::errHandler( - ::clEnqueueMarker(object_, (cl_event*) event), - __ENQUEUE_MARKER_ERR); - } - - CL_EXT_PREFIX__VERSION_1_1_DEPRECATED - cl_int enqueueWaitForEvents(const VECTOR_CLASS& events) const CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED - { - return detail::errHandler( - ::clEnqueueWaitForEvents( - object_, - (cl_uint) events.size(), - (const cl_event*) &events.front()), - __ENQUEUE_WAIT_FOR_EVENTS_ERR); - } -#endif // #if defined(CL_VERSION_1_1) - - cl_int enqueueAcquireGLObjects( - const VECTOR_CLASS* mem_objects = NULL, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueAcquireGLObjects( - object_, - (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, - (mem_objects != NULL) ? (const cl_mem *) &mem_objects->front(): NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_ACQUIRE_GL_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueReleaseGLObjects( - const VECTOR_CLASS* mem_objects = NULL, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueReleaseGLObjects( - object_, - (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, - (mem_objects != NULL) ? (const cl_mem *) &mem_objects->front(): NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_RELEASE_GL_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - -#if defined (USE_DX_INTEROP) -typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueAcquireD3D10ObjectsKHR)( - cl_command_queue command_queue, cl_uint num_objects, - const cl_mem* mem_objects, cl_uint num_events_in_wait_list, - const cl_event* event_wait_list, cl_event* event); -typedef CL_API_ENTRY cl_int (CL_API_CALL *PFN_clEnqueueReleaseD3D10ObjectsKHR)( - cl_command_queue command_queue, cl_uint num_objects, - const cl_mem* mem_objects, cl_uint num_events_in_wait_list, - const cl_event* event_wait_list, cl_event* event); - - cl_int enqueueAcquireD3D10Objects( - const VECTOR_CLASS* mem_objects = NULL, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - static PFN_clEnqueueAcquireD3D10ObjectsKHR pfn_clEnqueueAcquireD3D10ObjectsKHR = NULL; -#if defined(CL_VERSION_1_2) - cl_context context = getInfo(); - cl::Device device(getInfo()); - cl_platform_id platform = device.getInfo(); - __INIT_CL_EXT_FCN_PTR_PLATFORM(platform, clEnqueueAcquireD3D10ObjectsKHR); -#endif -#if defined(CL_VERSION_1_1) - __INIT_CL_EXT_FCN_PTR(clEnqueueAcquireD3D10ObjectsKHR); -#endif - - cl_event tmp; - cl_int err = detail::errHandler( - pfn_clEnqueueAcquireD3D10ObjectsKHR( - object_, - (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, - (mem_objects != NULL) ? (const cl_mem *) &mem_objects->front(): NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_ACQUIRE_GL_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } - - cl_int enqueueReleaseD3D10Objects( - const VECTOR_CLASS* mem_objects = NULL, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) const - { - static PFN_clEnqueueReleaseD3D10ObjectsKHR pfn_clEnqueueReleaseD3D10ObjectsKHR = NULL; -#if defined(CL_VERSION_1_2) - cl_context context = getInfo(); - cl::Device device(getInfo()); - cl_platform_id platform = device.getInfo(); - __INIT_CL_EXT_FCN_PTR_PLATFORM(platform, clEnqueueReleaseD3D10ObjectsKHR); -#endif // #if defined(CL_VERSION_1_2) -#if defined(CL_VERSION_1_1) - __INIT_CL_EXT_FCN_PTR(clEnqueueReleaseD3D10ObjectsKHR); -#endif // #if defined(CL_VERSION_1_1) - - cl_event tmp; - cl_int err = detail::errHandler( - pfn_clEnqueueReleaseD3D10ObjectsKHR( - object_, - (mem_objects != NULL) ? (cl_uint) mem_objects->size() : 0, - (mem_objects != NULL) ? (const cl_mem *) &mem_objects->front(): NULL, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_RELEASE_GL_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; - } -#endif - -/** - * Deprecated APIs for 1.2 - */ -#if defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS) || (defined(CL_VERSION_1_1) && !defined(CL_VERSION_1_2)) - CL_EXT_PREFIX__VERSION_1_1_DEPRECATED - cl_int enqueueBarrier() const CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED - { - return detail::errHandler( - ::clEnqueueBarrier(object_), - __ENQUEUE_BARRIER_ERR); - } -#endif // #if defined(CL_VERSION_1_1) - - cl_int flush() const - { - return detail::errHandler(::clFlush(object_), __FLUSH_ERR); - } - - cl_int finish() const - { - return detail::errHandler(::clFinish(object_), __FINISH_ERR); - } -}; - -#ifdef _WIN32 -__declspec(selectany) volatile int CommandQueue::default_initialized_ = __DEFAULT_NOT_INITIALIZED; -__declspec(selectany) CommandQueue CommandQueue::default_; -__declspec(selectany) volatile cl_int CommandQueue::default_error_ = CL_SUCCESS; -#else -__attribute__((weak)) volatile int CommandQueue::default_initialized_ = __DEFAULT_NOT_INITIALIZED; -__attribute__((weak)) CommandQueue CommandQueue::default_; -__attribute__((weak)) volatile cl_int CommandQueue::default_error_ = CL_SUCCESS; -#endif - -template< typename IteratorType > -Buffer::Buffer( - const Context &context, - IteratorType startIterator, - IteratorType endIterator, - bool readOnly, - bool useHostPtr, - cl_int* err) -{ - typedef typename std::iterator_traits::value_type DataType; - cl_int error; - - cl_mem_flags flags = 0; - if( readOnly ) { - flags |= CL_MEM_READ_ONLY; - } - else { - flags |= CL_MEM_READ_WRITE; - } - if( useHostPtr ) { - flags |= CL_MEM_USE_HOST_PTR; - } - - ::size_t size = sizeof(DataType)*(endIterator - startIterator); - - if( useHostPtr ) { - object_ = ::clCreateBuffer(context(), flags, size, static_cast(&*startIterator), &error); - } else { - object_ = ::clCreateBuffer(context(), flags, size, 0, &error); - } - - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - - if( !useHostPtr ) { - CommandQueue queue(context, 0, &error); - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - - error = cl::copy(queue, startIterator, endIterator, *this); - detail::errHandler(error, __CREATE_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - } -} - -inline cl_int enqueueReadBuffer( - const Buffer& buffer, - cl_bool blocking, - ::size_t offset, - ::size_t size, - void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueReadBuffer(buffer, blocking, offset, size, ptr, events, event); -} - -inline cl_int enqueueWriteBuffer( - const Buffer& buffer, - cl_bool blocking, - ::size_t offset, - ::size_t size, - const void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueWriteBuffer(buffer, blocking, offset, size, ptr, events, event); -} - -inline void* enqueueMapBuffer( - const Buffer& buffer, - cl_bool blocking, - cl_map_flags flags, - ::size_t offset, - ::size_t size, - const VECTOR_CLASS* events = NULL, - Event* event = NULL, - cl_int* err = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - detail::errHandler(error, __ENQUEUE_MAP_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - - void * result = ::clEnqueueMapBuffer( - queue(), buffer(), blocking, flags, offset, size, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (cl_event*) event, - &error); - - detail::errHandler(error, __ENQUEUE_MAP_BUFFER_ERR); - if (err != NULL) { - *err = error; - } - return result; -} - -inline cl_int enqueueUnmapMemObject( - const Memory& memory, - void* mapped_ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - detail::errHandler(error, __ENQUEUE_MAP_BUFFER_ERR); - if (error != CL_SUCCESS) { - return error; - } - - cl_event tmp; - cl_int err = detail::errHandler( - ::clEnqueueUnmapMemObject( - queue(), memory(), mapped_ptr, - (events != NULL) ? (cl_uint) events->size() : 0, - (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL, - (event != NULL) ? &tmp : NULL), - __ENQUEUE_UNMAP_MEM_OBJECT_ERR); - - if (event != NULL && err == CL_SUCCESS) - *event = tmp; - - return err; -} - -inline cl_int enqueueCopyBuffer( - const Buffer& src, - const Buffer& dst, - ::size_t src_offset, - ::size_t dst_offset, - ::size_t size, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueCopyBuffer(src, dst, src_offset, dst_offset, size, events, event); -} - -/** - * Blocking copy operation between iterators and a buffer. - * Host to Device. - * Uses default command queue. - */ -template< typename IteratorType > -inline cl_int copy( IteratorType startIterator, IteratorType endIterator, cl::Buffer &buffer ) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - if (error != CL_SUCCESS) - return error; - - return cl::copy(queue, startIterator, endIterator, buffer); -} - -/** - * Blocking copy operation between iterators and a buffer. - * Device to Host. - * Uses default command queue. - */ -template< typename IteratorType > -inline cl_int copy( const cl::Buffer &buffer, IteratorType startIterator, IteratorType endIterator ) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - if (error != CL_SUCCESS) - return error; - - return cl::copy(queue, buffer, startIterator, endIterator); -} - -/** - * Blocking copy operation between iterators and a buffer. - * Host to Device. - * Uses specified queue. - */ -template< typename IteratorType > -inline cl_int copy( const CommandQueue &queue, IteratorType startIterator, IteratorType endIterator, cl::Buffer &buffer ) -{ - typedef typename std::iterator_traits::value_type DataType; - cl_int error; - - ::size_t length = endIterator-startIterator; - ::size_t byteLength = length*sizeof(DataType); - - DataType *pointer = - static_cast(queue.enqueueMapBuffer(buffer, CL_TRUE, CL_MAP_WRITE, 0, byteLength, 0, 0, &error)); - // if exceptions enabled, enqueueMapBuffer will throw - if( error != CL_SUCCESS ) { - return error; - } -#if defined(_MSC_VER) - std::copy( - startIterator, - endIterator, - stdext::checked_array_iterator( - pointer, length)); -#else - std::copy(startIterator, endIterator, pointer); -#endif - Event endEvent; - error = queue.enqueueUnmapMemObject(buffer, pointer, 0, &endEvent); - // if exceptions enabled, enqueueUnmapMemObject will throw - if( error != CL_SUCCESS ) { - return error; - } - endEvent.wait(); - return CL_SUCCESS; -} - -/** - * Blocking copy operation between iterators and a buffer. - * Device to Host. - * Uses specified queue. - */ -template< typename IteratorType > -inline cl_int copy( const CommandQueue &queue, const cl::Buffer &buffer, IteratorType startIterator, IteratorType endIterator ) -{ - typedef typename std::iterator_traits::value_type DataType; - cl_int error; - - ::size_t length = endIterator-startIterator; - ::size_t byteLength = length*sizeof(DataType); - - DataType *pointer = - static_cast(queue.enqueueMapBuffer(buffer, CL_TRUE, CL_MAP_READ, 0, byteLength, 0, 0, &error)); - // if exceptions enabled, enqueueMapBuffer will throw - if( error != CL_SUCCESS ) { - return error; - } - std::copy(pointer, pointer + length, startIterator); - Event endEvent; - error = queue.enqueueUnmapMemObject(buffer, pointer, 0, &endEvent); - // if exceptions enabled, enqueueUnmapMemObject will throw - if( error != CL_SUCCESS ) { - return error; - } - endEvent.wait(); - return CL_SUCCESS; -} - -#if defined(CL_VERSION_1_1) -inline cl_int enqueueReadBufferRect( - const Buffer& buffer, - cl_bool blocking, - const size_t<3>& buffer_offset, - const size_t<3>& host_offset, - const size_t<3>& region, - ::size_t buffer_row_pitch, - ::size_t buffer_slice_pitch, - ::size_t host_row_pitch, - ::size_t host_slice_pitch, - void *ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueReadBufferRect( - buffer, - blocking, - buffer_offset, - host_offset, - region, - buffer_row_pitch, - buffer_slice_pitch, - host_row_pitch, - host_slice_pitch, - ptr, - events, - event); -} - -inline cl_int enqueueWriteBufferRect( - const Buffer& buffer, - cl_bool blocking, - const size_t<3>& buffer_offset, - const size_t<3>& host_offset, - const size_t<3>& region, - ::size_t buffer_row_pitch, - ::size_t buffer_slice_pitch, - ::size_t host_row_pitch, - ::size_t host_slice_pitch, - void *ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueWriteBufferRect( - buffer, - blocking, - buffer_offset, - host_offset, - region, - buffer_row_pitch, - buffer_slice_pitch, - host_row_pitch, - host_slice_pitch, - ptr, - events, - event); -} - -inline cl_int enqueueCopyBufferRect( - const Buffer& src, - const Buffer& dst, - const size_t<3>& src_origin, - const size_t<3>& dst_origin, - const size_t<3>& region, - ::size_t src_row_pitch, - ::size_t src_slice_pitch, - ::size_t dst_row_pitch, - ::size_t dst_slice_pitch, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueCopyBufferRect( - src, - dst, - src_origin, - dst_origin, - region, - src_row_pitch, - src_slice_pitch, - dst_row_pitch, - dst_slice_pitch, - events, - event); -} -#endif - -inline cl_int enqueueReadImage( - const Image& image, - cl_bool blocking, - const size_t<3>& origin, - const size_t<3>& region, - ::size_t row_pitch, - ::size_t slice_pitch, - void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueReadImage( - image, - blocking, - origin, - region, - row_pitch, - slice_pitch, - ptr, - events, - event); -} - -inline cl_int enqueueWriteImage( - const Image& image, - cl_bool blocking, - const size_t<3>& origin, - const size_t<3>& region, - ::size_t row_pitch, - ::size_t slice_pitch, - void* ptr, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueWriteImage( - image, - blocking, - origin, - region, - row_pitch, - slice_pitch, - ptr, - events, - event); -} - -inline cl_int enqueueCopyImage( - const Image& src, - const Image& dst, - const size_t<3>& src_origin, - const size_t<3>& dst_origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueCopyImage( - src, - dst, - src_origin, - dst_origin, - region, - events, - event); -} - -inline cl_int enqueueCopyImageToBuffer( - const Image& src, - const Buffer& dst, - const size_t<3>& src_origin, - const size_t<3>& region, - ::size_t dst_offset, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueCopyImageToBuffer( - src, - dst, - src_origin, - region, - dst_offset, - events, - event); -} - -inline cl_int enqueueCopyBufferToImage( - const Buffer& src, - const Image& dst, - ::size_t src_offset, - const size_t<3>& dst_origin, - const size_t<3>& region, - const VECTOR_CLASS* events = NULL, - Event* event = NULL) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.enqueueCopyBufferToImage( - src, - dst, - src_offset, - dst_origin, - region, - events, - event); -} - - -inline cl_int flush(void) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - return queue.flush(); -} - -inline cl_int finish(void) -{ - cl_int error; - CommandQueue queue = CommandQueue::getDefault(&error); - - if (error != CL_SUCCESS) { - return error; - } - - - return queue.finish(); -} - -// Kernel Functor support -// New interface as of September 2011 -// Requires the C++11 std::tr1::function (note do not support TR1) -// Visual Studio 2010 and GCC 4.2 - -struct EnqueueArgs -{ - CommandQueue queue_; - const NDRange offset_; - const NDRange global_; - const NDRange local_; - VECTOR_CLASS events_; - - EnqueueArgs(NDRange global) : - queue_(CommandQueue::getDefault()), - offset_(NullRange), - global_(global), - local_(NullRange) - { - - } - - EnqueueArgs(NDRange global, NDRange local) : - queue_(CommandQueue::getDefault()), - offset_(NullRange), - global_(global), - local_(local) - { - - } - - EnqueueArgs(NDRange offset, NDRange global, NDRange local) : - queue_(CommandQueue::getDefault()), - offset_(offset), - global_(global), - local_(local) - { - - } - - EnqueueArgs(Event e, NDRange global) : - queue_(CommandQueue::getDefault()), - offset_(NullRange), - global_(global), - local_(NullRange) - { - events_.push_back(e); - } - - EnqueueArgs(Event e, NDRange global, NDRange local) : - queue_(CommandQueue::getDefault()), - offset_(NullRange), - global_(global), - local_(local) - { - events_.push_back(e); - } - - EnqueueArgs(Event e, NDRange offset, NDRange global, NDRange local) : - queue_(CommandQueue::getDefault()), - offset_(offset), - global_(global), - local_(local) - { - events_.push_back(e); - } - - EnqueueArgs(const VECTOR_CLASS &events, NDRange global) : - queue_(CommandQueue::getDefault()), - offset_(NullRange), - global_(global), - local_(NullRange), - events_(events) - { - - } - - EnqueueArgs(const VECTOR_CLASS &events, NDRange global, NDRange local) : - queue_(CommandQueue::getDefault()), - offset_(NullRange), - global_(global), - local_(local), - events_(events) - { - - } - - EnqueueArgs(const VECTOR_CLASS &events, NDRange offset, NDRange global, NDRange local) : - queue_(CommandQueue::getDefault()), - offset_(offset), - global_(global), - local_(local), - events_(events) - { - - } - - EnqueueArgs(CommandQueue &queue, NDRange global) : - queue_(queue), - offset_(NullRange), - global_(global), - local_(NullRange) - { - - } - - EnqueueArgs(CommandQueue &queue, NDRange global, NDRange local) : - queue_(queue), - offset_(NullRange), - global_(global), - local_(local) - { - - } - - EnqueueArgs(CommandQueue &queue, NDRange offset, NDRange global, NDRange local) : - queue_(queue), - offset_(offset), - global_(global), - local_(local) - { - - } - - EnqueueArgs(CommandQueue &queue, Event e, NDRange global) : - queue_(queue), - offset_(NullRange), - global_(global), - local_(NullRange) - { - events_.push_back(e); - } - - EnqueueArgs(CommandQueue &queue, Event e, NDRange global, NDRange local) : - queue_(queue), - offset_(NullRange), - global_(global), - local_(local) - { - events_.push_back(e); - } - - EnqueueArgs(CommandQueue &queue, Event e, NDRange offset, NDRange global, NDRange local) : - queue_(queue), - offset_(offset), - global_(global), - local_(local) - { - events_.push_back(e); - } - - EnqueueArgs(CommandQueue &queue, const VECTOR_CLASS &events, NDRange global) : - queue_(queue), - offset_(NullRange), - global_(global), - local_(NullRange), - events_(events) - { - - } - - EnqueueArgs(CommandQueue &queue, const VECTOR_CLASS &events, NDRange global, NDRange local) : - queue_(queue), - offset_(NullRange), - global_(global), - local_(local), - events_(events) - { - - } - - EnqueueArgs(CommandQueue &queue, const VECTOR_CLASS &events, NDRange offset, NDRange global, NDRange local) : - queue_(queue), - offset_(offset), - global_(global), - local_(local), - events_(events) - { - - } -}; - -namespace detail { - -class NullType {}; - -template -struct SetArg -{ - static void set (Kernel kernel, T0 arg) - { - kernel.setArg(index, arg); - } -}; - -template -struct SetArg -{ - static void set (Kernel, NullType) - { - } -}; - -template < - typename T0, typename T1, typename T2, typename T3, - typename T4, typename T5, typename T6, typename T7, - typename T8, typename T9, typename T10, typename T11, - typename T12, typename T13, typename T14, typename T15, - typename T16, typename T17, typename T18, typename T19, - typename T20, typename T21, typename T22, typename T23, - typename T24, typename T25, typename T26, typename T27, - typename T28, typename T29, typename T30, typename T31 -> -class KernelFunctorGlobal -{ -private: - Kernel kernel_; - -public: - KernelFunctorGlobal( - Kernel kernel) : - kernel_(kernel) - {} - - KernelFunctorGlobal( - const Program& program, - const STRING_CLASS name, - cl_int * err = NULL) : - kernel_(program, name.c_str(), err) - {} - - Event operator() ( - const EnqueueArgs& args, - T0 t0, - T1 t1 = NullType(), - T2 t2 = NullType(), - T3 t3 = NullType(), - T4 t4 = NullType(), - T5 t5 = NullType(), - T6 t6 = NullType(), - T7 t7 = NullType(), - T8 t8 = NullType(), - T9 t9 = NullType(), - T10 t10 = NullType(), - T11 t11 = NullType(), - T12 t12 = NullType(), - T13 t13 = NullType(), - T14 t14 = NullType(), - T15 t15 = NullType(), - T16 t16 = NullType(), - T17 t17 = NullType(), - T18 t18 = NullType(), - T19 t19 = NullType(), - T20 t20 = NullType(), - T21 t21 = NullType(), - T22 t22 = NullType(), - T23 t23 = NullType(), - T24 t24 = NullType(), - T25 t25 = NullType(), - T26 t26 = NullType(), - T27 t27 = NullType(), - T28 t28 = NullType(), - T29 t29 = NullType(), - T30 t30 = NullType(), - T31 t31 = NullType() - ) - { - Event event; - SetArg<0, T0>::set(kernel_, t0); - SetArg<1, T1>::set(kernel_, t1); - SetArg<2, T2>::set(kernel_, t2); - SetArg<3, T3>::set(kernel_, t3); - SetArg<4, T4>::set(kernel_, t4); - SetArg<5, T5>::set(kernel_, t5); - SetArg<6, T6>::set(kernel_, t6); - SetArg<7, T7>::set(kernel_, t7); - SetArg<8, T8>::set(kernel_, t8); - SetArg<9, T9>::set(kernel_, t9); - SetArg<10, T10>::set(kernel_, t10); - SetArg<11, T11>::set(kernel_, t11); - SetArg<12, T12>::set(kernel_, t12); - SetArg<13, T13>::set(kernel_, t13); - SetArg<14, T14>::set(kernel_, t14); - SetArg<15, T15>::set(kernel_, t15); - SetArg<16, T16>::set(kernel_, t16); - SetArg<17, T17>::set(kernel_, t17); - SetArg<18, T18>::set(kernel_, t18); - SetArg<19, T19>::set(kernel_, t19); - SetArg<20, T20>::set(kernel_, t20); - SetArg<21, T21>::set(kernel_, t21); - SetArg<22, T22>::set(kernel_, t22); - SetArg<23, T23>::set(kernel_, t23); - SetArg<24, T24>::set(kernel_, t24); - SetArg<25, T25>::set(kernel_, t25); - SetArg<26, T26>::set(kernel_, t26); - SetArg<27, T27>::set(kernel_, t27); - SetArg<28, T28>::set(kernel_, t28); - SetArg<29, T29>::set(kernel_, t29); - SetArg<30, T30>::set(kernel_, t30); - SetArg<31, T31>::set(kernel_, t31); - - args.queue_.enqueueNDRangeKernel( - kernel_, - args.offset_, - args.global_, - args.local_, - &args.events_, - &event); - - return event; - } - -}; - -//------------------------------------------------------------------------------------------------------ - - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25, - typename T26, - typename T27, - typename T28, - typename T29, - typename T30, - typename T31> -struct functionImplementation_ -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - T30, - T31> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 32)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - T30, - T31); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25, - T26 arg26, - T27 arg27, - T28 arg28, - T29 arg29, - T30 arg30, - T31 arg31) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25, - arg26, - arg27, - arg28, - arg29, - arg30, - arg31); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25, - typename T26, - typename T27, - typename T28, - typename T29, - typename T30> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - T30, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - T30, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 31)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - T30); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25, - T26 arg26, - T27 arg27, - T28 arg28, - T29 arg29, - T30 arg30) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25, - arg26, - arg27, - arg28, - arg29, - arg30); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25, - typename T26, - typename T27, - typename T28, - typename T29> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 30)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - T29); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25, - T26 arg26, - T27 arg27, - T28 arg28, - T29 arg29) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25, - arg26, - arg27, - arg28, - arg29); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25, - typename T26, - typename T27, - typename T28> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 29)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - T28); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25, - T26 arg26, - T27 arg27, - T28 arg28) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25, - arg26, - arg27, - arg28); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25, - typename T26, - typename T27> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 28)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - T27); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25, - T26 arg26, - T27 arg27) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25, - arg26, - arg27); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25, - typename T26> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 27)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - T26); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25, - T26 arg26) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25, - arg26); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24, - typename T25> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 26)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - T25); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24, - T25 arg25) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24, - arg25); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23, - typename T24> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 25)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - T24); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23, - T24 arg24) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23, - arg24); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22, - typename T23> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 24)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - T23); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22, - T23 arg23) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22, - arg23); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21, - typename T22> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 23)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - T22); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21, - T22 arg22) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21, - arg22); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20, - typename T21> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 22)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - T21); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20, - T21 arg21) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20, - arg21); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19, - typename T20> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 21)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - T20); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19, - T20 arg20) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19, - arg20); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18, - typename T19> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 20)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - T19); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18, - T19 arg19) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18, - arg19); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17, - typename T18> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 19)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - T18); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17, - T18 arg18) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17, - arg18); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16, - typename T17> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 18)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - T17); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16, - T17 arg17) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16, - arg17); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15, - typename T16> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 17)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - T16); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15, - T16 arg16) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15, - arg16); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14, - typename T15> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 16)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - T15); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14, - T15 arg15) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14, - arg15); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13, - typename T14> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 15)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - T14); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13, - T14 arg14) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13, - arg14); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12, - typename T13> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 14)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - T13); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12, - T13 arg13) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12, - arg13); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11, - typename T12> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 13)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - T12); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11, - T12 arg12) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11, - arg12); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10, - typename T11> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 12)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - T11); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10, - T11 arg11) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10, - arg11); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9, - typename T10> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 11)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - T10); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9, - T10 arg10) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9, - arg10); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8, - typename T9> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 10)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - T9); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8, - T9 arg9) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8, - arg9); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7, - typename T8> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 9)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - T8); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7, - T8 arg8) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7, - arg8); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6, - typename T7> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 8)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6, - T7); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6, - T7 arg7) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6, - arg7); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5, - typename T6> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - T6, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - T6, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 7)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5, - T6); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5, - T6 arg6) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5, - arg6); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4, - typename T5> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - T5, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - T5, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 6)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4, - T5); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4, - T5 arg5) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4, - arg5); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3, - typename T4> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - T4, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - T4, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 5)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3, - T4); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3, - T4 arg4) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3, - arg4); - } - - -}; - -template< - typename T0, - typename T1, - typename T2, - typename T3> -struct functionImplementation_ -< T0, - T1, - T2, - T3, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - T3, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 4)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2, - T3); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2, - T3 arg3) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2, - arg3); - } - - -}; - -template< - typename T0, - typename T1, - typename T2> -struct functionImplementation_ -< T0, - T1, - T2, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - T2, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 3)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1, - T2); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1, - T2 arg2) - { - return functor_( - enqueueArgs, - arg0, - arg1, - arg2); - } - - -}; - -template< - typename T0, - typename T1> -struct functionImplementation_ -< T0, - T1, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - T1, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 2)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0, - T1); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0, - T1 arg1) - { - return functor_( - enqueueArgs, - arg0, - arg1); - } - - -}; - -template< - typename T0> -struct functionImplementation_ -< T0, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> -{ - typedef detail::KernelFunctorGlobal< - T0, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType, - NullType> FunctorType; - - FunctorType functor_; - - functionImplementation_(const FunctorType &functor) : - functor_(functor) - { - - #if (defined(_WIN32) && defined(_VARIADIC_MAX) && (_VARIADIC_MAX < 1)) - // Fail variadic expansion for dev11 - static_assert(0, "Visual Studio has a hard limit of argument count for a std::function expansion. Please define _VARIADIC_MAX to be 10. If you need more arguments than that VC12 and below cannot support it."); - #endif - - } - - //! \brief Return type of the functor - typedef Event result_type; - - //! \brief Function signature of kernel functor with no event dependency. - typedef Event type_( - const EnqueueArgs&, - T0); - - Event operator()( - const EnqueueArgs& enqueueArgs, - T0 arg0) - { - return functor_( - enqueueArgs, - arg0); - } - - -}; - - - - - -} // namespace detail - -//---------------------------------------------------------------------------------------------- - -template < - typename T0, typename T1 = detail::NullType, typename T2 = detail::NullType, - typename T3 = detail::NullType, typename T4 = detail::NullType, - typename T5 = detail::NullType, typename T6 = detail::NullType, - typename T7 = detail::NullType, typename T8 = detail::NullType, - typename T9 = detail::NullType, typename T10 = detail::NullType, - typename T11 = detail::NullType, typename T12 = detail::NullType, - typename T13 = detail::NullType, typename T14 = detail::NullType, - typename T15 = detail::NullType, typename T16 = detail::NullType, - typename T17 = detail::NullType, typename T18 = detail::NullType, - typename T19 = detail::NullType, typename T20 = detail::NullType, - typename T21 = detail::NullType, typename T22 = detail::NullType, - typename T23 = detail::NullType, typename T24 = detail::NullType, - typename T25 = detail::NullType, typename T26 = detail::NullType, - typename T27 = detail::NullType, typename T28 = detail::NullType, - typename T29 = detail::NullType, typename T30 = detail::NullType, - typename T31 = detail::NullType -> -struct make_kernel : - public detail::functionImplementation_< - T0, T1, T2, T3, - T4, T5, T6, T7, - T8, T9, T10, T11, - T12, T13, T14, T15, - T16, T17, T18, T19, - T20, T21, T22, T23, - T24, T25, T26, T27, - T28, T29, T30, T31 - > -{ -public: - typedef detail::KernelFunctorGlobal< - T0, T1, T2, T3, - T4, T5, T6, T7, - T8, T9, T10, T11, - T12, T13, T14, T15, - T16, T17, T18, T19, - T20, T21, T22, T23, - T24, T25, T26, T27, - T28, T29, T30, T31 - > FunctorType; - - make_kernel( - const Program& program, - const STRING_CLASS name, - cl_int * err = NULL) : - detail::functionImplementation_< - T0, T1, T2, T3, - T4, T5, T6, T7, - T8, T9, T10, T11, - T12, T13, T14, T15, - T16, T17, T18, T19, - T20, T21, T22, T23, - T24, T25, T26, T27, - T28, T29, T30, T31 - >( - FunctorType(program, name, err)) - {} - - make_kernel( - const Kernel kernel) : - detail::functionImplementation_< - T0, T1, T2, T3, - T4, T5, T6, T7, - T8, T9, T10, T11, - T12, T13, T14, T15, - T16, T17, T18, T19, - T20, T21, T22, T23, - T24, T25, T26, T27, - T28, T29, T30, T31 - >( - FunctorType(kernel)) - {} -}; - - -//---------------------------------------------------------------------------------------------------------------------- - -#undef __ERR_STR -#if !defined(__CL_USER_OVERRIDE_ERROR_STRINGS) -#undef __GET_DEVICE_INFO_ERR -#undef __GET_PLATFORM_INFO_ERR -#undef __GET_DEVICE_IDS_ERR -#undef __GET_CONTEXT_INFO_ERR -#undef __GET_EVENT_INFO_ERR -#undef __GET_EVENT_PROFILE_INFO_ERR -#undef __GET_MEM_OBJECT_INFO_ERR -#undef __GET_IMAGE_INFO_ERR -#undef __GET_SAMPLER_INFO_ERR -#undef __GET_KERNEL_INFO_ERR -#undef __GET_KERNEL_ARG_INFO_ERR -#undef __GET_KERNEL_WORK_GROUP_INFO_ERR -#undef __GET_PROGRAM_INFO_ERR -#undef __GET_PROGRAM_BUILD_INFO_ERR -#undef __GET_COMMAND_QUEUE_INFO_ERR - -#undef __CREATE_CONTEXT_ERR -#undef __CREATE_CONTEXT_FROM_TYPE_ERR -#undef __GET_SUPPORTED_IMAGE_FORMATS_ERR - -#undef __CREATE_BUFFER_ERR -#undef __CREATE_SUBBUFFER_ERR -#undef __CREATE_IMAGE2D_ERR -#undef __CREATE_IMAGE3D_ERR -#undef __CREATE_SAMPLER_ERR -#undef __SET_MEM_OBJECT_DESTRUCTOR_CALLBACK_ERR - -#undef __CREATE_USER_EVENT_ERR -#undef __SET_USER_EVENT_STATUS_ERR -#undef __SET_EVENT_CALLBACK_ERR -#undef __SET_PRINTF_CALLBACK_ERR - -#undef __WAIT_FOR_EVENTS_ERR - -#undef __CREATE_KERNEL_ERR -#undef __SET_KERNEL_ARGS_ERR -#undef __CREATE_PROGRAM_WITH_SOURCE_ERR -#undef __CREATE_PROGRAM_WITH_BINARY_ERR -#undef __CREATE_PROGRAM_WITH_BUILT_IN_KERNELS_ERR -#undef __BUILD_PROGRAM_ERR -#undef __CREATE_KERNELS_IN_PROGRAM_ERR - -#undef __CREATE_COMMAND_QUEUE_ERR -#undef __SET_COMMAND_QUEUE_PROPERTY_ERR -#undef __ENQUEUE_READ_BUFFER_ERR -#undef __ENQUEUE_WRITE_BUFFER_ERR -#undef __ENQUEUE_READ_BUFFER_RECT_ERR -#undef __ENQUEUE_WRITE_BUFFER_RECT_ERR -#undef __ENQEUE_COPY_BUFFER_ERR -#undef __ENQEUE_COPY_BUFFER_RECT_ERR -#undef __ENQUEUE_READ_IMAGE_ERR -#undef __ENQUEUE_WRITE_IMAGE_ERR -#undef __ENQUEUE_COPY_IMAGE_ERR -#undef __ENQUEUE_COPY_IMAGE_TO_BUFFER_ERR -#undef __ENQUEUE_COPY_BUFFER_TO_IMAGE_ERR -#undef __ENQUEUE_MAP_BUFFER_ERR -#undef __ENQUEUE_MAP_IMAGE_ERR -#undef __ENQUEUE_UNMAP_MEM_OBJECT_ERR -#undef __ENQUEUE_NDRANGE_KERNEL_ERR -#undef __ENQUEUE_TASK_ERR -#undef __ENQUEUE_NATIVE_KERNEL - -#undef __CL_EXPLICIT_CONSTRUCTORS - -#undef __UNLOAD_COMPILER_ERR -#endif //__CL_USER_OVERRIDE_ERROR_STRINGS - -#undef __CL_FUNCTION_TYPE - -// Extensions -/** - * Deprecated APIs for 1.2 - */ -#if defined(CL_VERSION_1_1) -#undef __INIT_CL_EXT_FCN_PTR -#endif // #if defined(CL_VERSION_1_1) -#undef __CREATE_SUB_DEVICES - -#if defined(USE_CL_DEVICE_FISSION) -#undef __PARAM_NAME_DEVICE_FISSION -#endif // USE_CL_DEVICE_FISSION - -#undef __DEFAULT_NOT_INITIALIZED -#undef __DEFAULT_BEING_INITIALIZED -#undef __DEFAULT_INITIALIZED - -} // namespace cl - -#ifdef _WIN32 -#pragma pop_macro("max") -#endif // _WIN32 - -#endif // CL_HPP_ diff --git a/examples/opencl/cl_helpers.h b/examples/opencl/cl_helpers.h index 04ed1c75..da79b06b 100644 --- a/examples/opencl/cl_helpers.h +++ b/examples/opencl/cl_helpers.h @@ -7,8 +7,18 @@ * http://arrayfire.com/licenses/BSD-3-Clause ********************************************************/ #include -#define __CL_ENABLE_EXCEPTIONS -#include + +#define CL_HPP_ENABLE_EXCEPTIONS +#define CL_HPP_MINIMUM_OPENCL_VERSION 120 +#define CL_HPP_TARGET_OPENCL_VERSION 120 + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#pragma GCC diagnostic ignored "-Wignored-attributes" +#include +#pragma GCC diagnostic pop + +#include using namespace cl; diff --git a/examples/opencl/fractal.cpp b/examples/opencl/fractal.cpp index 093663ad..a279e196 100644 --- a/examples/opencl/fractal.cpp +++ b/examples/opencl/fractal.cpp @@ -8,8 +8,8 @@ ********************************************************/ #include -#define __CL_ENABLE_EXCEPTIONS -#include +#include "cl_helpers.h" + #include #include #include @@ -17,7 +17,6 @@ #include #include #include -#include "cl_helpers.h" using namespace cl; using namespace std; @@ -94,7 +93,7 @@ void kernel(cl::Buffer& devOut, cl::CommandQueue& queue) kern = cl::Kernel(prog, "julia"); }); - auto juliaOp = cl::make_kernel(kern); + auto juliaOp = cl::KernelFunctor(kern); static const NDRange local(8, 8); NDRange global(local[0] * divup(DIMX, local[0]), diff --git a/examples/opencl/histogram.cpp b/examples/opencl/histogram.cpp index 7675ae58..db9d74bc 100644 --- a/examples/opencl/histogram.cpp +++ b/examples/opencl/histogram.cpp @@ -8,8 +8,8 @@ ********************************************************/ #include -#define __CL_ENABLE_EXCEPTIONS -#include +#include "cl_helpers.h" + #include #include #include @@ -18,7 +18,6 @@ #include #include #include -#include "cl_helpers.h" using namespace cl; using namespace std; diff --git a/examples/opencl/plot3.cpp b/examples/opencl/plot3.cpp index 73723b76..0bfb4f9b 100644 --- a/examples/opencl/plot3.cpp +++ b/examples/opencl/plot3.cpp @@ -8,8 +8,8 @@ ********************************************************/ #include -#define __CL_ENABLE_EXCEPTIONS -#include +#include "cl_helpers.h" + #include #include #include @@ -18,7 +18,6 @@ #include #include #include -#include "cl_helpers.h" const unsigned DIMX = 1000; const unsigned DIMY = 800; diff --git a/examples/opencl/plotting.cpp b/examples/opencl/plotting.cpp index 1ef7cf19..e454ac8e 100644 --- a/examples/opencl/plotting.cpp +++ b/examples/opencl/plotting.cpp @@ -8,8 +8,8 @@ ********************************************************/ #include -#define __CL_ENABLE_EXCEPTIONS -#include +#include "cl_helpers.h" + #include #include #include @@ -17,7 +17,6 @@ #include #include #include -#include "cl_helpers.h" using namespace cl; using namespace std; diff --git a/examples/opencl/surface.cpp b/examples/opencl/surface.cpp index 71ce5134..f54cb00f 100644 --- a/examples/opencl/surface.cpp +++ b/examples/opencl/surface.cpp @@ -8,8 +8,8 @@ ********************************************************/ #include -#define __CL_ENABLE_EXCEPTIONS -#include +#include "cl_helpers.h" + #include #include #include @@ -18,7 +18,6 @@ #include #include #include -#include "cl_helpers.h" static const float XMIN = -8.0f; static const float XMAX = 8.f; diff --git a/include/OpenCLCopy.hpp b/include/OpenCLCopy.hpp index 95b26302..9aa717fd 100644 --- a/include/OpenCLCopy.hpp +++ b/include/OpenCLCopy.hpp @@ -22,6 +22,9 @@ extern "C" { #ifdef __cplusplus +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" + namespace fg { @@ -68,6 +71,8 @@ void copy(const int resourceId, const ::size_t resourceSize, } +#pragma GCC diagnostic pop + #endif #endif //__OPENCL_DATA_COPY_H__ From 323ce7f871d143b9299ac3f44e5b65f0476c4f5f Mon Sep 17 00:00:00 2001 From: pradeep Date: Fri, 1 Jul 2016 20:43:11 +0530 Subject: [PATCH 15/16] Vendor Neutral Header with helper copy functions These copy functions can be used to transfer data compute backend buffers to OpenGL graphics related buffers. One of the following macros must be defined to use the helper functions with a given compute backend. - USE_FORGE_CPU_COPY_HELPERS for cpu based functionality - USE_FORGE_CUDA_COPY_HELPERS for cuda-opengl interop - USE_FORGE_OPENCL_COPY_HELPERS for opencl-opengl interop For example, to use the header with cpu, cuda backend, you have to do the following: ```cpp \#define USE_FORGE_CPU_COPY_HELPERS \#include ``` For opencl backend, couple of additional things need to be defined apart from above macro definition and header inclusion. ```cpp cl_context getContext() { // You need to return a valid cl_context object } ``` ```cpp cl_command_queue getCommandQueue() { // You need to return a valid cl_command_queue object } ``` --- examples/cpu/bubblechart.cpp | 30 ++++- examples/cpu/field.cpp | 19 ++- examples/cpu/fractal.cpp | 14 +- examples/cpu/histogram.cpp | 22 ++- examples/cpu/plot3.cpp | 13 +- examples/cpu/plotting.cpp | 22 ++- examples/cpu/stream.cpp | 20 ++- examples/cpu/surface.cpp | 11 +- examples/cuda/fractal.cu | 24 +++- examples/cuda/histogram.cu | 20 ++- examples/cuda/plot3.cu | 13 +- examples/cuda/plotting.cu | 32 ++++- examples/cuda/surface.cu | 28 +++- examples/opencl/cl_helpers.h | 1 - examples/opencl/fractal.cpp | 34 ++++- examples/opencl/histogram.cpp | 39 +++++- examples/opencl/plot3.cpp | 29 +++- examples/opencl/plotting.cpp | 40 ++++-- examples/opencl/surface.cpp | 30 ++++- include/CPUCopy.hpp | 52 -------- include/CUDACopy.hpp | 82 ------------ include/ComputeCopy.h | 245 ++++++++++++++++++++++++++++++++++ include/OpenCLCopy.hpp | 78 ----------- 23 files changed, 600 insertions(+), 298 deletions(-) delete mode 100644 include/CPUCopy.hpp delete mode 100644 include/CUDACopy.hpp create mode 100644 include/ComputeCopy.h delete mode 100644 include/OpenCLCopy.hpp diff --git a/examples/cpu/bubblechart.cpp b/examples/cpu/bubblechart.cpp index 29306413..855fc42e 100644 --- a/examples/cpu/bubblechart.cpp +++ b/examples/cpu/bubblechart.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -98,19 +99,36 @@ int main(void) * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plt1.vertices(), plt1.verticesSize(), (const void*)cosData.data()); - fg::copy(plt2.vertices(), plt2.verticesSize(), (const void*)tanData.data()); + + GfxHandle* handles[5]; + + // create GL-CPU interop buffers + createGLBuffer(&handles[0], plt1.vertices(), FORGE_VBO); + createGLBuffer(&handles[1], plt2.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], plt2.colors(), FORGE_VBO); + createGLBuffer(&handles[3], plt2.alphas(), FORGE_VBO); + createGLBuffer(&handles[4], plt2.markers(), FORGE_VBO); + + // copy the data from compute buffer to graphics buffer + copyToGLBuffer(handles[0], (ComputeResourceHandle)cosData.data(), plt1.verticesSize()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)tanData.data(), plt2.verticesSize()); /* update color value for tan graph */ - fg::copy(plt2.colors(), plt2.colorsSize(), (const void*)colors.data()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)colors.data(), plt2.colorsSize()); /* update alpha values for tan graph */ - fg::copy(plt2.alphas(), plt2.alphasSize(), (const void*)alphas.data()); + copyToGLBuffer(handles[3], (ComputeResourceHandle)alphas.data(), plt2.alphasSize()); /* update marker sizes for tan graph markers */ - fg::copy(plt2.markers(), plt2.markersSize(), (const void*)radii.data()); + copyToGLBuffer(handles[4], (ComputeResourceHandle)radii.data(), plt2.markersSize()); do { wnd.draw(chart); } while(!wnd.close()); + // destroy GL-CPU Interop buffer + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + releaseGLBuffer(handles[3]); + releaseGLBuffer(handles[4]); return 0; } diff --git a/examples/cpu/field.cpp b/examples/cpu/field.cpp index 6246fc3a..ffcba2fc 100644 --- a/examples/cpu/field.cpp +++ b/examples/cpu/field.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -67,14 +68,24 @@ int main(void) std::vector dirs; generatePoints(points, dirs); - fg::copy(field.vertices(), field.verticesSize(), (const void*)points.data()); - fg::copy(field.directions(), field.directionsSize(), (const void*)dirs.data()); + GfxHandle* handles[3]; - fg::copy(divPoints.vertices(), divPoints.verticesSize(), (const void*)DPOINTS); + createGLBuffer(&handles[0], divPoints.vertices(), FORGE_VBO); + createGLBuffer(&handles[1], field.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], field.directions(), FORGE_VBO); + + copyToGLBuffer(handles[0], (ComputeResourceHandle)DPOINTS, divPoints.verticesSize()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)points.data(), field.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)dirs.data(), field.directionsSize()); do { wnd.draw(chart); } while(!wnd.close()); + // destroy GL-cpu interop buffers + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + return 0; } diff --git a/examples/cpu/fractal.cpp b/examples/cpu/fractal.cpp index 6e076531..74a9970e 100644 --- a/examples/cpu/fractal.cpp +++ b/examples/cpu/fractal.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include @@ -63,12 +64,21 @@ int main(void) * along with the library to help with this task */ kernel(bmp); - fg::copy(img, (const void*)bmp.ptr); + + GfxHandle* handle = 0; + + // create GL-CPU interop buffer + createGLBuffer(&handle, img.pbo(), FORGE_PBO); + + // copy the data from compute buffer to graphics buffer + copyToGLBuffer(handle, (ComputeResourceHandle)bmp.ptr, img.size()); do { wnd.draw(img); } while(!wnd.close()); + // destroy GL-CPU Interop buffer + releaseGLBuffer(handle); destroyBitmap(bmp); return 0; } diff --git a/examples/cpu/histogram.cpp b/examples/cpu/histogram.cpp index b6b2b52d..66e14379 100644 --- a/examples/cpu/histogram.cpp +++ b/examples/cpu/histogram.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -85,13 +86,22 @@ int main(void) */ hist.setColor(FG_YELLOW); + GfxHandle* handles[3]; + + createGLBuffer(&handles[0], img.pbo(), FORGE_PBO); + createGLBuffer(&handles[1], hist.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], hist.colors(), FORGE_VBO); + do { /* * generate image, and prepare data to pass into * Histogram's underlying vertex buffer object */ kernel(bmp); - fg::copy(img, (const void*)bmp.ptr); + + copyToGLBuffer(handles[0], (ComputeResourceHandle)bmp.ptr, img.size()); + + //fg::copy(img, (const void*)bmp.ptr); /* copy your data into the vertex buffer object exposed by * fg::Histogram class and then proceed to rendering. @@ -103,8 +113,8 @@ int main(void) std::vector colArray(3*NBINS, 0.0f); populateBins(bmp, histArray.data(), NBINS, colArray.data()); - fg::copy(hist.vertices(), hist.verticesSize(), (const void*)histArray.data()); - fg::copy(hist.colors(), hist.colorsSize(), (const void*)colArray.data()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)histArray.data(), hist.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)colArray.data(), hist.colorsSize()); wnd.draw(0, 0, img, "Dynamic Perlin Noise" ); wnd.draw(1, 0, chart, "Histogram of Noisy Image"); @@ -112,6 +122,10 @@ int main(void) wnd.swapBuffers(); } while(!wnd.close()); + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + return 0; } diff --git a/examples/cpu/plot3.cpp b/examples/cpu/plot3.cpp index 7436d561..1515ef14 100644 --- a/examples/cpu/plot3.cpp +++ b/examples/cpu/plot3.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -56,20 +57,26 @@ int main(void) std::vector function; static float t=0; generateCurve(t, DX, function); + + GfxHandle* handle; + createGLBuffer(&handle, plot3.vertices(), FORGE_VBO); + /* copy your data into the pixel buffer object exposed by * fg::Plot class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plot3.vertices(), plot3.verticesSize(), (const void*)function.data()); + copyToGLBuffer(handle, (ComputeResourceHandle)function.data(), plot3.verticesSize()); do { t+=0.01; generateCurve(t, DX, function); - fg::copy(plot3.vertices(), plot3.verticesSize(), (const void*)function.data()); + copyToGLBuffer(handle, (ComputeResourceHandle)function.data(), plot3.verticesSize()); wnd.draw(chart); } while(!wnd.close()); + releaseGLBuffer(handle); + return 0; } diff --git a/examples/cpu/plotting.cpp b/examples/cpu/plotting.cpp index 4e3b370a..52cf1be1 100644 --- a/examples/cpu/plotting.cpp +++ b/examples/cpu/plotting.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -77,6 +78,11 @@ int main(void) plt2.setLegend("Tangent"); plt3.setLegend("Log base 10"); + GfxHandle* handles[4]; + createGLBuffer(&handles[0], plt0.vertices(), FORGE_VBO); + createGLBuffer(&handles[1], plt1.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], plt2.vertices(), FORGE_VBO); + createGLBuffer(&handles[3], plt3.vertices(), FORGE_VBO); /* copy your data into the pixel buffer object exposed by * fg::Plot class and then proceed to rendering. @@ -84,15 +90,19 @@ int main(void) * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plt0.vertices(), plt0.verticesSize(), (const void*)sinData.data()); - fg::copy(plt1.vertices(), plt1.verticesSize(), (const void*)cosData.data()); - fg::copy(plt2.vertices(), plt2.verticesSize(), (const void*)tanData.data()); - fg::copy(plt3.vertices(), plt3.verticesSize(), (const void*)logData.data()); + copyToGLBuffer(handles[0], (ComputeResourceHandle)sinData.data(), plt0.verticesSize()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)cosData.data(), plt1.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)tanData.data(), plt2.verticesSize()); + copyToGLBuffer(handles[3], (ComputeResourceHandle)logData.data(), plt3.verticesSize()); do { wnd.draw(chart); } while(!wnd.close()); + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + releaseGLBuffer(handles[3]); + return 0; } - diff --git a/examples/cpu/stream.cpp b/examples/cpu/stream.cpp index 29d228ee..4f6810f4 100644 --- a/examples/cpu/stream.cpp +++ b/examples/cpu/stream.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -109,15 +110,24 @@ int main(void) generatePoints(points, dirs); generateColors(colors); - fg::copy(field.vertices(), field.verticesSize(), (const void*)points.data()); - fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); - fg::copy(field.directions(), field.directionsSize(), (const void*)dirs.data()); + GfxHandle* handles[3]; + createGLBuffer(&handles[0], field.vertices(), FORGE_VBO); + createGLBuffer(&handles[1], field.colors(), FORGE_VBO); + createGLBuffer(&handles[2], field.directions(), FORGE_VBO); + + copyToGLBuffer(handles[0], (ComputeResourceHandle)points.data(), field.verticesSize()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)colors.data(), field.colorsSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)dirs.data() , field.directionsSize()); do { moveColors(colors); - fg::copy(field.colors(), field.colorsSize(), (const void*)colors.data()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)colors.data(), field.colorsSize()); wnd.draw(chart); } while(!wnd.close()); + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + return 0; } diff --git a/examples/cpu/surface.cpp b/examples/cpu/surface.cpp index 66f53b41..6826b298 100644 --- a/examples/cpu/surface.cpp +++ b/examples/cpu/surface.cpp @@ -8,7 +8,8 @@ ********************************************************/ #include -#include +#define USE_FORGE_CPU_COPY_HELPERS +#include #include #include #include @@ -59,17 +60,23 @@ int main(void) std::vector function; genSurface(DX, function); + + GfxHandle* handle; + createGLBuffer(&handle, surf.vertices(), FORGE_VBO); + /* copy your data into the pixel buffer object exposed by * fg::Plot class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(surf.vertices(), surf.verticesSize(), (const void*)function.data()); + copyToGLBuffer(handle, (ComputeResourceHandle)function.data(), surf.verticesSize()); do { wnd.draw(chart); } while(!wnd.close()); + releaseGLBuffer(handle); + return 0; } diff --git a/examples/cuda/fractal.cu b/examples/cuda/fractal.cu index 16f28df2..1a03daa8 100644 --- a/examples/cuda/fractal.cu +++ b/examples/cuda/fractal.cu @@ -1,7 +1,17 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + #include #include #include -#include +#define USE_FORGE_CUDA_COPY_HELPERS +#include #include const unsigned DIMX = 512; @@ -26,6 +36,12 @@ int main(void) * textures and pixel buffer objects to hold the image * */ fg::Image img(DIMX, DIMY, FG_RGBA, fg::u8); + + GfxHandle* handle = 0; + + // create GL-CPU interop buffer + createGLBuffer(&handle, img.pbo(), FORGE_PBO); + /* copy your data into the pixel buffer object exposed by * fg::Image class and then proceed to rendering. * To help the users with copying the data from compute @@ -34,12 +50,16 @@ int main(void) */ FORGE_CUDA_CHECK(cudaMalloc((void**)&dev_out, SIZE)); kernel(dev_out); - fg::copy(img, dev_out); + + // copy the data from compute buffer to graphics buffer + copyToGLBuffer(handle, (ComputeResourceHandle)dev_out, img.size()); do { wnd.draw(img); } while(!wnd.close()); + // destroy GL-CPU Interop buffer + releaseGLBuffer(handle); FORGE_CUDA_CHECK(cudaFree(dev_out)); return 0; } diff --git a/examples/cuda/histogram.cu b/examples/cuda/histogram.cu index 740d84ac..afa505ae 100644 --- a/examples/cuda/histogram.cu +++ b/examples/cuda/histogram.cu @@ -12,7 +12,8 @@ #include #include #include -#include +#define USE_FORGE_CUDA_COPY_HELPERS +#include #include const unsigned IMGW = 256; @@ -103,16 +104,23 @@ int main(void) FORGE_CUDA_CHECK(cudaMalloc((void**)&histOut, NBINS * sizeof(int))); FORGE_CUDA_CHECK(cudaMalloc((void**)&histColors, 3*NBINS * sizeof(float))); + GfxHandle* handles[3]; + + createGLBuffer(&handles[0], img.pbo(), FORGE_PBO); + createGLBuffer(&handles[1], hist.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], hist.colors(), FORGE_VBO); + unsigned frame = 0; do { if (frame%8==0) { kernel(bmp, noiseGenerator); - fg::copy(img, bmp.ptr); + copyToGLBuffer(handles[0], (ComputeResourceHandle)bmp.ptr, img.size()); populateBins(bmp, histOut, NBINS, histColors); - fg::copy(hist.vertices(), histOut); - fg::copy(hist.colors(), histColors); + copyToGLBuffer(handles[1], (ComputeResourceHandle)histOut, hist.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)histColors, hist.colorsSize()); + frame = 0; } @@ -125,6 +133,10 @@ int main(void) FORGE_CUDA_CHECK(cudaFree(histOut)); FORGE_CUDA_CHECK(cudaFree(histColors)); + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + return 0; } diff --git a/examples/cuda/plot3.cu b/examples/cuda/plot3.cu index 7fa9f032..9e569903 100644 --- a/examples/cuda/plot3.cu +++ b/examples/cuda/plot3.cu @@ -10,7 +10,8 @@ #include #include #include -#include +#define USE_FORGE_CUDA_COPY_HELPERS +#include #include #include @@ -46,23 +47,27 @@ int main(void) static float t=0; FORGE_CUDA_CHECK(cudaMalloc((void**)&dev_out, ZSIZE * 3 * sizeof(float) )); kernel(t, DX, dev_out); + + GfxHandle* handle; + createGLBuffer(&handle, plot3.vertices(), FORGE_VBO); + /* copy your data into the vertex buffer object exposed by * fg::Plot class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plot3.vertices(), dev_out); - + copyToGLBuffer(handle, (ComputeResourceHandle)dev_out, plot3.verticesSize()); do { t+=0.01; kernel(t, DX, dev_out); - fg::copy(plot3.vertices(), dev_out); + copyToGLBuffer(handle, (ComputeResourceHandle)dev_out, plot3.verticesSize()); wnd.draw(chart); } while(!wnd.close()); FORGE_CUDA_CHECK(cudaFree(dev_out)); + releaseGLBuffer(handle); return 0; } diff --git a/examples/cuda/plotting.cu b/examples/cuda/plotting.cu index c56e3f7f..b0b74fb4 100644 --- a/examples/cuda/plotting.cu +++ b/examples/cuda/plotting.cu @@ -1,7 +1,17 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + #include #include #include -#include +#define USE_FORGE_CUDA_COPY_HELPERS +#include #include #include @@ -65,16 +75,23 @@ int main(void) kernel(cos_out, 1); kernel(tan_out, 2); kernel(log_out, 3); + + GfxHandle* handles[4]; + createGLBuffer(&handles[0], plt0.vertices(), FORGE_VBO); + createGLBuffer(&handles[1], plt1.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], plt2.vertices(), FORGE_VBO); + createGLBuffer(&handles[3], plt3.vertices(), FORGE_VBO); + /* copy your data into the vertex buffer object exposed by * fg::Plot class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plt0.vertices(), sin_out); - fg::copy(plt1.vertices(), cos_out); - fg::copy(plt2.vertices(), tan_out); - fg::copy(plt3.vertices(), log_out); + copyToGLBuffer(handles[0], (ComputeResourceHandle)sin_out, plt0.verticesSize()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)cos_out, plt1.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)tan_out, plt2.verticesSize()); + copyToGLBuffer(handles[3], (ComputeResourceHandle)log_out, plt3.verticesSize()); do { wnd.draw(chart); @@ -84,6 +101,11 @@ int main(void) FORGE_CUDA_CHECK(cudaFree(cos_out)); FORGE_CUDA_CHECK(cudaFree(tan_out)); FORGE_CUDA_CHECK(cudaFree(log_out)); + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + releaseGLBuffer(handles[3]); + return 0; } diff --git a/examples/cuda/surface.cu b/examples/cuda/surface.cu index 53824a24..cc989ae1 100644 --- a/examples/cuda/surface.cu +++ b/examples/cuda/surface.cu @@ -1,7 +1,17 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + #include #include #include -#include +#define USE_FORGE_CUDA_COPY_HELPERS +#include #include #include @@ -37,27 +47,31 @@ int main(void) FORGE_CUDA_CHECK(cudaMalloc((void**)&dev_out, XSIZE * YSIZE * 3 * sizeof(float) )); kernel(DX, dev_out); + + GfxHandle* handle; + createGLBuffer(&handle, surf.vertices(), FORGE_VBO); /* copy your data into the vertex buffer object exposed by * fg::Plot class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(surf.vertices(), dev_out); + copyToGLBuffer(handle, (ComputeResourceHandle)dev_out, surf.verticesSize()); do { wnd.draw(chart); } while(!wnd.close()); FORGE_CUDA_CHECK(cudaFree(dev_out)); + releaseGLBuffer(handle); return 0; } -__global__ + __global__ void sincos_surf(float dx, float* out, - const float XMIN, const float YMIN, - const size_t XSIZE, const size_t YSIZE) + const float XMIN, const float YMIN, + const size_t XSIZE, const size_t YSIZE) { int i = blockIdx.x * blockDim.x + threadIdx.x; int j = blockIdx.y * blockDim.y + threadIdx.y; @@ -73,7 +87,7 @@ void sincos_surf(float dx, float* out, } } -inline + inline int divup(int a, int b) { return (a+b-1)/b; @@ -83,7 +97,7 @@ void kernel(float dx, float* dev_out) { static const dim3 threads(8, 8); dim3 blocks(divup(XSIZE, threads.x), - divup(YSIZE, threads.y)); + divup(YSIZE, threads.y)); sincos_surf<<< blocks, threads >>>(dx, dev_out, XMIN, YMIN, XSIZE, YSIZE); } diff --git a/examples/opencl/cl_helpers.h b/examples/opencl/cl_helpers.h index da79b06b..4b140b58 100644 --- a/examples/opencl/cl_helpers.h +++ b/examples/opencl/cl_helpers.h @@ -105,4 +105,3 @@ bool checkExtnAvailability(const Device &pDevice, std::string pName) } return ret_val; } - diff --git a/examples/opencl/fractal.cpp b/examples/opencl/fractal.cpp index a279e196..55d5bc07 100644 --- a/examples/opencl/fractal.cpp +++ b/examples/opencl/fractal.cpp @@ -9,8 +9,6 @@ #include #include "cl_helpers.h" - -#include #include #include #include @@ -25,6 +23,22 @@ const unsigned DIMX = 512; const unsigned DIMY = 512; const unsigned IMG_SIZE = DIMX * DIMY * 4; +cl::CommandQueue queue; +cl::Context context; + +cl_context getContext() +{ + return context(); +} + +cl_command_queue getCommandQueue() +{ + return queue(); +} + +#define USE_FORGE_OPENCL_COPY_HELPERS +#include + static const std::string fractal_ocl_kernel = "float magnitude(float2 a)\n" "{\n" @@ -148,8 +162,7 @@ int main(void) plat.getDevices(CL_DEVICE_TYPE_GPU, &devs); Device device; - CommandQueue queue; - Context context; + for (auto& d : devs) { if (checkExtnAvailability(d, CL_GL_SHARING_EXT)) { device = d; @@ -172,11 +185,22 @@ int main(void) cl::Buffer devOut(context, CL_MEM_READ_WRITE, IMG_SIZE); kernel(devOut, queue); - fg::copy(img, devOut, queue); + + GfxHandle* handle = 0; + + // create GL-CPU interop buffer + createGLBuffer(&handle, img.pbo(), FORGE_PBO); + + // copy the data from compute buffer to graphics buffer + copyToGLBuffer(handle, (ComputeResourceHandle)devOut(), img.size()); do { wnd.draw(img); } while(!wnd.close()); + + // destroy GL-CPU Interop buffer + releaseGLBuffer(handle); + }catch (fg::Error err) { std::cout << err.what() << "(" << err.err() << ")" << std::endl; } catch (cl::Error err) { diff --git a/examples/opencl/histogram.cpp b/examples/opencl/histogram.cpp index db9d74bc..5b0d3b42 100644 --- a/examples/opencl/histogram.cpp +++ b/examples/opencl/histogram.cpp @@ -9,8 +9,6 @@ #include #include "cl_helpers.h" - -#include #include #include #include @@ -32,6 +30,22 @@ const unsigned WIN_COLS = 2; const unsigned NBINS = 256; const float PERSISTENCE = 0.5f; +cl::CommandQueue queue; +cl::Context context; + +cl_context getContext() +{ + return context(); +} + +cl_command_queue getCommandQueue() +{ + return queue(); +} + +#define USE_FORGE_OPENCL_COPY_HELPERS +#include + static const std::string perlinKernels = R"EOK( float rand(int x) @@ -319,8 +333,7 @@ int main(void) plat.getDevices(CL_DEVICE_TYPE_GPU, &devs); Device device; - CommandQueue queue; - Context context; + for (auto& d : devs) { if (checkExtnAvailability(d, CL_GL_SHARING_EXT)) { device = d; @@ -340,14 +353,21 @@ int main(void) cl::Buffer histOut(context, CL_MEM_READ_WRITE, NBINS * sizeof(int)); cl::Buffer colors(context, CL_MEM_READ_WRITE, 3 * NBINS * sizeof(float)); + GfxHandle* handles[3]; + + createGLBuffer(&handles[0], img.pbo(), FORGE_PBO); + createGLBuffer(&handles[1], hist.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], hist.colors(), FORGE_VBO); + unsigned frame = 0; do { if (frame%8==0) { kernel(image, baseNoise, perlinNoise, histOut, colors, queue, device); - fg::copy(img, image, queue); - fg::copy(hist.vertices(), hist.verticesSize(), histOut, queue); - fg::copy(hist.colors(), hist.colorsSize(), colors, queue); + copyToGLBuffer(handles[0], (ComputeResourceHandle)image(), img.size()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)histOut(), hist.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)colors(), hist.colorsSize()); + frame = 0; } @@ -357,6 +377,11 @@ int main(void) wnd.swapBuffers(); frame++; } while(!wnd.close()); + + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + }catch (fg::Error err) { std::cout << err.what() << "(" << err.err() << ")" << std::endl; } catch (cl::Error err) { diff --git a/examples/opencl/plot3.cpp b/examples/opencl/plot3.cpp index 0bfb4f9b..657b4120 100644 --- a/examples/opencl/plot3.cpp +++ b/examples/opencl/plot3.cpp @@ -9,8 +9,6 @@ #include #include "cl_helpers.h" - -#include #include #include #include @@ -28,9 +26,24 @@ static const float ZMAX = 10.f; const float DX = 0.005; static const unsigned ZSIZE = (ZMAX-ZMIN)/DX+1; - using namespace std; +cl::CommandQueue queue; +cl::Context context; + +cl_context getContext() +{ + return context(); +} + +cl_command_queue getCommandQueue() +{ + return queue(); +} + +#define USE_FORGE_OPENCL_COPY_HELPERS +#include + static const std::string sincos_surf_kernel = "kernel void generateCurve(global float* out, const float t, const float dx, const float zmin, const unsigned SIZE)\n" "{\n" @@ -135,20 +148,26 @@ int main(void) cl::Buffer devOut(context, CL_MEM_READ_WRITE, sizeof(float) * ZSIZE * 3); static float t=0; kernel(devOut, queue, t); + + GfxHandle* handle; + createGLBuffer(&handle, plot3.vertices(), FORGE_VBO); /* copy your data into the pixel buffer object exposed by * fg::Surface class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plot3.vertices(), plot3.verticesSize(), devOut, queue); + copyToGLBuffer(handle, (ComputeResourceHandle)devOut(), plot3.verticesSize()); do { t+=0.01; kernel(devOut, queue, t); - fg::copy(plot3.vertices(), plot3.verticesSize(), devOut, queue); + copyToGLBuffer(handle, (ComputeResourceHandle)devOut(), plot3.verticesSize()); wnd.draw(chart); } while(!wnd.close()); + + releaseGLBuffer(handle); + }catch (fg::Error err) { std::cout << err.what() << "(" << err.err() << ")" << std::endl; } catch (cl::Error err) { diff --git a/examples/opencl/plotting.cpp b/examples/opencl/plotting.cpp index e454ac8e..658b687a 100644 --- a/examples/opencl/plotting.cpp +++ b/examples/opencl/plotting.cpp @@ -9,8 +9,6 @@ #include #include "cl_helpers.h" - -#include #include #include #include @@ -29,6 +27,22 @@ const float FRANGE_START = 0.f; const float FRANGE_END = 2 * 3.141592f; const unsigned DATA_SIZE = ( FRANGE_END - FRANGE_START ) / dx; +cl::CommandQueue queue; +cl::Context context; + +cl_context getContext() +{ + return context(); +} + +cl_command_queue getCommandQueue() +{ + return queue(); +} + +#define USE_FORGE_OPENCL_COPY_HELPERS +#include + static const std::string sinf_ocl_kernel = R"( kernel void sinf(global float* out, const float dx, const unsigned DATA_SIZE, int fnCode) { @@ -140,8 +154,7 @@ int main(void) plat.getDevices(CL_DEVICE_TYPE_GPU, &devs); Device device; - CommandQueue queue; - Context context; + for (auto& d : devs) { if (checkExtnAvailability(d, CL_GL_SHARING_EXT)) { device = d; @@ -164,20 +177,31 @@ int main(void) kernel(tanOut, queue, 2); kernel(logOut, queue, 3); + GfxHandle* handles[4]; + createGLBuffer(&handles[0], plt0.vertices(), FORGE_VBO); + createGLBuffer(&handles[1], plt1.vertices(), FORGE_VBO); + createGLBuffer(&handles[2], plt2.vertices(), FORGE_VBO); + createGLBuffer(&handles[3], plt3.vertices(), FORGE_VBO); /* copy your data into the vertex buffer object exposed by * fg::Plot class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(plt0.vertices(), plt0.verticesSize(), sinOut, queue); - fg::copy(plt1.vertices(), plt1.verticesSize(), cosOut, queue); - fg::copy(plt2.vertices(), plt2.verticesSize(), tanOut, queue); - fg::copy(plt3.vertices(), plt3.verticesSize(), logOut, queue); + copyToGLBuffer(handles[0], (ComputeResourceHandle)sinOut(), plt0.verticesSize()); + copyToGLBuffer(handles[1], (ComputeResourceHandle)cosOut(), plt1.verticesSize()); + copyToGLBuffer(handles[2], (ComputeResourceHandle)tanOut(), plt2.verticesSize()); + copyToGLBuffer(handles[3], (ComputeResourceHandle)logOut(), plt3.verticesSize()); do { wnd.draw(chart); } while(!wnd.close()); + + releaseGLBuffer(handles[0]); + releaseGLBuffer(handles[1]); + releaseGLBuffer(handles[2]); + releaseGLBuffer(handles[3]); + }catch (fg::Error err) { std::cout << err.what() << "(" << err.err() << ")" << std::endl; } catch (cl::Error err) { diff --git a/examples/opencl/surface.cpp b/examples/opencl/surface.cpp index f54cb00f..318697f3 100644 --- a/examples/opencl/surface.cpp +++ b/examples/opencl/surface.cpp @@ -9,8 +9,6 @@ #include #include "cl_helpers.h" - -#include #include #include #include @@ -30,6 +28,22 @@ const unsigned YSIZE = (YMAX-YMIN)/DX; using namespace std; +cl::CommandQueue queue; +cl::Context context; + +cl_context getContext() +{ + return context(); +} + +cl_command_queue getCommandQueue() +{ + return queue(); +} + +#define USE_FORGE_OPENCL_COPY_HELPERS +#include + static const std::string sin_surf_kernel = R"EOK( kernel @@ -144,8 +158,7 @@ int main(void) plat.getDevices(CL_DEVICE_TYPE_GPU, &devs); Device device; - CommandQueue queue; - Context context; + for (auto& d : devs) { if (checkExtnAvailability(d, CL_GL_SHARING_EXT)) { device = d; @@ -162,22 +175,27 @@ int main(void) cl::Buffer devOut(context, CL_MEM_READ_WRITE, sizeof(float) * XSIZE * YSIZE * 3); kernel(devOut, queue, device); + + GfxHandle* handle; + createGLBuffer(&handle, surf.vertices(), FORGE_VBO); /* copy your data into the pixel buffer object exposed by * fg::Surface class and then proceed to rendering. * To help the users with copying the data from compute * memory to display memory, Forge provides copy headers * along with the library to help with this task */ - fg::copy(surf.vertices(), surf.verticesSize(), devOut, queue); + copyToGLBuffer(handle, (ComputeResourceHandle)devOut(), surf.verticesSize()); do { wnd.draw(chart); } while(!wnd.close()); + + releaseGLBuffer(handle); }catch (fg::Error err) { std::cout << err.what() << "(" << err.err() << ")" << std::endl; } catch (cl::Error err) { std::cout << err.what() << "(" << err.err() << ")" << std::endl; } + return 0; } - diff --git a/include/CPUCopy.hpp b/include/CPUCopy.hpp deleted file mode 100644 index 7a474cf9..00000000 --- a/include/CPUCopy.hpp +++ /dev/null @@ -1,52 +0,0 @@ -/******************************************************* - * Copyright (c) 2015-2019, ArrayFire - * All rights reserved. - * - * This file is distributed under 3-clause BSD license. - * The complete license agreement can be obtained at: - * http://arrayfire.com/licenses/BSD-3-Clause - ********************************************************/ - -#ifndef __CPU_DATA_COPY_H__ -#define __CPU_DATA_COPY_H__ - - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -} -#endif - - -#ifdef __cplusplus - -namespace fg -{ - -static -void copy(fg::Image& out, const void * dataPtr) -{ - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, out.pbo()); - glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, out.size(), dataPtr); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); -} - -/* - * Below functions expects OpenGL resource Id and size in bytes to copy the data from - * cpu memory location to graphics memory - */ -static -void copy(const int resourceId, const size_t resourceSize, const void * dataPtr) -{ - glBindBuffer(GL_ARRAY_BUFFER, resourceId); - glBufferSubData(GL_ARRAY_BUFFER, 0, resourceSize, dataPtr); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} - -} - -#endif - -#endif //__CPU_DATA_COPY_H__ diff --git a/include/CUDACopy.hpp b/include/CUDACopy.hpp deleted file mode 100644 index 0d640930..00000000 --- a/include/CUDACopy.hpp +++ /dev/null @@ -1,82 +0,0 @@ -/******************************************************* - * Copyright (c) 2015-2019, ArrayFire - * All rights reserved. - * - * This file is distributed under 3-clause BSD license. - * The complete license agreement can be obtained at: - * http://arrayfire.com/licenses/BSD-3-Clause - ********************************************************/ - -#ifndef __CUDA_DATA_COPY_H__ -#define __CUDA_DATA_COPY_H__ - -#include - - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -} -#endif - - -#ifdef __cplusplus - -#include - -static void handleCUDAError(cudaError_t err, const char *file, int line) -{ - if (err != cudaSuccess) { - printf( "%s in %s at line %d\n", cudaGetErrorString(err), file, line); - exit(EXIT_FAILURE); - } -} - -#define FORGE_CUDA_CHECK(err) (handleCUDAError(err, __FILE__, __LINE__ )) - -namespace fg -{ - -template -void copy(fg::Image& out, const T * devicePtr) -{ - cudaGraphicsResource *cudaPBOResource; - FORGE_CUDA_CHECK(cudaGraphicsGLRegisterBuffer(&cudaPBOResource, out.pbo(), cudaGraphicsMapFlagsWriteDiscard)); - - size_t num_bytes; - T* pboDevicePtr = NULL; - - FORGE_CUDA_CHECK(cudaGraphicsMapResources(1, &cudaPBOResource, 0)); - FORGE_CUDA_CHECK(cudaGraphicsResourceGetMappedPointer((void **)&pboDevicePtr, &num_bytes, cudaPBOResource)); - FORGE_CUDA_CHECK(cudaMemcpy(pboDevicePtr, devicePtr, num_bytes, cudaMemcpyDeviceToDevice)); - FORGE_CUDA_CHECK(cudaGraphicsUnmapResources(1, &cudaPBOResource, 0)); - FORGE_CUDA_CHECK(cudaGraphicsUnregisterResource(cudaPBOResource)); -} - -/* - * Below functions expects OpenGL resource Id and size in bytes to copy the data from - * CUDA device memory location to graphics memory - */ -template -void copy(const int resourceId, const T * devicePtr) -{ - cudaGraphicsResource *cudaVBOResource; - FORGE_CUDA_CHECK(cudaGraphicsGLRegisterBuffer(&cudaVBOResource, resourceId, - cudaGraphicsMapFlagsWriteDiscard)); - size_t num_bytes; - T* vboDevicePtr = NULL; - - FORGE_CUDA_CHECK(cudaGraphicsMapResources(1, &cudaVBOResource, 0)); - FORGE_CUDA_CHECK(cudaGraphicsResourceGetMappedPointer((void **)&vboDevicePtr, &num_bytes, cudaVBOResource)); - FORGE_CUDA_CHECK(cudaMemcpy(vboDevicePtr, devicePtr, num_bytes, cudaMemcpyDeviceToDevice)); - FORGE_CUDA_CHECK(cudaGraphicsUnmapResources(1, &cudaVBOResource, 0)); - FORGE_CUDA_CHECK(cudaGraphicsUnregisterResource(cudaVBOResource)); -} - -} - -#endif - -#endif //__CUDA_DATA_COPY_H__ diff --git a/include/ComputeCopy.h b/include/ComputeCopy.h new file mode 100644 index 00000000..3c303d96 --- /dev/null +++ b/include/ComputeCopy.h @@ -0,0 +1,245 @@ +/******************************************************* + * Copyright (c) 2015-2019, ArrayFire + * All rights reserved. + * + * This file is distributed under 3-clause BSD license. + * The complete license agreement can be obtained at: + * http://arrayfire.com/licenses/BSD-3-Clause + ********************************************************/ + +#ifndef __COMPUTE_DATA_COPY_H__ +#define __COMPUTE_DATA_COPY_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +#if defined(USE_FORGE_CPU_COPY_HELPERS) + +// No special headers for cpu backend + +#elif defined(USE_FORGE_CUDA_COPY_HELPERS) + +#include +#include +#include +#include + +#elif defined(USE_FORGE_OPENCL_COPY_HELPERS) + +// No special headers for opencl backend + +#else + + #error "Invalid Compute model, exiting." + +#endif + + +/** A backend-agnostic handle to a compute memory resource originating from an OpenGL resource. + + - cudaGraphicsResource in CUDA + - cl_mem in OpenCL + - GLuint from standard cpu + */ +#if defined(USE_FORGE_CPU_COPY_HELPERS) +typedef GLuint GfxResourceHandle; +#elif defined(USE_FORGE_CUDA_COPY_HELPERS) +typedef cudaGraphicsResource* GfxResourceHandle; +#elif defined(USE_FORGE_OPENCL_COPY_HELPERS) +typedef cl_mem GfxResourceHandle; +#endif + + +/** A backend-agnostic handle to a compute memory resource. + + For example: + CUDA device pointer, like float*, int* from cudaMalloc. + A cl_mem* from OpenCL's clCreateBuffer + */ +typedef void* ComputeResourceHandle; + +typedef enum { + FORGE_PBO = 0, ///< OpenGL Pixel Buffer Object + FORGE_VBO = 1 ///< OpenGL Vertex Buffer Object +} BufferType; + +typedef struct { + GfxResourceHandle mId; + BufferType mTarget; +} GfxHandle; + + +/////////////////////////////////////////////////////////////////////////////// + +#if defined(USE_FORGE_CPU_COPY_HELPERS) + +static +void createGLBuffer(GfxHandle** pOut, const unsigned pResourceId, const BufferType pTarget) +{ + GfxHandle* temp = (GfxHandle*)malloc(sizeof(GfxHandle)); + + temp->mId = pResourceId; + temp->mTarget = pTarget; + + *pOut = temp; +} + +static +void releaseGLBuffer(GfxHandle* pHandle) +{ + free(pHandle); +} + +static +void copyToGLBuffer(GfxHandle* pGLDestination, ComputeResourceHandle pSource, const size_t pSize) +{ + GfxHandle* temp = pGLDestination; + + GLenum target = (temp->mTarget==FORGE_PBO ? GL_PIXEL_UNPACK_BUFFER : GL_ARRAY_BUFFER); + + glBindBuffer(target, temp->mId); + glBufferSubData(target, 0, pSize, pSource); + glBindBuffer(target, 0); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// + +#if defined(USE_FORGE_CUDA_COPY_HELPERS) + +static void handleCUDAError(cudaError_t err, const char *file, int line) +{ + if (err != cudaSuccess) { + printf( "%s in %s at line %d\n", cudaGetErrorString(err), file, line); + exit(EXIT_FAILURE); + } +} + +#define FORGE_CUDA_CHECK(err) (handleCUDAError(err, __FILE__, __LINE__ )) + +static +void createGLBuffer(GfxHandle** pOut, const unsigned pResourceId, const BufferType pTarget) +{ + GfxHandle* temp = (GfxHandle*)malloc(sizeof(GfxHandle)); + + temp->mTarget = pTarget; + + cudaGraphicsResource *cudaPBOResource; + + FORGE_CUDA_CHECK(cudaGraphicsGLRegisterBuffer(&cudaPBOResource, + pResourceId, + cudaGraphicsMapFlagsWriteDiscard)); + + temp->mId = cudaPBOResource; + + *pOut = temp; +} + +static +void releaseGLBuffer(GfxHandle* pHandle) +{ + FORGE_CUDA_CHECK(cudaGraphicsUnregisterResource(pHandle->mId)); + free(pHandle); +} + +static +void copyToGLBuffer(GfxHandle* pGLDestination, ComputeResourceHandle pSource, const size_t pSize) +{ + size_t numBytes; + void* pointer = NULL; + + cudaGraphicsResource *cudaResource = pGLDestination->mId; + + FORGE_CUDA_CHECK(cudaGraphicsMapResources(1, &cudaResource, 0)); + + FORGE_CUDA_CHECK(cudaGraphicsResourceGetMappedPointer(&pointer, &numBytes, cudaResource)); + + FORGE_CUDA_CHECK(cudaMemcpy(pointer, pSource, numBytes, cudaMemcpyDeviceToDevice)); + + FORGE_CUDA_CHECK(cudaGraphicsUnmapResources(1, &cudaResource, 0)); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// + +#if defined(USE_FORGE_OPENCL_COPY_HELPERS) + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" + +#define FORGE_OCL_CHECK(cl_status, message) \ + if(cl_status != CL_SUCCESS) \ + { \ + printf("Error: %s \nError Code: %d\n", message, cl_status);\ + printf("Location: %s:%i\n", __FILE__, __LINE__);\ + exit(EXIT_FAILURE); \ + } + +static +void createGLBuffer(GfxHandle** pOut, const unsigned pResourceId, const BufferType pTarget) +{ + GfxHandle* temp = (GfxHandle*)malloc(sizeof(GfxHandle)); + + temp->mTarget = pTarget; + + cl_int returnCode = CL_SUCCESS; + + temp->mId = clCreateFromGLBuffer(getContext(), CL_MEM_WRITE_ONLY, pResourceId, &returnCode); + + FORGE_OCL_CHECK(returnCode, "Failed in clCreateFromGLBuffer"); + + *pOut = temp; +} + +static +void releaseGLBuffer(GfxHandle* pHandle) +{ + FORGE_OCL_CHECK(clReleaseMemObject(pHandle->mId), "Failed in clReleaseMemObject"); + free(pHandle); +} + +static +void copyToGLBuffer(GfxHandle* pGLDestination, ComputeResourceHandle pSource, const size_t pSize) +{ + // The user is expected to implement a function + // `cl_command_queue getCommandQueue()` + cl_command_queue queue = getCommandQueue(); + + cl_event waitEvent; + + cl_mem src = (cl_mem)pSource; + cl_mem dst = pGLDestination->mId; + + glFinish(); + + FORGE_OCL_CHECK(clEnqueueAcquireGLObjects(queue, 1, &dst, 0, NULL, &waitEvent), + "Failed in clEnqueueAcquireGLObjects"); + + FORGE_OCL_CHECK(clWaitForEvents(1, &waitEvent), + "Failed in clWaitForEvents after clEnqueueAcquireGLObjects"); + + FORGE_OCL_CHECK(clEnqueueCopyBuffer(queue, src, dst, 0, 0, pSize, 0, NULL, &waitEvent), + "Failed in clEnqueueCopyBuffer"); + + FORGE_OCL_CHECK(clEnqueueReleaseGLObjects(queue, 1, &dst, 0, NULL, &waitEvent), + "Failed in clEnqueueReleaseGLObjects"); + + FORGE_OCL_CHECK(clWaitForEvents(1, &waitEvent), + "Failed in clWaitForEvents after clEnqueueReleaseGLObjects"); +} + +#pragma GCC diagnostic pop + +#endif + +/////////////////////////////////////////////////////////////////////////////// + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/OpenCLCopy.hpp b/include/OpenCLCopy.hpp deleted file mode 100644 index 9aa717fd..00000000 --- a/include/OpenCLCopy.hpp +++ /dev/null @@ -1,78 +0,0 @@ -/******************************************************* - * Copyright (c) 2015-2019, ArrayFire - * All rights reserved. - * - * This file is distributed under 3-clause BSD license. - * The complete license agreement can be obtained at: - * http://arrayfire.com/licenses/BSD-3-Clause - ********************************************************/ - -#ifndef __OPENCL_DATA_COPY_H__ -#define __OPENCL_DATA_COPY_H__ - - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -} -#endif - - -#ifdef __cplusplus - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-but-set-variable" - -namespace fg -{ - -static -void copy(fg::Image& out, const cl::Buffer& in, const cl::CommandQueue& queue) -{ - cl::Event ev; - cl::BufferGL pboMapBuffer(queue.getInfo(), CL_MEM_WRITE_ONLY, out.pbo(), NULL); - - std::vector shared_objects; - shared_objects.push_back(pboMapBuffer); - - glFinish(); - cl_int res = queue.enqueueAcquireGLObjects(&shared_objects, NULL, &ev); - ev.wait(); - - queue.enqueueCopyBuffer(in, pboMapBuffer, 0, 0, out.size(), NULL, NULL); - res = queue.enqueueReleaseGLObjects(&shared_objects, NULL, &ev); - queue.finish(); -} - -/* - * Below functions expects OpenGL resource Id and size in bytes to copy the data from - * OpenCL Buffer to graphics memory - */ -static -void copy(const int resourceId, const ::size_t resourceSize, - const cl::Buffer& in, const cl::CommandQueue& queue) -{ - cl::Event ev; - cl::BufferGL vboMapBuffer(queue.getInfo(), CL_MEM_WRITE_ONLY, resourceId, NULL); - - std::vector shared_objects; - shared_objects.push_back(vboMapBuffer); - - glFinish(); - cl_int res = queue.enqueueAcquireGLObjects(&shared_objects, NULL, &ev); - ev.wait(); - - queue.enqueueCopyBuffer(in, vboMapBuffer, 0, 0, resourceSize, NULL, NULL); - res = queue.enqueueReleaseGLObjects(&shared_objects, NULL, &ev); - queue.finish(); -} - -} - -#pragma GCC diagnostic pop - -#endif - -#endif //__OPENCL_DATA_COPY_H__ From 3b27cac2ad98a65cdc3db911cd975992538690c0 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 4 Jul 2016 21:04:19 +0530 Subject: [PATCH 16/16] Remove local var's that were shadowing global var's in example_plot3_opencl --- examples/opencl/plot3.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/opencl/plot3.cpp b/examples/opencl/plot3.cpp index 657b4120..ff8d9975 100644 --- a/examples/opencl/plot3.cpp +++ b/examples/opencl/plot3.cpp @@ -130,8 +130,7 @@ int main(void) plat.getDevices(CL_DEVICE_TYPE_GPU, &devs); Device device; - CommandQueue queue; - Context context; + for (auto& d : devs) { if (checkExtnAvailability(d, CL_GL_SHARING_EXT)) { device = d;