1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "harness/compat.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include "procs.h"
25
test_simple_read_image_pitch(cl_device_id device,cl_context cl_context_,cl_command_queue q,int num_elements)26 int test_simple_read_image_pitch(cl_device_id device, cl_context cl_context_, cl_command_queue q, int num_elements)
27 {
28 cl_int err = CL_SUCCESS;
29
30 size_t imageW = 143;
31 size_t imageH = 151;
32 size_t bufferW = 151*4;
33 size_t bufferH = 151;
34
35 size_t pixel_bytes = 4;
36 size_t image_bytes = imageW * imageH * pixel_bytes;
37
38 size_t buffer_bytes = bufferW * bufferH;
39
40 PASSIVE_REQUIRE_IMAGE_SUPPORT( device );
41
42 char* host_image = (char*)malloc(image_bytes);
43 memset(host_image,0x1,image_bytes);
44
45 cl_image_format fmt = { 0 };
46 fmt.image_channel_order = CL_RGBA;
47 fmt.image_channel_data_type = CL_UNORM_INT8;
48
49 cl_image_desc desc = { 0 };
50 desc.image_type = CL_MEM_OBJECT_IMAGE2D;
51 desc.image_width = imageW;
52 desc.image_height = imageH;
53
54 cl_mem image = clCreateImage(cl_context_, CL_MEM_COPY_HOST_PTR|CL_MEM_READ_WRITE, &fmt, &desc, host_image, &err);
55 test_error(err,"clCreateImage");
56
57 char* host_buffer = (char*)malloc(buffer_bytes);
58 memset(host_buffer,0xa,buffer_bytes);
59
60 // Test reading from the image
61 size_t origin[] = { 0, 0, 0 };
62 size_t region[] = { imageW, imageH, 1 };
63
64 err = clEnqueueReadImage(q, image, CL_TRUE, origin, region, bufferW, 0, host_buffer, 0, NULL, NULL);
65 test_error(err,"clEnqueueReadImage");
66
67 size_t errors = 0;
68 for (size_t j=0;j<bufferH;++j) {
69 for (size_t i=0;i<bufferW;++i) {
70 char val = host_buffer[j*bufferW+i];
71 if ((i<imageW*pixel_bytes) && (val != 0x1)) {
72 log_error("Bad value %x in image at (byte: %lu, row: %lu)\n",val,i,j);
73 ++errors;
74 }
75 else if ((i>=imageW*pixel_bytes) && (val != 0xa)) {
76 log_error("Bad value %x outside image at (byte: %lu, row: %lu)\n",val,i,j);
77 ++errors;
78 }
79 }
80 }
81
82 test_error(clReleaseMemObject(image),"clReleaseMemObject");
83 free(host_image);
84 free(host_buffer);
85
86 return errors == 0 ? TEST_PASS : TEST_FAIL;
87 }
88
test_simple_write_image_pitch(cl_device_id device,cl_context cl_context_,cl_command_queue q,int num_elements)89 int test_simple_write_image_pitch(cl_device_id device, cl_context cl_context_, cl_command_queue q, int num_elements)
90 {
91 cl_int err = CL_SUCCESS;
92
93 size_t imageW = 143;
94 size_t imageH = 151;
95 size_t bufferW = 151*4;
96 size_t bufferH = 151;
97
98 size_t pixel_bytes = 4;
99 size_t image_bytes = imageW * imageH * pixel_bytes;
100
101 size_t buffer_bytes = bufferW * bufferH;
102
103 PASSIVE_REQUIRE_IMAGE_SUPPORT( device );
104
105 char* host_image = (char*)malloc(image_bytes);
106 memset(host_image,0x0,image_bytes);
107
108 cl_image_format fmt = { 0 };
109 fmt.image_channel_order = CL_RGBA;
110 fmt.image_channel_data_type = CL_UNORM_INT8;
111
112 cl_image_desc desc = { 0 };
113 desc.image_type = CL_MEM_OBJECT_IMAGE2D;
114 desc.image_width = imageW;
115 desc.image_height = imageH;
116
117 cl_mem image = clCreateImage(cl_context_, CL_MEM_COPY_HOST_PTR|CL_MEM_READ_WRITE, &fmt, &desc, host_image, &err);
118 test_error(err,"clCreateImage");
119
120 char* host_buffer = (char*)malloc(buffer_bytes);
121 memset(host_buffer,0xa,buffer_bytes);
122
123 // Test reading from the image
124 size_t origin[] = { 0, 0, 0 };
125 size_t region[] = { imageW, imageH, 1 };
126
127 err = clEnqueueWriteImage(q, image, CL_TRUE, origin, region, bufferW, 0, host_buffer, 0, NULL, NULL);
128 test_error(err,"clEnqueueWriteImage");
129
130 size_t mapped_pitch = 0;
131 char* mapped_image = (char*)clEnqueueMapImage(q, image, CL_TRUE, CL_MAP_READ, origin, region, &mapped_pitch, NULL, 0, NULL, NULL, &err);
132 test_error(err,"clEnqueueMapImage");
133
134 size_t errors = 0;
135 for (size_t j=0;j<imageH;++j) {
136 for (size_t i=0;i<mapped_pitch;++i) {
137 char val = mapped_image[j*mapped_pitch+i];
138 if ((i<imageW*pixel_bytes) && (val != 0xa)) {
139 log_error("Bad value %x in image at (byte: %lu, row: %lu)\n",val,i,j);
140 ++errors;
141 }
142 }
143 }
144
145 err = clEnqueueUnmapMemObject(q, image, (void *)mapped_image, 0, 0, 0);
146 test_error(err,"clEnqueueUnmapMemObject");
147
148 test_error(clReleaseMemObject(image),"clReleaseMemObject");
149 free(host_image);
150 free(host_buffer);
151
152 return errors == 0 ? TEST_PASS : TEST_FAIL;
153 }
154