title: Edit Cuda Texture Object
body: <p>I've created a texture object which references 2d pitched memory given a specified <code>Width</code> &amp; <code>Height</code>. Nothing special there. I can use this texture object to read from the texture and make use of all of the texture 'stuff'.</p>
  
  <p>My question regards <strong>how to edit the texture</strong> and is quite straight forward. Some examples show the creation of a <code>cudaSurfaceObject_t</code> and use <code>surf2Dwrite()</code> in a kernel to edit the texture. </p>
  
  <p><strong>Why should or shouldn't I use a surface instead of the raw memory via the memory pointer to edit the texture contents?</strong></p>
  
  <p>Is this the main difference? The surface object will ensure that a read from the texture gets the correct value from texture cache. Editing the raw buffer will not update a value currently stored in the texture cache, thus a subsequent read of the updated value may not contain the correct value?</p>
  
  <p>Assign 2d pitched memory:</p>
  
  <pre><code>float* d_MyBuffer;
  unsigned int Pitch;
  cudaMallocPitch&lt;float&gt;(&amp;d_MyBuffer, &amp;Pitch, Width * sizeof(float), Height);
  </code></pre>
  
  <p>Create the texture object:</p>
  
  <pre><code>cudaTextureObject_t t_MyTexture;
  
  cudaResourceDesc resDescr;
  memset(&amp;resDescr, 0, sizeof(cudaResourceDesc));
  resDescr.resType = cudaResourceType::cudaResourceTypePitch2D;
  resDescr.res.pitch2D.desc = cudaCreateChannelDesc&lt;float&gt;();
  resDescr.res.pitch2D.devPtr = d_MyBuffer;
  resDescr.res.pitch2D.height = Height;
  resDescr.res.pitch2D.pitchInBytes = Pitch;
  resDescr.res.pitch2D.width = Width;
  
  cudaTextureDesc  texDescr;
  memset(&amp;texDescr, 0, sizeof(cudaTextureDesc));
  texDescr.normalizedCoords = 1;
  texDescr.filterMode = cudaFilterModeLinear;
  texDescr.addressMode[0] = cudaAddressModeClamp;
  texDescr.addressMode[1] = cudaAddressModeClamp;
  texDescr.addressMode[2] = cudaAddressModeClamp;
  texDescr.readMode = cudaReadModeElementType;
  
  cudaCreateTextureObject(&amp;t_MyTexture, &amp;resDescr, &amp;texDescr, NULL);
  </code></pre>
  
  <p>Create surface:</p>
  
  <pre><code>cudaSurfaceObject_t s_MySurface;
  
  cudaResourceDesc surfRes;
  memset(&amp;surfRes, 0, sizeof(cudaResourceDesc));
  surfRes.resType = cudaResourceType::cudaResourceTypePitch2D;
  surfRes.res.pitch2D.desc = cudaCreateChannelDesc&lt;float&gt;();
  surfRes.res.pitch2D.devPtr = d_MyBuffer;
  surfRes.res.pitch2D.height = Height;
  surfRes.res.pitch2D.pitchInBytes = Pitch;
  surfRes.res.pitch2D.width = Width;
  
  cudaCreateSurfaceObject(&amp;s_MySurface,&amp;surfRes)
  </code></pre>
  
  <p>In a kernel should I prefer to use:</p>
  
  <pre><code>surf2Dwrite(value, s_MySurface, x, y);
  </code></pre>
  
  <p>Or in a kernel should I prefer to use:</p>
  
  <pre><code>d_MyBuffer[x + Pitch*y] = value;
  </code></pre>
  
  <p>I apologize for any typos in the code. This is more of a conceptual question rather than a coding syntax question.</p>