Skip to content

Add support thread-local directx (OpenCL interop) initialization#18593

Merged
alalek merged 5 commits intoopencv:masterfrom
masa-iwm:master
Oct 18, 2020
Merged

Add support thread-local directx (OpenCL interop) initialization#18593
alalek merged 5 commits intoopencv:masterfrom
masa-iwm:master

Conversation

@masa-iwm
Copy link
Copy Markdown
Contributor

@masa-iwm masa-iwm commented Oct 15, 2020

Add support for DirectX-OpenCL interop thread-local initialization.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
force_builders=Linux x64,Linux OpenCL,Win64 OpenCL,Docs

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution!

BTW, did you try to validate this patch on configuration with multiple GPUs on some example?

if (impl.initializedPlatform9 != platform)
{
clCreateFromDX9MediaSurfaceKHR = (clCreateFromDX9MediaSurfaceKHR_fn)
impl.clCreateFromDX9MediaSurfaceKHR = (clCreateFromDX9MediaSurfaceKHR_fn)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps such initialization should be moved to OpenCLDirectXImpl methods to keep code responsibilities clean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, move all the initialization to OpenCLDirectXImpl.

if (!platform)
{
cl_device_id current_device = (cl_device_id)ocl::Device::getDefault().ptr();
CV_Assert(current_device);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it is better to move this below (after platforms query, before bool found = false;)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I move this below.
(I'm changing it to use clDeviceInfo(), so it doesn't have much effect.)

cl_platform_id getPlatform()
{
if (!platform)
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It make sense to add this check here: CV_Assert(cv::ocl::haveOpenCV());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe haveOpenCL instead of haveOpenCV?

namespace cv { namespace directx {
struct OpenCLDirectXImpl;
OpenCLDirectXImpl* createDirectXImpl();
void deleteDirectXImpl(OpenCLDirectXImpl**);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this into modules/core/src/directx.cpp and use it instead (here and in directx.hpp).

Consider using this namespace:

  • cv::directx::internal
  • or cv::directx::detail

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, move this into modules/core/src/directx.cpp and using cv::directx::internal namespace.

Comment on lines +337 to +342
OpenCLDirectXImpl& getImpl()
{
OpenCLDirectXImpl* i = getDirectXImpl(ocl::Context::getDefault());
CV_Assert(i);
return *i;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static call of OpenCLDirectXImpl?

Comment on lines +306 to +309
if (current_device == devices[j])
{
platform = platforms[i];
found = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you want to use clGetDeviceInfo() call with CL_DEVICE_PLATFORM.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Thank you!
Certainly the platform can be obtained using clGetDeviceInfo(). Change it to use this.

@masa-iwm
Copy link
Copy Markdown
Contributor Author

It has been verified in my multi-GPU environment.
In the software I'm developing, I need to convert DirectX 11 textures to UMat, but if the primary GPU is Intel and the secondary GPU is NVIDIA, using OpenCV 4.5 caused an error, so I created a this patch.

@masa-iwm
Copy link
Copy Markdown
Contributor Author

The error in the above comment occurs when using an NVIDIA GPU.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution 👍

@alalek alalek merged commit 5ac0712 into opencv:master Oct 18, 2020
TolyaTalamanov pushed a commit to TolyaTalamanov/opencv that referenced this pull request Oct 19, 2020
Add support thread-local directx (OpenCL interop) initialization

* support thread-local directx (OpenCL interop) initialization

* reflect reviews

* Remove verbose function prototype declarations

* Countermeasures for VC warnings. (declaration of 'platform' hides class member)

* core(directx): remove internal stuff from public headers
@alalek alalek mentioned this pull request Nov 27, 2020
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
Add support thread-local directx (OpenCL interop) initialization

* support thread-local directx (OpenCL interop) initialization

* reflect reviews

* Remove verbose function prototype declarations

* Countermeasures for VC warnings. (declaration of 'platform' hides class member)

* core(directx): remove internal stuff from public headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants