Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


transfer byte[] to effect


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
28 replies to this topic

#1 begemot   Members   -  Reputation: 103

Like
0Likes
Like

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!!!

Sponsor:

#2 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

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.

Niko Suni


#3 begemot   Members   -  Reputation: 103

Like
0Likes
Like

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?

#4 begemot   Members   -  Reputation: 103

Like
0Likes
Like

Posted 26 April 2012 - 05:09 AM

May be creating parameters are not optimized? or code to fill?

#5 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

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.

Niko Suni


#6 begemot   Members   -  Reputation: 103

Like
0Likes
Like

Posted 26 April 2012 - 06:46 AM

It seems to me, that I can not use Marshal.Copy so as pitch of source image not equals texture's pitch at times..
I'm dreaming to remove this copying process..


Yes. I need get and display frames from video file in realtime mode)))

#7 begemot   Members   -  Reputation: 103

Like
0Likes
Like

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

#8 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

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.

Niko Suni


#9 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

Posted 26 April 2012 - 01:59 PM

Also pay attention to the lock flags. If you don't need to read back the texture, be sure to request write only access and discard behavior.

Niko Suni


#10 Adam_42   Crossbones+   -  Reputation: 2619

Like
0Likes
Like

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.

#11 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

Posted 26 April 2012 - 03:11 PM

It is also possible that the hardware supports YUV texture formats natively. You will want to check this at runtime, though.

Niko Suni


#12 begemot   Members   -  Reputation: 103

Like
0Likes
Like

Posted 26 April 2012 - 11:40 PM

Which of ways of creating texture you suppose is the fastest?

Texture.FromMemory
Texture.FromStream
new Texture() and copy data

#13 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

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.

Niko Suni


#14 Adam_42   Crossbones+   -  Reputation: 2619

Like
0Likes
Like

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 begemot   Members   -  Reputation: 103

Like
0Likes
Like

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?

#16 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

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.

Niko Suni


#17 begemot   Members   -  Reputation: 103

Like
0Likes
Like

Posted 27 April 2012 - 04:44 AM

But I don't understand how I pass my surface to shader..
I can create texture, Get surface as
Texture.GetSurfaceLevel(0),
update surface using line per line CopyMemory and pass to shader texture. Is it right?

#18 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

Posted 27 April 2012 - 04:46 AM

You pass textures to the shader.

You can create a texture with only one surface, by setting the mipLevels field of the texture descriptor to 1.

Niko Suni


#19 begemot   Members   -  Reputation: 103

Like
0Likes
Like

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?

#20 Nik02   Crossbones+   -  Reputation: 2927

Like
0Likes
Like

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?

Niko Suni





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS