Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12-cl/src/lib/cl-target/ocl-image.c
ViewVC logotype

Annotation of /branches/vis12-cl/src/lib/cl-target/ocl-image.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3120 - (view) (download) (as text)

1 : jhr 3118 /*! \file ocl-image.c
2 :     *
3 :     * \author John Reppy
4 :     *
5 :     * Support for loading
6 :     */
7 :    
8 :     /*
9 :     * COPYRIGHT (c) 2015 The Diderot Project (http://diderot-language.cs.uchicago.edu)
10 :     * All rights reserved.
11 :     */
12 :    
13 :     #ifndef DIDEROT_TARGET_CL
14 :     # define DIDEROT_TARGET_CL
15 :     #endif
16 :    
17 : jhr 3120 #include "Diderot/diderot.h"
18 : jhr 3118 #include "Diderot/shadow-types.h"
19 :    
20 :     // Report an OpenCL error
21 :     STATIC_INLINE void ReportOCLError (OCLWorldPrefix_t *wrld, cl_int sts, const char *msg)
22 :     {
23 :     biffMsgAddf (wrld->errors, "%s: %s", msg, Diderot_OCLErrorString(sts));
24 :     }
25 :    
26 :     // Check the return status of a CL API call. If there is an error, add a message to
27 :     // the biff buffer and return true, otherwise return false.
28 :     STATIC_INLINE bool CheckOCLStatus (OCLWorldPrefix_t *wrld, cl_int sts, const char *msg)
29 :     {
30 :     if (sts != CL_SUCCESS) {
31 :     ReportOCLError (wrld, sts, msg);
32 :     return true;
33 :     }
34 :     else
35 :     return false;
36 :     }
37 :    
38 :     //! load a 1D image into a GPU-side global.
39 :     //! \param wrld the world
40 :     //! \param globals the GPU-side memory object that holds the program globals
41 :     //! \param img the name of the image nrrd file
42 :     //! \param initKern the kernel that initializes the GPU-side global image in question
43 :     //! \param obj the GPU-side memory object that is allocated to hold the image data.
44 :     //! \return true if there are any errors
45 :     //
46 :     bool Diderot_LoadGPUImage1D (OCLWorldPrefix_t *wrld, cl_mem globals, const char *imgName, cl_kernel initKern, cl_mem *objOut)
47 :     {
48 :     Diderot_image1D_t *img;
49 :     Shadow_image1D_t shadow;
50 :     cl_int sts;
51 :    
52 :     if (Diderot_LoadImage1D((WorldPrefix_t *)wrld, imgName, &img)) {
53 :     return true;
54 :     }
55 :    
56 :     // initialize the shadow object
57 :     shadow.size[0] = img->size[0];
58 :     shadow.s = img->s;
59 :     shadow.t = img->t;
60 :    
61 :     // allocate and initialize GPU-side image object
62 :     cl_mem imgObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, sizeof(shadow), &shadow, &sts);
63 :     if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image")) {
64 :     Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
65 :     return true;
66 :     }
67 :    
68 :     // allocate and initialize GPU-side image data
69 :     cl_mem dataObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, img->dataSzb, img->data, &sts);
70 :     if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image data")) {
71 :     Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
72 :     clReleaseMemObject(imgObj);
73 :     return true;
74 :     }
75 :    
76 :     /* invoke the initialization kernel. s*/
77 :     if (((sts = clSetKernelArg (initKern, 0, sizeof(cl_mem), &globals)) != CL_SUCCESS)
78 :     || ((sts = clSetKernelArg (initKern, 1, sizeof(cl_mem), &imgObj)) != CL_SUCCESS)
79 :     || ((sts = clSetKernelArg (initKern, 2, sizeof(cl_mem), &dataObj)) != CL_SUCCESS))
80 :     {
81 :     ReportOCLError (wrld, sts, "error enqueuing image initialization kernel");
82 :     Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
83 :     clReleaseMemObject(imgObj);
84 :     clReleaseMemObject(dataObj);
85 :     return true;
86 :     }
87 :    
88 :     // free CPU-side data
89 :     Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
90 :    
91 :     // free GPU-side shadow object
92 :     if (clReleaseMemObject(imgObj) != CL_SUCCESS) {
93 :     ReportOCLError (wrld, sts, "error freeing output kernel");
94 :     clReleaseMemObject(dataObj);
95 :     return true;
96 :     }
97 :    
98 :     // return data object for future release
99 :     *objOut = dataObj;
100 :    
101 :     return false;
102 :    
103 :     } // Diderot_LoadGPUImage1D
104 :    
105 :     //! load a 2D image into a GPU-side global.
106 :     //! \param wrld the world
107 :     //! \param globals the GPU-side memory object that holds the program globals
108 :     //! \param img the name of the image nrrd file
109 :     //! \param initKern the kernel that initializes the GPU-side global image in question
110 :     //! \param obj the GPU-side memory object that is allocated to hold the image data.
111 :     //! \return true if there are any errors
112 :     //
113 :     bool Diderot_LoadGPUImage2D (OCLWorldPrefix_t *wrld, cl_mem globals, const char *imgName, cl_kernel initKern, cl_mem *objOut)
114 :     {
115 :     Diderot_image2D_t *img;
116 :     Shadow_image2D_t shadow;
117 :     cl_int sts;
118 :    
119 :     if (Diderot_LoadImage2D((WorldPrefix_t *)wrld, imgName, &img)) {
120 :     return true;
121 :     }
122 :    
123 :     // initialize the shadow object
124 :     for (int i = 0; i < 2; i++) {
125 :     shadow.size[i] = img->size[i];
126 :     ShadowVec2 (&(shadow.w2i[i]), img->w2i[i].v);
127 :     ShadowVec2 (&(shadow.w2iT[i]), img->w2iT[i].v);
128 :     }
129 :     ShadowVec2 (&shadow.tVec, img->tVec);
130 :    
131 :     // allocate and initialize GPU-side image object
132 :     cl_mem imgObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, sizeof(shadow), &shadow, &sts);
133 :     if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image")) {
134 :     Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
135 :     return true;
136 :     }
137 :    
138 :     // allocate and initialize GPU-side image data
139 :     cl_mem dataObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, img->dataSzb, img->data, &sts);
140 :     if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image data")) {
141 :     Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
142 :     clReleaseMemObject(imgObj);
143 :     return true;
144 :     }
145 :    
146 :     /* invoke the initialization kernel. s*/
147 :     if (((sts = clSetKernelArg (initKern, 0, sizeof(cl_mem), &globals)) != CL_SUCCESS)
148 :     || ((sts = clSetKernelArg (initKern, 1, sizeof(cl_mem), &imgObj)) != CL_SUCCESS)
149 :     || ((sts = clSetKernelArg (initKern, 2, sizeof(cl_mem), &dataObj)) != CL_SUCCESS))
150 :     {
151 :     ReportOCLError (wrld, sts, "error enqueuing image initialization kernel");
152 :     Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
153 :     clReleaseMemObject(imgObj);
154 :     clReleaseMemObject(dataObj);
155 :     return true;
156 :     }
157 :    
158 :     // free CPU-side data
159 :     Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
160 :    
161 :     // free GPU-side shadow object
162 :     if (clReleaseMemObject(imgObj) != CL_SUCCESS) {
163 :     ReportOCLError (wrld, sts, "error freeing output kernel");
164 :     clReleaseMemObject(dataObj);
165 :     return true;
166 :     }
167 :    
168 :     // return data object for future release
169 :     *objOut = dataObj;
170 :    
171 :     return false;
172 :    
173 :     } // Diderot_LoadGPUImage2D
174 :    
175 :     //! load a 3D image into a GPU-side global.
176 :     //! \param wrld the world
177 :     //! \param globals the GPU-side memory object that holds the program globals
178 :     //! \param img the name of the image nrrd file
179 :     //! \param initKern the kernel that initializes the GPU-side global image in question
180 :     //! \param obj the GPU-side memory object that is allocated to hold the image data.
181 :     //! \return true if there are any errors
182 :     //
183 :     bool Diderot_LoadGPUImage3D (OCLWorldPrefix_t *wrld, cl_mem globals, const char *imgName, cl_kernel initKern, cl_mem *objOut)
184 :     {
185 :     Diderot_image3D_t *img;
186 :     Shadow_image3D_t shadow;
187 :     cl_int sts;
188 :    
189 :     if (Diderot_LoadImage3D((WorldPrefix_t *)wrld, imgName, &img)) {
190 :     return true;
191 :     }
192 :    
193 :     // initialize the shadow object
194 :     for (int i = 0; i < 3; i++) {
195 :     shadow.size[i] = img->size[i];
196 :     ShadowVec3 (&(shadow.w2i[i]), img->w2i[i].v);
197 :     ShadowVec3 (&(shadow.w2iT[i]), img->w2iT[i].v);
198 :     }
199 :     ShadowVec3 (&shadow.tVec, img->tVec);
200 :    
201 :     // allocate and initialize GPU-side image object
202 :     cl_mem imgObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, sizeof(shadow), &shadow, &sts);
203 :     if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image")) {
204 :     Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
205 :     return true;
206 :     }
207 :    
208 :     // allocate and initialize GPU-side image data
209 :     cl_mem dataObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, img->dataSzb, img->data, &sts);
210 :     if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image data")) {
211 :     Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
212 :     clReleaseMemObject(imgObj);
213 :     return true;
214 :     }
215 :    
216 :     /* invoke the initialization kernel. s*/
217 :     if (((sts = clSetKernelArg (initKern, 0, sizeof(cl_mem), &globals)) != CL_SUCCESS)
218 :     || ((sts = clSetKernelArg (initKern, 1, sizeof(cl_mem), &imgObj)) != CL_SUCCESS)
219 :     || ((sts = clSetKernelArg (initKern, 2, sizeof(cl_mem), &dataObj)) != CL_SUCCESS))
220 :     {
221 :     ReportOCLError (wrld, sts, "error enqueuing image initialization kernel");
222 :     Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
223 :     clReleaseMemObject(imgObj);
224 :     clReleaseMemObject(dataObj);
225 :     return true;
226 :     }
227 :    
228 :     // free CPU-side data
229 :     Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
230 :    
231 :     // free GPU-side shadow object
232 :     if (clReleaseMemObject(imgObj) != CL_SUCCESS) {
233 :     ReportOCLError (wrld, sts, "error freeing output kernel");
234 :     clReleaseMemObject(dataObj);
235 :     return true;
236 :     }
237 :    
238 :     // return data object for future release
239 :     *objOut = dataObj;
240 :    
241 :     return false;
242 :    
243 :     } // Diderot_LoadGPUImage3D

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0