# texture memory - unnormalizing.

**URL:** <https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477>\
**Category:** CUDA Programming and Performance\
**Created:** [September 13, 2007, 2:24pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477 "2007-09-13T14:24:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yunior](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Yunior](https://forums.developer.nvidia.com/u/Yunior)\
**Post date:** [September 13, 2007, 2:24pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/1 "2007-09-13T14:24:25Z")

</div>

Hi Mates,

Simple question - is there some simple way to unnormalize result of data received from texture memory ?

TTFN,

Yunior

---

<div class="post-metadata">

**Author:** ![wumpus](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/wumpus/32/12040_2.png) [@wumpus](https://forums.developer.nvidia.com/u/wumpus)\
**Post date:** [September 13, 2007, 2:45pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/2 "2007-09-13T14:45:44Z")

</div>

I just wrote the following test program that does just that: read a texture and unnormalize, as to end up with the original values

```auto
texture<float, 2, cudaReadModeNormalizedFloat> tex;

__device__ uint8_t clamp_u8(int16_t i)

{

    return min(max(i, 0), 255);

}

__global__ void

transformKernel( uint8_t * g_odata, int stride, int width, int height)

{

    unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;

    unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;

   g_odata[y*stride + x] = clamp_u8(tex2D(tex, x+0.5, y+0.5)*256.0);

}

```

---

<div class="post-metadata">

**Author:** ![Yunior](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Yunior](https://forums.developer.nvidia.com/u/Yunior)\
**Post date:** [September 13, 2007, 3:11pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/3 "2007-09-13T15:11:57Z")

</div>

> [@](#):
>
> I just wrote the following test program that does just that: read a texture and unnormalize, as to end up with the original values
> 
> ```auto
> texture<float, 2, cudaReadModeNormalizedFloat> tex;
> 
> __device__ uint8_t clamp_u8(int16_t i)
> 
> {
> 
>    return min(max(i, 0), 255);
> 
> }
> 
> __global__ void
> 
> transformKernel( uint8_t * g_odata, int stride, int width, int height)
> 
> {
> 
>    unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
> 
>    unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
> 
>    g_odata[y*stride + x] = clamp_u8(tex2D(tex, x+0.5, y+0.5)*256.0);
> 
> }
> 
> ```
> 
> VERY NICE :). What is “stride” as a input in transformKernel&nbsp; ?
> 
> Y.
> 
> [snapback]250527[/snapback]

---

<div class="post-metadata">

**Author:** ![wumpus](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/wumpus/32/12040_2.png) [@wumpus](https://forums.developer.nvidia.com/u/wumpus)\
**Post date:** [September 13, 2007, 3:42pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/4 "2007-09-13T15:42:14Z")

</div>

stride is the memory difference in bytes between two successive rows , this can be different than the width in some cases for efficiency (rounded up)

---

<div class="post-metadata">

**Author:** ![Simon\_Green](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Simon\_Green](https://forums.developer.nvidia.com/u/Simon_Green)\
**Post date:** [September 13, 2007, 5:04pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/5 "2007-09-13T17:04:54Z")

</div>

If you use cudaReadModeElementType, the results from the texture fetch will be of the same type as the texture.

---

<div class="post-metadata">

**Author:** ![wumpus](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/wumpus/32/12040_2.png) [@wumpus](https://forums.developer.nvidia.com/u/wumpus)\
**Post date:** [September 14, 2007, 3:18pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/6 "2007-09-14T15:18:12Z")

</div>

> [@](#):
>
> If you use cudaReadModeElementType, the results from the texture fetch will be of the same type as the texture.
> 
> [snapback]250579[/snapback]

But sometimes you need linear interpolation _and_ the result to be in the same range as the original. And as far as I’ve seen that only works when normalizing.

---

<div class="post-metadata">

**Author:** ![jimh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jimh](https://forums.developer.nvidia.com/u/jimh)\
**Post date:** [September 14, 2007, 3:58pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/7 "2007-09-14T15:58:08Z")

</div>

> [@](#):
>
> But sometimes you need linear interpolation _and_ the result to be in the same range as the original. And as far as I’ve seen that only works when normalizing.
> 
> [snapback]251020[/snapback]

You don’t have to use normalized results to get that. Filtering works just fine on unnormalized float types.

---

<div class="post-metadata">

**Author:** ![wumpus](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/wumpus/32/12040_2.png) [@wumpus](https://forums.developer.nvidia.com/u/wumpus)\
**Post date:** [September 14, 2007, 8:16pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/8 "2007-09-14T20:16:30Z")

</div>

> [@](#):
>
> You don’t have to use normalized results to get that. Filtering works just fine on unnormalized float types.
> 
> [snapback]251030[/snapback]

Well, my experience is that you cannot use linear filtering if you use this:

```auto
typedef texture<float, 2, cudaReadModeElementType> RefTex;

```

But you can if you use

```auto
typedef texture<float, 2, cudaReadModeNormalizedFloat> RefTex;

```

How do you use it on unnormalized types? Mind you, my textures internal format is uint8 not floats:

```auto
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindUnsigned);

```

I think my only option is to use normalized floats and “unnormalize” them if I want then in 0.0 … 255.0

---

<div class="post-metadata">

**Author:** ![jimh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jimh](https://forums.developer.nvidia.com/u/jimh)\
**Post date:** [September 17, 2007, 4:21pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/9 "2007-09-17T16:21:44Z")

</div>

> [@](#):
>
> Well, my experience is that you cannot use linear filtering if you use this:
> 
> ```auto
> typedef texture<float, 2, cudaReadModeElementType> RefTex;
> 
> ```
> 
> But you can if you use
> 
> ```auto
> typedef texture<float, 2, cudaReadModeNormalizedFloat> RefTex;
> 
> ```
> 
> How do you use it on unnormalized types? Mind you, my textures internal format is uint8 not floats

Use float as the internal texture type. I’m using it in my code without a problem.

> [@](#):
>
> ```auto
> cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindUnsigned);
> 
> ```

Are you using this channelDesc with the texture reference declarations above? (The channelDesc indicates an 8-bit unsigned type and the texture reference indicates a 32-bit signed float.) I don’t know the internal details, but I would think this would cause problems in texture fetching.

---

<div class="post-metadata">

**Author:** ![wumpus](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/wumpus/32/12040_2.png) [@wumpus](https://forums.developer.nvidia.com/u/wumpus)\
**Post date:** [September 17, 2007, 6:11pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/10 "2007-09-17T18:11:15Z")

</div>

That is intended behaviour, actually it’s exactly like what happens when you fetch from a texture in OpenGL. I store the texture as uint8 in memory to save out memory and bandwidth. When you fetch a texel, it is normalized from 0…255 to 0.0…1.0.

---

<div class="post-metadata">

**Author:** ![jimh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jimh](https://forums.developer.nvidia.com/u/jimh)\
**Post date:** [September 17, 2007, 8:33pm UTC](https://forums.developer.nvidia.com/t/texture-memory-unnormalizing/1477/11 "2007-09-17T20:33:15Z")

</div>

I didn’t know you could do that. Thanks for the info.

Since there isn’t a “cudaReadModeFloat”, I suspect you’re stuck using normalized floats if float isn’t the underlying type.
