|
| 1 | +/********************************************************************** |
| 2 | + Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved. |
| 3 | + |
| 4 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + of this software and associated documentation files (the "Software"), to deal |
| 6 | + in the Software without restriction, including without limitation the rights |
| 7 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + copies of the Software, and to permit persons to whom the Software is |
| 9 | + furnished to do so, subject to the following conditions: |
| 10 | + |
| 11 | + The above copyright notice and this permission notice shall be included in |
| 12 | + all copies or substantial portions of the Software. |
| 13 | + |
| 14 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + THE SOFTWARE. |
| 21 | + ********************************************************************/ |
| 22 | +#ifndef KERNEL_CL |
| 23 | +#define KERNEL_CL |
| 24 | + |
| 25 | +typedef struct _Ray |
| 26 | +{ |
| 27 | + /// xyz - origin, w - max range |
| 28 | + float4 o; |
| 29 | + /// xyz - direction, w - time |
| 30 | + float4 d; |
| 31 | + /// x - ray mask, y - activity flag |
| 32 | + int2 extra; |
| 33 | + /// Padding |
| 34 | + float2 padding; |
| 35 | +} Ray; |
| 36 | + |
| 37 | +typedef struct _Camera |
| 38 | + { |
| 39 | + // Camera coordinate frame |
| 40 | + float3 forward; |
| 41 | + float3 up; |
| 42 | + float3 p; |
| 43 | + |
| 44 | + // Near and far Z |
| 45 | + float2 zcap; |
| 46 | + } Camera; |
| 47 | + |
| 48 | +typedef struct _Intersection |
| 49 | +{ |
| 50 | + // id of a shape |
| 51 | + int shapeid; |
| 52 | + // Primitive index |
| 53 | + int primid; |
| 54 | + // Padding elements |
| 55 | + int padding0; |
| 56 | + int padding1; |
| 57 | + |
| 58 | + // uv - hit barycentrics, w - ray distance |
| 59 | + float4 uvwt; |
| 60 | +} Intersection; |
| 61 | + |
| 62 | +float4 ConvertFromBarycentric(__global const float* vec, |
| 63 | + __global const int* ind, |
| 64 | + int prim_id, |
| 65 | + __global const float4* uvwt) |
| 66 | +{ |
| 67 | + float4 a = (float4)(vec[ind[prim_id * 3] * 3], |
| 68 | + vec[ind[prim_id * 3] * 3 + 1], |
| 69 | + vec[ind[prim_id * 3] * 3 + 2], 0.f); |
| 70 | + |
| 71 | + float4 b = (float4)(vec[ind[prim_id * 3 + 1] * 3], |
| 72 | + vec[ind[prim_id * 3 + 1] * 3 + 1], |
| 73 | + vec[ind[prim_id * 3 + 1] * 3 + 2], 0.f); |
| 74 | + |
| 75 | + float4 c = (float4)(vec[ind[prim_id * 3 + 2] * 3], |
| 76 | + vec[ind[prim_id * 3 + 2] * 3 + 1], |
| 77 | + vec[ind[prim_id * 3 + 2] * 3 + 2], 0.f); |
| 78 | + return a * (1 - uvwt->x - uvwt->y) + b * uvwt->x + c * uvwt->y; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +__kernel void GeneratePerspectiveRays(__global Ray* rays, |
| 83 | + __global const Camera* cam, |
| 84 | + int width, |
| 85 | + int height) |
| 86 | +{ |
| 87 | + int2 globalid; |
| 88 | + globalid.x = get_global_id(0); |
| 89 | + globalid.y = get_global_id(1); |
| 90 | + |
| 91 | + // Check borders |
| 92 | + if (globalid.x < width && globalid.y < height) |
| 93 | + { |
| 94 | + const float xstep = 2.f / (float)width; |
| 95 | + const float ystep = 2.f / (float)height; |
| 96 | + float x = -1.f + xstep * (float)globalid.x; |
| 97 | + float y = ystep * (float)globalid.y; |
| 98 | + float z = cam->zcap.x; |
| 99 | + // Perspective view |
| 100 | + int k = globalid.y * width + globalid.x; |
| 101 | + rays[k].o.xyz = cam->p; |
| 102 | + rays[k].d.x = x - cam->p.x; |
| 103 | + rays[k].d.y = y - cam->p.y; |
| 104 | + rays[k].d.z = z - cam->p.z; |
| 105 | + rays[k].o.w = cam->zcap.y; |
| 106 | + |
| 107 | + rays[k].extra.x = 0xFFFFFFFF; |
| 108 | + rays[k].extra.y = 0xFFFFFFFF; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +__kernel void GenerateShadowRays(__global Ray* rays, |
| 113 | + //scene |
| 114 | + __global float* positions, |
| 115 | + __global float* normals, |
| 116 | + __global int* ids, |
| 117 | + __global float* colors, |
| 118 | + __global int* indents, |
| 119 | + //intersection |
| 120 | + __global Intersection* isect, |
| 121 | + //light pos |
| 122 | + float4 light, |
| 123 | + //window size |
| 124 | + int width, |
| 125 | + int height) |
| 126 | +{ |
| 127 | + int2 globalid; |
| 128 | + globalid.x = get_global_id(0); |
| 129 | + globalid.y = get_global_id(1); |
| 130 | + |
| 131 | + // Check borders |
| 132 | + if (globalid.x < width && globalid.y < height) |
| 133 | + { |
| 134 | + int k = globalid.y * width + globalid.x; |
| 135 | + int shape_id = isect[k].shapeid; |
| 136 | + int prim_id = isect[k].primid; |
| 137 | + |
| 138 | + // Need shadow rays only for intersections |
| 139 | + if (shape_id == -1 || prim_id == -1) |
| 140 | + { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + // Calculate position of the intersection point |
| 145 | + int ind = indents[shape_id]; |
| 146 | + float4 pos = ConvertFromBarycentric(positions + ind*3, ids + ind*3, prim_id, &isect[k].uvwt); |
| 147 | + float4 dir = light - pos; |
| 148 | + rays[k].d = normalize(dir); |
| 149 | + rays[k].o = pos + rays[k].d * FLT_EPSILON; |
| 150 | + rays[k].o.w = length(dir); |
| 151 | + |
| 152 | + rays[k].extra.x = 0xFFFFFFFF; |
| 153 | + rays[k].extra.y = 0xFFFFFFFF; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +__kernel void Shading(//scene |
| 159 | + __global float* positions, |
| 160 | + __global float* normals, |
| 161 | + __global int* ids, |
| 162 | + __global float* colors, |
| 163 | + __global int* indents, |
| 164 | + //intersection |
| 165 | + __global Intersection* isect, |
| 166 | + __global const int* occl, |
| 167 | + //light pos |
| 168 | + float4 light, |
| 169 | + int width, |
| 170 | + int height, |
| 171 | + __global unsigned char* out) |
| 172 | +{ |
| 173 | + int2 globalid; |
| 174 | + globalid.x = get_global_id(0); |
| 175 | + globalid.y = get_global_id(1); |
| 176 | + |
| 177 | + // Check borders |
| 178 | + if (globalid.x < width && globalid.y < height) |
| 179 | + { |
| 180 | + int k = globalid.y * width + globalid.x; |
| 181 | + int shape_id = isect[k].shapeid; |
| 182 | + int prim_id = isect[k].primid; |
| 183 | + |
| 184 | + if (shape_id != -1 && prim_id != -1/* && occl[k] != -1*/) |
| 185 | + { |
| 186 | + // Calculate position and normal of the intersection point |
| 187 | + int ind = indents[shape_id]; |
| 188 | + float4 pos = ConvertFromBarycentric(positions + ind*3, ids + ind*3, prim_id, &isect[k].uvwt); |
| 189 | + float4 norm = ConvertFromBarycentric(normals + ind*3, ids + ind*3, prim_id, &isect[k].uvwt); |
| 190 | + norm = normalize(norm); |
| 191 | + |
| 192 | + // float4 diff_col = (float4)( 0.5f, |
| 193 | + // 0.5f, |
| 194 | + // 0.5f, 1.f); |
| 195 | + int color_id = (ind + prim_id)*3; |
| 196 | + float4 diff_col = (float4)( colors[color_id], |
| 197 | + colors[color_id + 1], |
| 198 | + colors[color_id + 2], 1.f); |
| 199 | + |
| 200 | + |
| 201 | + // Calculate lighting |
| 202 | + float4 col = (float4)( 0.f, 0.f, 0.f, 0.f ); |
| 203 | + float4 light_dir = normalize(light - pos); |
| 204 | + float dot_prod = dot(norm, light_dir); |
| 205 | + if (dot_prod > 0) |
| 206 | + col += dot_prod * diff_col; |
| 207 | + |
| 208 | + out[k * 4] = col.x * 255; |
| 209 | + out[k * 4 + 1] = col.y * 255; |
| 210 | + out[k * 4 + 2] = col.z * 255; |
| 211 | + out[k * 4 + 3] = 255; |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | +#endif //KERNEL_CL |
0 commit comments