File: prism-opencl.html

package info (click to toggle)
node-prismjs 1.30.0%2Bdfsg%2B~1.26.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,220 kB
  • sloc: javascript: 27,628; makefile: 9; sh: 7; awk: 4
file content (82 lines) | stat: -rw-r--r-- 3,469 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<p><strong>Note:</strong> Use the class <code class="language-none">"language-opencl"</code> for OpenCL kernel code.
	Host code is automatically highlighted with the <code class="language-none">"language-c"</code>
	or <code class="language-none">"language-cpp"</code> class.
</p>

<h2>OpenCL host code</h2>
<pre class="language-cpp"><code>// OpenCL functions, constants, etc. are also highlighted in OpenCL host code in the c or cpp language
cl::Event KernelFilterImages::runSingle(const cl::Image2D& imgSrc, SPImage2D& imgDst)
{
	const size_t rows = imgSrc.getImageInfo&lt;CL_IMAGE_HEIGHT>();
	const size_t cols = imgSrc.getImageInfo&lt;CL_IMAGE_WIDTH>();

	ASSERT(rows > 0 && cols > 0, "The image object seems to be invalid, no rows/cols set");
	ASSERT(imgSrc.getImageInfo&lt;CL_IMAGE_FORMAT>().image_channel_data_type == CL_FLOAT, "Only float type images are supported");
	ASSERT(imgSrc.getInfo&lt;CL_MEM_FLAGS>() == CL_MEM_READ_ONLY || imgSrc.getInfo&lt;CL_MEM_FLAGS>() == CL_MEM_READ_WRITE, "Can't read the input image");

	imgDst = std::make_shared&lt;cl::Image2D>(*context, CL_MEM_READ_WRITE, cl::ImageFormat(CL_R, CL_FLOAT), cols, rows);

	cl::Kernel kernel(*program, "filter_single");
	kernel.setArg(0, imgSrc);
	kernel.setArg(1, *imgDst);
	kernel.setArg(2, bufferKernel1);
	kernel.setArg(3, kernel1.rows);
	kernel.setArg(4, kernel1.rows / 2);
	kernel.setArg(5, kernel1.cols);
	kernel.setArg(6, kernel1.cols / 2);
	kernel.setArg(7, border);

	cl::Event eventFilter;
	const cl::NDRange global(cols, rows);
	queue->enqueueNDRangeKernel(kernel, cl::NullRange, global, cl::NullRange, &events, &eventFilter);
}</code></pre>

<h2>OpenCL kernel code</h2>
<pre><code>// CLK_ADDRESS_CLAMP_TO_EDGE = aaa|abcd|ddd
constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
typedef float type_single;

type_single filter_sum_single_3x3(read_only image2d_t imgIn,
                                  constant float* filterKernel,
                                  const int2 coordBase,
                                  const int border)
{
    type_single sum = (type_single)(0.0f);
    const int rows = get_image_height(imgIn);
    const int cols = get_image_width(imgIn);
    int2 coordCurrent;
    int2 coordBorder;
    float color;

    // Image patch is row-wise accessed
    // Filter kernel is centred in the middle
    #pragma unroll
    for (int y = -ROWS_HALF_3x3; y &lt;= ROWS_HALF_3x3; ++y)       // Start at the top left corner of the filter
    {
        coordCurrent.y = coordBase.y + y;
        #pragma unroll
        for (int x = -COLS_HALF_3x3; x &lt;= COLS_HALF_3x3; ++x)   // And end at the bottom right corner
        {
            coordCurrent.x = coordBase.x + x;
            coordBorder = borderCoordinate(coordCurrent, rows, cols, border);
            color = read_imagef(imgIn, sampler, coordBorder).x;

            const int idx = (y + ROWS_HALF_3x3) * COLS_3x3 + x + COLS_HALF_3x3;
            sum += color * filterKernel[idx];
        }
    }

    return sum;
}

kernel void filter_single_3x3(read_only image2d_t imgIn,
                              write_only image2d_t imgOut,
                              constant float* filterKernel,
                              const int border)
{
    int2 coordBase = (int2)(get_global_id(0), get_global_id(1));

    type_single sum = filter_sum_single_3x3(imgIn, filterKernel, coordBase, border);

    write_imagef(imgOut, coordBase, sum);
}</code></pre>