Skip to main content
GameDev.net gamedev.net
🔒 Locked

transfer byte[] to effect

Started by begemot Apr 26, 2012 at 8:51 AM 27 replies 5.2k views
Original Post
begemot
begemot
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!!!
Nik02
Nik02
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
begemot
begemot
I need to use D3D9..

Now I create texture as

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"] [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Texture[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"](device, w[/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"], h[/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"], 1, [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Usage[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].Dynamic, [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Format[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].A8R8G8B8,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pool[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].Default);[/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"]and then fill it [/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]DataRectangle[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] a_ = t.LockRectangle(0, [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]LockFlags[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].None);[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] pitch = (([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"])a_.Pitch / [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]sizeof[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]ushort[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]));[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]DataStream[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] a = a_.Data;[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]unsafe[/font][/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]ushort[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]* to = ([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]ushort[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]*)a.DataPointer;[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]ushort[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]* from = ([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]ushort[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]*)ptr.ToPointer();[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] j = 0; j < heightdef; j++)[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] i = 0; i < pit; i++)[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][font="Consolas"][size="2"]to[i + j * pitch] = from[i + j * pit];[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"][font="Consolas"][size="2"][font="Consolas"][size="2"]t.UnlockRectangle(0);[/font][/font][/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]then I can set it to my effect to texture parameter: effect.SetTexture("Tex",t). [/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"] Is exist any faster way to update texture in effect?[/font][/font]
begemot
begemot
May be creating parameters are not optimized? or code to fill?
Nik02
Nik02
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
begemot
begemot
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)))
begemot
begemot
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

[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Texture[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].FromMemory(device, imageBytes,widthdef,heightdef,1,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Usage[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].None,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Format[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].A8R8G8B8,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Pool[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].Default,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Filter[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].None,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Filter[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].None,[/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"].White.ToArgb())[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][color="#000000"]In what I'm mistake?[/font][/font]

3. Create one - dimension texture and pass it to shader/effect
Nik02
Nik02
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
Nik02
Nik02
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
Adam_42
Adam_42
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.
Nik02
Nik02
It is also possible that the hardware supports YUV texture formats natively. You will want to check this at runtime, though.
Niko Suni
begemot
begemot
Which of ways of creating texture you suppose is the fastest?

Texture.FromMemory
Texture.FromStream
new Texture() and copy data
Nik02
Nik02
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
Adam_42
Adam_42
You might also want to look at Suface.FromMemory() which will do all the copying for you from a byte[] array.
begemot
begemot
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?
Nik02
Nik02
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
begemot
begemot
But I don't understand how I pass my surface to shader..
I can create texture, Get surface as
[font="Consolas"][size="2"][font="Consolas"][size="2"]Texture.GetSurfaceLevel(0)[/font][/font],
[font="Consolas"][size="2"][font="Consolas"][size="2"]update surface using line per line CopyMemory and pass to shader texture. Is it right?[/font][/font]
Nik02
Nik02
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
begemot
begemot
I have texture and can do next operation

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]using[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Surface[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] surface = t.GetSurfaceLevel(0))[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]DataRectangle[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] dr = surface.LockRectangle([/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]LockFlags[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Discard);[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] d_pit = dr.Pitch;[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] j = 0; j < heightdef; j++)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]CopyMemory(dr.Data.DataPointer.ToInt32() + j*d_pit,[/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]ptr.ToInt32() + [/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]j*pit,[/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]d_pit);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]surface.UnlockRectangle();[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]then I pass it to shader.[/font][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]Is it the fastest way? [/font][/font]
Nik02
Nik02
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.