28 replies to this topic
#1 Members - Reputation: 103
Posted 26 April 2012 - 02:51 AM
I have byte array of image's data (or pointer).
I have effect with pixel shader to fill result texture. I want to work with my image data in pixel shader without unnecessary transformations.
How I can transfer bytes array to my Effect and how I must define it in HLSL program?
I'm using SlimDX and C#.
Help me,please!!!
I have effect with pixel shader to fill result texture. I want to work with my image data in pixel shader without unnecessary transformations.
How I can transfer bytes array to my Effect and how I must define it in HLSL program?
I'm using SlimDX and C#.
Help me,please!!!
Sponsor:
#2 Members - Reputation: 1991
Posted 26 April 2012 - 04:49 AM
Usually this kind of thing is done via textures.
A "texture" is essentially (and in somewhat simplified terms) "a handle to video memory area that contains texels for the purpose of sampling filtered image-type data by the gpu".
Typically, hardware supports several texture formats so "unnecessary transforms" are seldom needed. If you are targeting hardware with D3D10 and up, you can also load data from custom structured buffers in the pixel shader (but you have to do your own filtering).
Actual texture objects are usually faster than custom buffers since hardware tends to have dedicated circuits for the specific purpose of taking filtered samples from known common format textures.
A "texture" is essentially (and in somewhat simplified terms) "a handle to video memory area that contains texels for the purpose of sampling filtered image-type data by the gpu".
Typically, hardware supports several texture formats so "unnecessary transforms" are seldom needed. If you are targeting hardware with D3D10 and up, you can also load data from custom structured buffers in the pixel shader (but you have to do your own filtering).
Actual texture objects are usually faster than custom buffers since hardware tends to have dedicated circuits for the specific purpose of taking filtered samples from known common format textures.
Niko Suni
Software developer
Software developer
#3 Members - Reputation: 103
Posted 26 April 2012 - 05:07 AM
I need to use D3D9..
Now I create texture as
new Texture(device, w, h, 1, Usage.Dynamic, Format.A8R8G8B8,Pool.Default);
and then fill it
DataRectangle a_ = t.LockRectangle(0, LockFlags.None);
int pitch = ((int)a_.Pitch / sizeof(ushort));
DataStream a = a_.Data;
unsafe
{
ushort* to = (ushort*)a.DataPointer;
ushort* from = (ushort*)ptr.ToPointer();
for (int j = 0; j < heightdef; j++)
for (int i = 0; i < pit; i++)
to[i + j * pitch] = from[i + j * pit];
}
t.UnlockRectangle(0);
then I can set it to my effect to texture parameter: effect.SetTexture("Tex",t).
Is exist any faster way to update texture in effect?
Now I create texture as
new Texture(device, w, h, 1, Usage.Dynamic, Format.A8R8G8B8,Pool.Default);
and then fill it
DataRectangle a_ = t.LockRectangle(0, LockFlags.None);
int pitch = ((int)a_.Pitch / sizeof(ushort));
DataStream a = a_.Data;
unsafe
{
ushort* to = (ushort*)a.DataPointer;
ushort* from = (ushort*)ptr.ToPointer();
for (int j = 0; j < heightdef; j++)
for (int i = 0; i < pit; i++)
to[i + j * pitch] = from[i + j * pit];
}
t.UnlockRectangle(0);
then I can set it to my effect to texture parameter: effect.SetTexture("Tex",t).
Is exist any faster way to update texture in effect?
#5 Members - Reputation: 1991
Posted 26 April 2012 - 06:08 AM
It is faster to perform block copies of the data (CopyMemory or Marshal.Copy) than copying pixel per pixel.
You may also want to use double buffering; keep a pair of textures and update the other when you use the another in rendering the current frame. This helps to prevent stalling (condition where either CPU or GPU waits for access because other side has a resource locked).
Setting effect variables is not particularly slow; internally, when you apply an effect pass, the framework sets the texture for you using IDirect3DDevice9::SetTexture.
If you don't have to lock the texture each frame, then don't lock it each frame
I assume that you have a video source or something like that here, though.
You may also want to use double buffering; keep a pair of textures and update the other when you use the another in rendering the current frame. This helps to prevent stalling (condition where either CPU or GPU waits for access because other side has a resource locked).
Setting effect variables is not particularly slow; internally, when you apply an effect pass, the framework sets the texture for you using IDirect3DDevice9::SetTexture.
If you don't have to lock the texture each frame, then don't lock it each frame
Niko Suni
Software developer
Software developer
#7 Members - Reputation: 103
Posted 26 April 2012 - 07:03 AM
May be, those copying code can be exclude in the following ways:
1. My idea to put bytes to shader/effect
2. Create texture from memory. In this case I have error in code
Texture.FromMemory(device, imageBytes,widthdef,heightdef,1,Usage.None,Format.A8R8G8B8,Pool.Default,Filter.None,Filter.None,Color.White.ToArgb())
In what I'm mistake?
3. Create one - dimension texture and pass it to shader/effect
1. My idea to put bytes to shader/effect
2. Create texture from memory. In this case I have error in code
Texture.FromMemory(device, imageBytes,widthdef,heightdef,1,Usage.None,Format.A8R8G8B8,Pool.Default,Filter.None,Filter.None,Color.White.ToArgb())
In what I'm mistake?
3. Create one - dimension texture and pass it to shader/effect
#8 Members - Reputation: 1991
Posted 26 April 2012 - 01:56 PM
You can use Marshal.Copy to copy one line at a time. This is still far more efficient than doing byte-per-byte copies.
I believe that Texture.FromMemory expects to find image file contents from the source array, not just the image pixel data.
I believe that Texture.FromMemory expects to find image file contents from the source array, not just the image pixel data.
Niko Suni
Software developer
Software developer
#10 Members - Reputation: 1411
Posted 26 April 2012 - 02:34 PM
One trick that can often be done with video data is to get the pixel shader to do the YUV->RGB conversion. You'll probably want to use three 8-bit textures for this (e.g. D3DFMT_A8). That method has several benefits:
- The GPU can do the conversion much quicker than the CPU.
- You need to upload less data (because U and V are a quarter of the size of Y).
- Saves a fair bit of memory too.
Ideally you would get your video codec to output directly to your texture too, but that's not always so easy.
- The GPU can do the conversion much quicker than the CPU.
- You need to upload less data (because U and V are a quarter of the size of Y).
- Saves a fair bit of memory too.
Ideally you would get your video codec to output directly to your texture too, but that's not always so easy.
#13 Members - Reputation: 1991
Posted 26 April 2012 - 11:55 PM
It varies based on system architecture, and the source of the stream in FromStream (and a number of other things). A stream could be a disk file or a network socket, which are generally way slower than a local memory buffer.
FromMemory and FromStream differ from your third method in that they read and decode whole image file contents whereas in manual copying, you generally only handle the actual pixel data.
You are not supposed to create new textures each frame anyway; if you use a texture in more than one frame (which is very, very common), you allocate it outside the render loop when you initialize your objects and update it as needed.
FromMemory and FromStream differ from your third method in that they read and decode whole image file contents whereas in manual copying, you generally only handle the actual pixel data.
You are not supposed to create new textures each frame anyway; if you use a texture in more than one frame (which is very, very common), you allocate it outside the render loop when you initialize your objects and update it as needed.
Niko Suni
Software developer
Software developer
#14 Members - Reputation: 1411
Posted 27 April 2012 - 03:49 AM
You might also want to look at Suface.FromMemory() which will do all the copying for you from a byte[] array.
#15 Members - Reputation: 103
Posted 27 April 2012 - 04:05 AM
I tested creating texture from memory using Texture.FromMemory () (Nik02 was right in data format - it must contain BITMAPINFOHEADER),but it is very slow..
Now I do CopyMemory -this way a little bit faster when unsafe copying.
Working with video memory throw surfaces is faster then throw textures?
Now I do CopyMemory -this way a little bit faster when unsafe copying.
Working with video memory throw surfaces is faster then throw textures?
#16 Members - Reputation: 1991
Posted 27 April 2012 - 04:16 AM
Texture.FromMemory allocates a new texture each time. Allocations are always much slower than updates.
A surface is a block of video memory. A texture is a collection of 1...n surfaces. If you are updating one level of a texture, you are effectively updating a surface.
A surface is a block of video memory. A texture is a collection of 1...n surfaces. If you are updating one level of a texture, you are effectively updating a surface.
Niko Suni
Software developer
Software developer
#19 Members - Reputation: 103
Posted 27 April 2012 - 05:18 AM
I have texture and can do next operation
using (Surface surface = t.GetSurfaceLevel(0))
{
DataRectangle dr = surface.LockRectangle(LockFlags.Discard);
int d_pit = dr.Pitch;
for (int j = 0; j < heightdef; j++)
CopyMemory(dr.Data.DataPointer.ToInt32() + j*d_pit,ptr.ToInt32() + j*pit,d_pit);
surface.UnlockRectangle();
}
then I pass it to shader.
Is it the fastest way?
using (Surface surface = t.GetSurfaceLevel(0))
{
DataRectangle dr = surface.LockRectangle(LockFlags.Discard);
int d_pit = dr.Pitch;
for (int j = 0; j < heightdef; j++)
CopyMemory(dr.Data.DataPointer.ToInt32() + j*d_pit,ptr.ToInt32() + j*pit,d_pit);
surface.UnlockRectangle();
}
then I pass it to shader.
Is it the fastest way?
#20 Members - Reputation: 1991
Posted 27 April 2012 - 05:26 AM
In general, line by line copy is the fastest robust method with which you can copy stuff to surfaces. To optimize, you could check whether the destination pitch equals width*number of bytes per pixel, and if it happens to be the same, copy all the data in one call. If not, just copy line per line like you do now.
The texture usage flags and lock flags are very important for performance. Discard is often the desired behavior, when you want to overwrite the entire destination data.
I previously mentioned double buffering; have you tried it?
And finally, have you profiled your application? Do you have actual knowledge of where your performance bottleneck is?
The texture usage flags and lock flags are very important for performance. Discard is often the desired behavior, when you want to overwrite the entire destination data.
I previously mentioned double buffering; have you tried it?
And finally, have you profiled your application? Do you have actual knowledge of where your performance bottleneck is?
Niko Suni
Software developer
Software developer






