opencv/modules/dnn/src/vkcom/shader/conv.comp
WuZhiwen 6e3ea8b49d Merge pull request #12703 from wzw-intel:vkcom
* dnn: Add a Vulkan based backend

This commit adds a new backend "DNN_BACKEND_VKCOM" and a
new target "DNN_TARGET_VULKAN". VKCOM means vulkan based
computation library.

This backend uses Vulkan API and SPIR-V shaders to do
the inference computation for layers. The layer types
that implemented in DNN_BACKEND_VKCOM include:
Conv, Concat, ReLU, LRN, PriorBox, Softmax, MaxPooling,
AvePooling, Permute

This is just a beginning work for Vulkan in OpenCV DNN,
more layer types will be supported and performance
tuning is on the way.

Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>

* dnn/vulkan: Add FindVulkan.cmake to detect Vulkan SDK

In order to build dnn with Vulkan support, need installing
Vulkan SDK and setting environment variable "VULKAN_SDK" and
add "-DWITH_VULKAN=ON" to cmake command.

You can download Vulkan SDK from:
https://vulkan.lunarg.com/sdk/home#linux

For how to install, see
https://vulkan.lunarg.com/doc/sdk/latest/linux/getting_started.html
https://vulkan.lunarg.com/doc/sdk/latest/windows/getting_started.html
https://vulkan.lunarg.com/doc/sdk/latest/mac/getting_started.html
respectively for linux, windows and mac.

To run the vulkan backend, also need installing mesa driver.
On Ubuntu, use this command 'sudo apt-get install mesa-vulkan-drivers'

To test, use command '$BUILD_DIR/bin/opencv_test_dnn --gtest_filter=*VkCom*'

Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>

* dnn/Vulkan: dynamically load Vulkan runtime

No compile-time dependency on Vulkan library.
If Vulkan runtime is unavailable, fallback to CPU path.

Use environment "OPENCL_VULKAN_RUNTIME" to specify path to your
own vulkan runtime library.

Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>

* dnn/Vulkan: Add a python script to compile GLSL shaders to SPIR-V shaders

The SPIR-V shaders are in format of text-based 32-bit hexadecimal
numbers, and inserted into .cpp files as unsigned int32 array.

* dnn/Vulkan: Put Vulkan headers into 3rdparty directory and some other fixes

Vulkan header files are copied from
https://github.com/KhronosGroup/Vulkan-Docs/tree/master/include/vulkan
to 3rdparty/include

Fix the Copyright declaration issue.

Refine OpenCVDetectVulkan.cmake

* dnn/Vulkan: Add vulkan backend tests into existing ones.

Also fixed some test failures.

- Don't use bool variable as uniform for shader
- Fix dispathed group number beyond max issue
- Bypass "group > 1" convolution. This should be support in future.

* dnn/Vulkan: Fix multiple initialization in one thread.
2018-10-29 17:51:26 +03:00

74 lines
2.1 KiB
Plaintext

#version 450
#define LOCAL_SZ_X 256
layout(binding = 0) readonly buffer Input0{
float image_data[];
};
layout(binding = 1) readonly buffer Input1 {
float bias_data[];
};
layout(binding = 2) readonly buffer Input3{
float weight_data[];
};
layout(binding = 3) writeonly buffer Output{
float convolved_image_data[];
};
layout(push_constant) uniform pushBlock {
int in_h;
int in_w;
int out_h;
int out_w;
int stride_h;
int stride_w;
int pad_h;
int pad_w;
int filter_h;
int filter_w;
int dilation_h;
int dilation_w;
int channels;
int batch;
int has_bias;
int M;
int K;
int N;
} p;
layout(local_size_x = LOCAL_SZ_X, local_size_y = 1, local_size_z = 1) in;
void main()
{
int gx = int(gl_GlobalInvocationID.x);
int gy = int(gl_GlobalInvocationID.y);
int gz = int(gl_GlobalInvocationID.z);
if(gx < p.M && gy < p.N && gz < p.batch)
{
float sum = 0.0f;
int output_y = gx / p.out_w;
int output_x = gx % p.out_w;
int org_y = output_y * p.stride_h - p.pad_h;
int org_x = output_x * p.stride_w - p.pad_w;
int weight_off = gy * p.K;
int input_off = gz * p.in_h * p.in_w * p.channels + (org_y * p.in_w + org_x);
for(int c = 0; c < p.channels; c++)
{
for(int y = 0; y < p.filter_h; y++)
{
for(int x = 0; x < p.filter_w; x++)
{
if((org_y + y * p.dilation_h >= 0) && (org_y + y * p.dilation_h < p.in_h) && (org_x + x * p.dilation_w >= 0) && (org_x + x * p.dilation_w < p.in_w))
{
sum += image_data[input_off + x * p.dilation_w] * weight_data[weight_off + x];
}
}
input_off += p.in_w * p.dilation_h;
weight_off += p.filter_w;
}
input_off += p.in_h * p.in_w - p.in_w * p.filter_h * p.dilation_h;
}
int offset = gz * p.M * p.N + gx + gy * p.M;
if (p.has_bias == 1)
sum += bias_data[gy];
convolved_image_data[offset] = sum;
}
}