void CudaModule::setTexRef( const std::string& name, CUarray cudaArray, bool wrap, bool bilinear, bool normalizedCoords, bool readAsInt) { U32 flags = 0; if (normalizedCoords) { flags |= CU_TRSF_NORMALIZED_COORDINATES; } if (readAsInt) { flags |= CU_TRSF_READ_AS_INTEGER; } CUaddress_mode addressMode; CUfilter_mode filterMode; addressMode = (wrap) ? CU_TR_ADDRESS_MODE_WRAP : CU_TR_ADDRESS_MODE_CLAMP; filterMode = (bilinear) ? CU_TR_FILTER_MODE_LINEAR : CU_TR_FILTER_MODE_POINT; CUtexref texRef = getTexRef(name); for (int dim=0; dim<3; ++dim) { checkError( "cuTexRefSetAddressMode", cuTexRefSetAddressMode(texRef, dim, addressMode)); } checkError("cuTexRefSetFilterMode", cuTexRefSetFilterMode(texRef, filterMode)); checkError("cuTexRefSetFlags", cuTexRefSetFlags(texRef, flags)); checkError("cuTexRefSetArray", cuTexRefSetArray(texRef, cudaArray, CU_TRSA_OVERRIDE_FORMAT)); }
CUresult CuModule::BindArrayTexture(const std::string& name, CuTexture* texture, const CuTexSamplerAttr& sampler) { TexBind* texBind; CUresult result = FindTexRef(name, &texBind); HANDLE_RESULT(); SetSampler(texBind, sampler); result = cuTexRefSetArray(texBind->texRef, texture->Handle(), 0); HANDLE_RESULT(); texBind->texture = texture; texBind->mem.reset(); return CUDA_SUCCESS; }
void swanMakeTexture1DEx( const char *modname, const char *texname, size_t width, void *ptr, size_t typesize, int flags ) { int err; // get the texture CUtexref cu_texref; int mode, channels; CUarray array; CUDA_MEMCPY2D copyParam; CUDA_ARRAY_DESCRIPTOR p; // get the module CUmodule mod = swanGetModule( modname ); err = cuModuleGetTexRef(&cu_texref, mod, texname ); if( err != CUDA_SUCCESS) { error( "swanMakeTexture1D failed -- texture not found" ); } p.Width = width; p.Height= 1; mode = flags & TEXTURE_TYPE_MASK; channels = typesize / sizeof(float); switch( mode ) { case TEXTURE_FLOAT: p.Format = CU_AD_FORMAT_FLOAT; p.NumChannels = channels; break; case TEXTURE_INT: p.Format = CU_AD_FORMAT_SIGNED_INT32; p.NumChannels = channels; break; case TEXTURE_UINT: p.Format = CU_AD_FORMAT_UNSIGNED_INT32; p.NumChannels = channels; break; default: error( "swanMakeTexture1D failed -- invalid format" ); } err = cuArrayCreate( &array , &p); if( err != CUDA_SUCCESS) { error( "swanMakeTexture1D failed -- array create failed" ); } memset(©Param, 0, sizeof(copyParam)); copyParam.dstMemoryType = CU_MEMORYTYPE_ARRAY; copyParam.dstArray = array; copyParam.srcMemoryType = CU_MEMORYTYPE_HOST; copyParam.srcHost = ptr; copyParam.srcPitch = width * sizeof(float); copyParam.WidthInBytes = copyParam.srcPitch; copyParam.Height = 1; // err = cuMemcpy2D(©Param); err = cuMemcpyHtoA( array, 0, ptr, typesize * width ); if( err != CUDA_SUCCESS) { error( "swanMakeTexture1D failed -- memcpy failed" ); } err = cuTexRefSetArray ( cu_texref, array, CU_TRSA_OVERRIDE_FORMAT ); if( err != CUDA_SUCCESS) { error( "swanMakeTexture1D failed -- setarray failed" ); } if( (flags & TEXTURE_INTERPOLATE) == TEXTURE_INTERPOLATE ) { err = cuTexRefSetFilterMode( cu_texref, CU_TR_FILTER_MODE_LINEAR ); } else { err = cuTexRefSetFilterMode( cu_texref, CU_TR_FILTER_MODE_POINT ); } if( err != CUDA_SUCCESS) { error( "swanBindToTexture1D failed -- setfiltermode failed" ); } if( (flags & TEXTURE_NORMALISE ) == TEXTURE_NORMALISE ) { err = cuTexRefSetFlags(cu_texref, CU_TRSF_NORMALIZED_COORDINATES); err |= cuTexRefSetAddressMode(cu_texref, 0, CU_TR_ADDRESS_MODE_CLAMP); err |= cuTexRefSetAddressMode(cu_texref, 1, CU_TR_ADDRESS_MODE_CLAMP); if( err != CUDA_SUCCESS) { error( "swanBindToTexture1D failed -- setflags 1 failed" ); } } err = cuTexRefSetFormat( cu_texref, CU_AD_FORMAT_FLOAT, channels ); if( err != CUDA_SUCCESS) { error( "swanBindToTexture1D failed -- setformat failed" ); } //printf("TEX BIND DONE\n"); }