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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include "CudaCommon.h"
#include "CudaMath.h"
#include "CudaTexture.h"
#include "cuda.h"
//#define umul24(x,y) ((x)*(y))
#if defined(__cplusplus) && CUDA_VERSION < 2000
namespace sofa
{
namespace gpu
{
namespace cuda
{
#endif
extern "C"
{
void CudaVisualModelCuda3f_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3f_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3f_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x);
void CudaVisualModelCuda3f1_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3f1_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3f1_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x);
#ifdef SOFA_GPU_CUDA_DOUBLE
void CudaVisualModelCuda3d_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3d_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3d_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x);
void CudaVisualModelCuda3d1_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3d1_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x);
void CudaVisualModelCuda3d1_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x);
#endif // SOFA_GPU_CUDA_DOUBLE
}
//////////////////////
// GPU-side methods //
//////////////////////
// #define USE_TEXTURE false
#ifdef USE_TEXTURE
#undef USE_TEXTURE
#endif
// no texture is used unless this template is specialized
template<typename real, class TIn>
class CudaCudaVisualModelTextures
{
public:
static __host__ void setX(const void* /*x*/)
{
}
static __inline__ __device__ CudaVec3<real> getX(int i, const TIn* x)
{
return CudaVec3<real>::make(x[i]);
}
static __host__ void setN(const void* /*x*/)
{
}
static __inline__ __device__ CudaVec3<real> getN(int i, const TIn* x)
{
return CudaVec3<real>::make(x[i]);
}
};
#ifdef USE_TEXTURE
static texture<float,1,cudaReadModeElementType> tex_3f_x;
static texture<float,1,cudaReadModeElementType> tex_3f_n;
template<>
class CudaCudaVisualModelTextures<float, CudaVec3<float> >
{
public:
typedef float real;
typedef CudaVec3<real> TIn;
static __host__ void setX(const void* x)
{
static const void* cur = NULL;
if (x!=cur)
{
cudaBindTexture((size_t*)NULL, tex_3f_x, x);
cur = x;
}
}
static __inline__ __device__ CudaVec3<real> getX(int i, const TIn* x)
{
int i3 = umul24(i,3);
float x1 = tex1Dfetch(tex_3f_x, i3);
float x2 = tex1Dfetch(tex_3f_x, i3+1);
float x3 = tex1Dfetch(tex_3f_x, i3+2);
return CudaVec3<real>::make(x1,x2,x3);
}
static __host__ void setN(const void* n)
{
static const void* cur = NULL;
if (n!=cur)
{
cudaBindTexture((size_t*)NULL, tex_3f_n, n);
cur = n;
}
}
static __inline__ __device__ CudaVec3<real> getN(int i, const TIn* n)
{
int i3 = umul24(i,3);
float x1 = tex1Dfetch(tex_3f_n, i3);
float x2 = tex1Dfetch(tex_3f_n, i3+1);
float x3 = tex1Dfetch(tex_3f_n, i3+2);
return CudaVec3<real>::make(x1,x2,x3);
}
};
static texture<float4,1,cudaReadModeElementType> tex_3f1_x;
static texture<float4,1,cudaReadModeElementType> tex_3f1_n;
template<>
class CudaCudaVisualModelTextures<float, CudaVec4<float> >
{
public:
typedef float real;
typedef CudaVec4<real> TIn;
static __host__ void setX(const void* x)
{
static const void* cur = NULL;
if (x!=cur)
{
cudaBindTexture((size_t*)NULL, tex_3f1_x, x);
cur = x;
}
}
static __inline__ __device__ CudaVec3<real> getX(int i, const TIn* x)
{
return CudaVec3<real>::make(tex1Dfetch(tex_3f1_x, i));
}
static __host__ void setN(const void* n)
{
static const void* cur = NULL;
if (n!=cur)
{
cudaBindTexture((size_t*)NULL, tex_3f1_n, n);
cur = n;
}
}
static __inline__ __device__ CudaVec3<real> getN(int i, const TIn* n)
{
return CudaVec3<real>::make(tex1Dfetch(tex_3f1_n, i));
}
};
#endif
template<typename real, class TIn>
__global__ void CudaVisualModelCuda3t_calcTNormals_kernel(int nbElem, const int* elems, real* fnormals, const TIn* x)
{
int index0 = umul24(blockIdx.x,BSIZE); //blockDim.x;
int index1 = threadIdx.x;
int index = index0+index1;
int index3 = umul24(index1,3);
int iext = umul24(blockIdx.x,BSIZE*3)+index1; //index0*3+index1;
__shared__ union {
int itemp[3*BSIZE];
real rtemp[3*BSIZE];
} s;
s.itemp[index1] = elems[iext];
s.itemp[index1+BSIZE] = elems[iext+BSIZE];
s.itemp[index1+2*BSIZE] = elems[iext+2*BSIZE];
__syncthreads();
CudaVec3<real> N = CudaVec3<real>::make(0,0,0);
if (index < nbElem)
{
CudaVec3<real> A = CudaCudaVisualModelTextures<real,TIn>::getX(s.itemp[index3+0], x);
CudaVec3<real> B = CudaCudaVisualModelTextures<real,TIn>::getX(s.itemp[index3+1], x);
CudaVec3<real> C = CudaCudaVisualModelTextures<real,TIn>::getX(s.itemp[index3+2], x);
B -= A;
C -= A;
N = cross(B,C);
N *= invnorm(N);
}
if (sizeof(real) != sizeof(int)) __syncthreads();
s.rtemp[index3+0] = N.x;
s.rtemp[index3+1] = N.y;
s.rtemp[index3+2] = N.z;
__syncthreads();
fnormals[iext] = s.rtemp[index1];
fnormals[iext+BSIZE] = s.rtemp[index1+BSIZE];
fnormals[iext+2*BSIZE] = s.rtemp[index1+2*BSIZE];
}
template<typename real, class TIn>
__global__ void CudaVisualModelCuda3t1_calcTNormals_kernel(int nbElem, const int* elems, CudaVec4<real>* fnormals, const TIn* x)
{
int index0 = umul24(blockIdx.x,BSIZE); //blockDim.x;
int index1 = threadIdx.x;
int index = index0+index1;
int index3 = umul24(index1,3);
int iext = umul24(blockIdx.x,BSIZE*3)+index1; //index0*3+index1;
__shared__ int itemp[3*BSIZE];
itemp[index1] = elems[iext];
itemp[index1+BSIZE] = elems[iext+BSIZE];
itemp[index1+2*BSIZE] = elems[iext+2*BSIZE];
__syncthreads();
CudaVec3<real> N = CudaVec3<real>::make(0,0,0);
if (index < nbElem)
{
CudaVec3<real> A = CudaCudaVisualModelTextures<real,TIn>::getX(itemp[index3+0], x);
CudaVec3<real> B = CudaCudaVisualModelTextures<real,TIn>::getX(itemp[index3+1], x);
CudaVec3<real> C = CudaCudaVisualModelTextures<real,TIn>::getX(itemp[index3+2], x);
B -= A;
C -= A;
N = cross(B,C);
N *= invnorm(N);
}
fnormals[index] = CudaVec4<real>::make(N,0.0f);
}
template<typename real, class TIn>
__global__ void CudaVisualModelCuda3t_calcQNormals_kernel(int nbElem, const int4* elems, real* fnormals, const TIn* x)
{
int index0 = umul24(blockIdx.x,BSIZE); //blockDim.x;
int index1 = threadIdx.x;
int index = index0+index1;
int index3 = umul24(index1,3);
int iext = umul24(blockIdx.x,BSIZE*3)+index1; //index0*3+index1;
__shared__ real rtemp[3*BSIZE];
CudaVec3<real> N = CudaVec3<real>::make(0,0,0);
if (index < nbElem)
{
int4 itemp = elems[index];
CudaVec3<real> A = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.x, x);
CudaVec3<real> B = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.y, x);
CudaVec3<real> C = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.z, x);
CudaVec3<real> D = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.w, x);
C -= A;
D -= B;
N = cross(C,D);
N *= invnorm(N);
}
rtemp[index3+0] = N.x;
rtemp[index3+1] = N.y;
rtemp[index3+2] = N.z;
__syncthreads();
fnormals[iext] = rtemp[index1];
fnormals[iext+BSIZE] = rtemp[index1+BSIZE];
fnormals[iext+2*BSIZE] = rtemp[index1+2*BSIZE];
}
template<typename real, class TIn>
__global__ void CudaVisualModelCuda3t1_calcQNormals_kernel(int nbElem, const int4* elems, CudaVec4<real>* fnormals, const TIn* x)
{
int index0 = umul24(blockIdx.x,BSIZE); //blockDim.x;
int index1 = threadIdx.x;
int index = index0+index1;
CudaVec3<real> N = CudaVec3<real>::make(0,0,0);
if (index < nbElem)
{
int4 itemp = elems[index];
CudaVec3<real> A = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.x, x);
CudaVec3<real> B = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.y, x);
CudaVec3<real> C = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.z, x);
CudaVec3<real> D = CudaCudaVisualModelTextures<real,TIn>::getX(itemp.w, x);
C -= A;
D -= B;
N = cross(C,D);
N *= invnorm(N);
}
fnormals[index] = CudaVec4<real>::make(N,0.0f);
}
template<typename real, class TIn>
__global__ void CudaVisualModelCuda3t_calcVNormals_kernel(int nbVertex, unsigned int nbElemPerVertex, const int* velems, real* vnormals, const TIn* fnormals)
{
int index0 = umul24(blockIdx.x,BSIZE); //blockDim.x;
int index1 = threadIdx.x;
int index3 = umul24(index1,3); //3*index1;
__shared__ real temp[3*BSIZE];
int iext = umul24(blockIdx.x,BSIZE*3)+index1; //index0*3+index1;
CudaVec3<real> n = CudaVec3<real>::make(0.0f,0.0f,0.0f);
velems+=umul24(index0,nbElemPerVertex)+index1;
if (index0+index1 < nbVertex)
{
for (int s = 0;s < nbElemPerVertex; s++)
{
int i = *velems -1;
velems+=BSIZE;
if (i != -1)
{
n += CudaCudaVisualModelTextures<real,TIn>::getN(i,fnormals);
}
}
real invn = invnorm(n);
if (invn < 100000.0)
n *= invn;
}
temp[index3 ] = n.x;
temp[index3+1] = n.y;
temp[index3+2] = n.z;
__syncthreads();
vnormals[iext ] = temp[index1 ];
vnormals[iext+ BSIZE] = temp[index1+ BSIZE];
vnormals[iext+2*BSIZE] = temp[index1+2*BSIZE];
}
template<typename real, class TIn>
__global__ void CudaVisualModelCuda3t1_calcVNormals_kernel(int nbVertex, unsigned int nbElemPerVertex, const int* velems, CudaVec4<real>* vnormals, const TIn* fnormals)
{
const int index0 = umul24(blockIdx.x,BSIZE); //blockDim.x;
const int index1 = threadIdx.x;
const int index = index0 + index1;
CudaVec3<real> n = CudaVec3<real>::make(0.0f,0.0f,0.0f);
velems+=umul24(index0,nbElemPerVertex)+index1;
if (index < nbVertex)
{
for (int s = 0;s < nbElemPerVertex; s++)
{
int i = *velems -1;
velems+=BSIZE;
if (i != -1)
{
n += CudaCudaVisualModelTextures<real,TIn>::getN(i,fnormals);
}
}
real invn = invnorm(n);
if (invn < 100000.0)
n *= invn;
}
vnormals[index] = CudaVec4<real>::make(n,0.0f);
}
//////////////////////
// CPU-side methods //
//////////////////////
void CudaVisualModelCuda3f_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<float,CudaVec3<float> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t_calcTNormals_kernel<float, CudaVec3<float> ><<< grid1, threads1 >>>(nbElem, (const int*)elems, (float*)fnormals, (const CudaVec3<float>*)x);
}
void CudaVisualModelCuda3f_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<float,CudaVec3<float> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t_calcQNormals_kernel<float, CudaVec3<float> ><<< grid1, threads1 >>>(nbElem, (const int4*)elems, (float*)fnormals, (const CudaVec3<float>*)x);
}
void CudaVisualModelCuda3f_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x)
{
dim3 threads2(BSIZE,1);
dim3 grid2((nbVertex+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t_calcVNormals_kernel<float, CudaVec3<float> ><<< grid2, threads2 >>>(nbVertex, nbElemPerVertex, (const int*)velems, (float*)vnormals, (const CudaVec3<float>*)fnormals);
}
void CudaVisualModelCuda3f1_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<float,CudaVec4<float> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t1_calcTNormals_kernel<float, CudaVec4<float> ><<< grid1, threads1 >>>(nbElem, (const int*)elems, (CudaVec4<float>*)fnormals, (const CudaVec4<float>*)x);
}
void CudaVisualModelCuda3f1_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<float,CudaVec4<float> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t1_calcQNormals_kernel<float, CudaVec4<float> ><<< grid1, threads1 >>>(nbElem, (const int4*)elems, (CudaVec4<float>*)fnormals, (const CudaVec4<float>*)x);
}
void CudaVisualModelCuda3f1_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x)
{
dim3 threads2(BSIZE,1);
dim3 grid2((nbVertex+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t1_calcVNormals_kernel<float, CudaVec4<float> ><<< grid2, threads2 >>>(nbVertex, nbElemPerVertex, (const int*)velems, (CudaVec4<float>*)vnormals, (const CudaVec4<float>*)fnormals);
}
#ifdef SOFA_GPU_CUDA_DOUBLE
void CudaVisualModelCuda3d_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<double,CudaVec3<double> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t_calcTNormals_kernel<double, CudaVec3<double> ><<< grid1, threads1 >>>(nbElem, (const int*)elems, (double*)fnormals, (const CudaVec3<double>*)x);
}
void CudaVisualModelCuda3d_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<double,CudaVec3<double> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t_calcQNormals_kernel<double, CudaVec3<double> ><<< grid1, threads1 >>>(nbElem, (const int4*)elems, (double*)fnormals, (const CudaVec3<double>*)x);
}
void CudaVisualModelCuda3d_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x)
{
dim3 threads2(BSIZE,1);
dim3 grid2((nbVertex+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t_calcVNormals_kernel<double, CudaVec3<double> ><<< grid2, threads2 >>>(nbVertex, nbElemPerVertex, (const int*)velems, (double*)vnormals, (const CudaVec3<double>*)fnormals);
}
void CudaVisualModelCuda3d1_calcTNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<double,CudaVec4<double> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t1_calcTNormals_kernel<double, CudaVec4<double> ><<< grid1, threads1 >>>(nbElem, (const int*)elems, (CudaVec4<double>*)fnormals, (const CudaVec4<double>*)x);
}
void CudaVisualModelCuda3d1_calcQNormals(unsigned int nbElem, unsigned int nbVertex, const void* elems, void* fnormals, const void* x)
{
CudaCudaVisualModelTextures<double,CudaVec4<double> >::setX(x);
dim3 threads1(BSIZE,1);
dim3 grid1((nbElem+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t1_calcQNormals_kernel<double, CudaVec4<double> ><<< grid1, threads1 >>>(nbElem, (const int4*)elems, (CudaVec4<double>*)fnormals, (const CudaVec4<double>*)x);
}
void CudaVisualModelCuda3d1_calcVNormals(unsigned int nbElem, unsigned int nbVertex, unsigned int nbElemPerVertex, const void* velems, void* vnormals, const void* fnormals, const void* x)
{
dim3 threads2(BSIZE,1);
dim3 grid2((nbVertex+BSIZE-1)/BSIZE,1);
CudaVisualModelCuda3t1_calcVNormals_kernel<double, CudaVec4<double> ><<< grid2, threads2 >>>(nbVertex, nbElemPerVertex, (const int*)velems, (CudaVec4<double>*)vnormals, (const CudaVec4<double>*)fnormals);
}
#endif // SOFA_GPU_CUDA_DOUBLE
#if defined(__cplusplus) && CUDA_VERSION < 2000
} // namespace cuda
} // namespace gpu
} // namespace sofa
#endif
|