Normalized vs Unnormalized

Hi,

I am facing the following problem :

When using Unnormalized coordinates all values of my 4 by 4 Texture are read out properly
e.g.

printf("%i %i %i %i \n " , tex2D(refTex,0.5,0.5) , tex2D(refTex,1.5,0.5) , tex2D(refTex,2.5,0.5) ,tex2D(refTex,3.5,0.5) );
printf("%i %i %i %i \n " , tex2D(refTex,0.5,1.5) , tex2D(refTex,1.5,1.5) , tex2D(refTex,2.5,1.5) ,tex2D(refTex,3.5,1.5) );
printf("%i %i %i %i \n " , tex2D(refTex,0.5,2.5) , tex2D(refTex,1.5,2.5) , tex2D(refTex,2.5,2.5) ,tex2D(refTex,3.5,2.5) );
printf("%i %i %i %i \n " , tex2D(refTex,0.5,3.5) , tex2D(refTex,1.5,3.5) , tex2D(refTex,2.5,3.5) ,tex2D(refTex,3.5,3.5) );

BUT when using normalized coordinates
e.g.

printf("%i %i %i %i \n " , tex2D(refTex,0. /4.0,0.) , tex2D(refTex,1.0/4.0) , tex2D(refTex,2.0/4,0.0) ,tex2D(refTex,3.0/4.0,0.0) );

I get the folowing numbers
193 177 129 225
although the values in the texture lie in the range [1-16]

Normalized coordinate access is cruscial to me since I want to use the WRAP access mode for textures…

HERE IS THE CODE

//constructs a 4 by 4 matrix of type unsigned int
this->buildDitherMatrix(ditherMatrix);
//this->buildButtingMatrix(buttingMatrix);

try
{
// allocate array and copy image data
CUarray cu_array;
CUDA_ARRAY_DESCRIPTOR desc;
desc.Format = CU_AD_FORMAT_UNSIGNED_INT8;
desc.NumChannels = 1;
desc.Width = 4;
desc.Height = 4;

errorCode = cuArrayCreate( &cu_array, &desc );
cuErr(errorCode, "Creating array failed");
CUDA_MEMCPY2D copyParam;
memset(&copyParam, 0, sizeof(copyParam));
copyParam.dstMemoryType = CU_MEMORYTYPE_ARRAY;
copyParam.dstArray = cu_array;
copyParam.srcMemoryType = CU_MEMORYTYPE_HOST;	
copyParam.srcHost = ditherMatrix;
copyParam.srcPitch = desc.Width* sizeof(uint8_t);
copyParam.WidthInBytes = copyParam.srcPitch;
copyParam.Height = desc.Height;
errorCode = cuMemcpy2D(&copyParam);
cuErr(errorCode, "Copying dither matrix to CUDA array failed");


std::cout<<copyParam.Height<<std::endl;
std::cout<<copyParam.WidthInBytes<<std::endl;
std::cout<<copyParam.srcPitch<<std::endl;
std::cout<<copyParam.srcXInBytes<<std::endl;
// set texture parameters
CUtexref cu_texref;
errorCode= (cuModuleGetTexRef(&cu_texref, cuModule, "refTex"));
cuErr(errorCode, "Fetching reference to global texture failed");
errorCode= (cuTexRefSetArray(cu_texref, cu_array, CU_TRSA_OVERRIDE_FORMAT));
cuErr(errorCode, "Set CUDA array format failed");
errorCode= (cuTexRefSetAddressMode(cu_texref, 0, CU_TR_ADDRESS_MODE_WRAP));
cuErr(errorCode, "Set CUDA addressing mode failed");
errorCode= (cuTexRefSetAddressMode(cu_texref, 1, CU_TR_ADDRESS_MODE_WRAP));
cuErr(errorCode, "Set CUDA addressing mode failed");
errorCode= (cuTexRefSetFilterMode(cu_texref, CU_TR_FILTER_MODE_LINEAR));
cuErr(errorCode, "Set CUDA filtering mode failed");


errorCode= (cuTexRefSetFlags(cu_texref, CU_TRSF_NORMALIZED_COORDINATES));
//errorCode= (cuTexRefSetFlags(cu_texref, CU_TRSF_READ_AS_INTEGER));
cuErr(errorCode, "Set CUDA normalized coordinate addressing mode failed");
errorCode= (cuTexRefSetFormat(cu_texref,   CU_AD_FORMAT_UNSIGNED_INT8, 1));
cuErr(errorCode, "Set CUDA texture data type failed");



CUfunction hfunc;
//CUresult cuModuleGetFunction ( hfunc, CUmodule hmod, const char* name ) 
    errorCode = cuModuleGetFunction( &hfunc, cuModule, "Png_kernel" );

cuErr(errorCode, "Get module functionPNG_TEXTURE failed");
errorCode= cuParamSetTexRef(hfunc, CU_PARAM_TR_DEFAULT, cu_texref);
cuErr(errorCode, "Bind CUDA array totexture failed");

}
catch(Exception &e)
{
throw e;
}
}

Thank you for your help.

Hi,

I am facing the following problem :

When using Unnormalized coordinates all values of my 4 by 4 Texture are read out properly
e.g.

printf("%i %i %i %i \n " , tex2D(refTex,0.5,0.5) , tex2D(refTex,1.5,0.5) ,

BUT when using normalized coordinates
e.g.

printf("%i %i %i %i \n " ,tex2D(refTex,0.0,0.0) , tex2D(refTex,0.25,0.0) , tex2D(refTex,0.5,0.0)  ,tex2D(refTex,0.75,0.0) );

I get the folowing numbers
193 145 129 129

although the values in the texture lie in the range [1-16]

Normalized coordinate access is crucial to me since I want to use the WRAP access mode for textures…

HERE IS THE CODE

void Output::initTextureMem(CUmodule &cuModule)
{
  CUresult errorCode;
  CUdeviceptr devPtr;
  uint8_t ditherMatrix[16];
  //uint8_t buttingMatrix[16];

//fills 4 by 4 matrix with uint8_t values
  this->buildDitherMatrix(ditherMatrix);
  //this->buildButtingMatrix(buttingMatrix);

  try
  {
    // allocate array and copy image data
    CUarray cu_array;
    CUDA_ARRAY_DESCRIPTOR desc;
    desc.Format = CU_AD_FORMAT_UNSIGNED_INT8;
    desc.NumChannels = 1;
    desc.Width = 4;
    desc.Height = 4;
    
    errorCode = cuArrayCreate( &cu_array, &desc );
    cuErr(errorCode, "Creating array failed");
    CUDA_MEMCPY2D copyParam;
    memset(&copyParam, 0, sizeof(copyParam));
    copyParam.dstMemoryType = CU_MEMORYTYPE_ARRAY;
    copyParam.dstArray = cu_array;
    copyParam.srcMemoryType = CU_MEMORYTYPE_HOST;	
    copyParam.srcHost = ditherMatrix;
    copyParam.srcPitch = desc.Width* sizeof(uint8_t);
    copyParam.WidthInBytes = copyParam.srcPitch;
    copyParam.Height = desc.Height;
    errorCode = cuMemcpy2D(&copyParam);
    cuErr(errorCode, "Copying dither matrix to CUDA array failed");
    
    
    std::cout<<copyParam.Height<<std::endl;
    std::cout<<copyParam.WidthInBytes<<std::endl;
    std::cout<<copyParam.srcPitch<<std::endl;
    std::cout<<copyParam.srcXInBytes<<std::endl;
    // set texture parameters
    CUtexref cu_texref;
    errorCode= (cuModuleGetTexRef(&cu_texref, cuModule, "refTex"));
    cuErr(errorCode, "Fetching reference to global texture failed");
    errorCode= (cuTexRefSetArray(cu_texref, cu_array, CU_TRSA_OVERRIDE_FORMAT));
    cuErr(errorCode, "Set CUDA array format failed");
    errorCode= (cuTexRefSetAddressMode(cu_texref, 0, CU_TR_ADDRESS_MODE_WRAP));
    cuErr(errorCode, "Set CUDA addressing mode failed");
    errorCode= (cuTexRefSetAddressMode(cu_texref, 1, CU_TR_ADDRESS_MODE_WRAP));
    cuErr(errorCode, "Set CUDA addressing mode failed");
    errorCode= (cuTexRefSetFilterMode(cu_texref, CU_TR_FILTER_MODE_POINT));
    //errorCode= (cuTexRefSetFilterMode(cu_texref, CU_TR_FILTER_MODE_LINEAR));
    cuErr(errorCode, "Set CUDA filtering mode failed");
    

    errorCode= (cuTexRefSetFlags(cu_texref, CU_TRSF_NORMALIZED_COORDINATES));
   // errorCode= (cuTexRefSetFlags(cu_texref, CU_TRSF_READ_AS_INTEGER));
    cuErr(errorCode, "Set CUDA normalized coordinate addressing mode failed");
    errorCode= (cuTexRefSetFormat(cu_texref,   CU_AD_FORMAT_UNSIGNED_INT8, 1));
    cuErr(errorCode, "Set CUDA texture data type failed");
    
    
    
    CUfunction hfunc;
    //CUresult cuModuleGetFunction ( hfunc, CUmodule hmod, const char* name ) 
        errorCode = cuModuleGetFunction( &hfunc, cuModule, "Png_kernel" );
   cuErr(errorCode, "Get module functionPNG_TEXTURE failed");
	errorCode= cuParamSetTexRef(hfunc, CU_PARAM_TR_DEFAULT, cu_texref);
    cuErr(errorCode, "Bind CUDA array totexture failed");
   
  }
  catch(Exception &e)
  {
    throw e;
  }
}

Thank you for your help.