How do i use PureDevice?

Started by
86 comments, last by ZeroWalker 10 years, 8 months ago


I have the data in one thread, and it´s in a byte array , which i guess is the buffer(in ram). Though that´s in jpeg currently (if i use jpeg).
So i will have to decode it into Texture. And to do that, the easiest way would be to just use, Texture.FromMemory(....).

now you're gettin' with it! <g>.

i've played with dynamic buffers but not textures. i personally haven't needed dynamic textures until that colored world map thing i came up with the other day for the new version of Global War. i use bmp format, as its microsoft's default, and they wrote directx. so bmp is sort of the defacto default uncompressed public format for directx. DDS is directx's internal native format. i use create texture from file.

if you need to use create texture from memory to do the jpg -> dds conversion for you, then you'll want to release and recreate for each new image transmitted. actually a little simpler than than lock and unlock.

again, orders of magnitude faster than your transmission rate, so no performance issues there.

and since you create once, then draw, that is - by definition - a STATIC texture.

so, think you'll have it running by snack time this afternoon? (its 10am here in Washington DC now). <g>

I think you have to much hope in me there;P

The problem is, i don´t know how to Release/Recreate.
Or well, maybe, if that means, Dispose, Initialize.

But the problem is, that´s what i am doing currently, I have to Dispose and Remake a Texture all the time, and i am thinking,
That isn´t a good idea, to remake it.

I simply want to Fill the texture with new data, as that should be faster?

EDIT: Missed your post:

3. lock the texture



4. memcpy your buffer to the texture



5. unlock the texture


Those are what i don´t get how to do.
memcpy, perhaps. But lock and unlock, i just don´t get how to do it:S

EDIT 2:

I am guessing, something like this makes a Texture with Null data.

using (tex = new Texture(device, -2, -2, 1, SharpDX.Direct3D9.Usage.None, SharpDX.Direct3D9.Format.X8R8G8B8, SharpDX.Direct3D9.Pool.Default))

Though, i can´t use -2 (NoPowerof2) with that, i have to write something. And it doesn´t seem to allow many resolutions at all.

Meaning, i can add to it when i want?
Do, i don´t get how to add to it, other than to use
tex = SharpDX.Direct3D9.Texture.FromMemory(device, tempBytes, -2, -2, 1, SharpDX.Direct3D9.Usage.Dynamic, SharpDX.Direct3D9.Format.X8R8G8B8, SharpDX.Direct3D9.Pool.Default, SharpDX.Direct3D9.Filter.None, SharpDX.Direct3D9.Filter.None, 0)

Which i think, remakes it.

Advertisement

Also, i have a problem with my textures that i am displaying.
I don´t know why, but they are, "corrupted". Hard to explain, so i took a screenshot.

This is with Discard, if i use Flip/FlipEx it´s a bit smoother for some reason.

Texture_Not_Displaying_Correctly.png

As you can see, it alters the image, or Crops it or somthing.

It´s not easy to see if you have a big image. But you can easily see it on small stuff like Text.

And this is Jpeg, but BMP doesn´t change it. The image itself is not wrong. The alteration occurs either on Displaying the Texture, or Converting to a Texture.


The problem is, i don´t know how to Release/Recreate.

that will be a couple of calls in your graphics library you're using.

they in turn, being based on directx, will call: d3d_device_ptr->CreateTexture (or whatever its called) to create,

and

mytexture-> Release() to destroy.


I have to Dispose and Remake a Texture all the time

no need to allocate and deallocate your buffer each time you want to receive a new frame. once at program (or transmission) start and end is fine .

i personally am unfamiliar with the various create texture from memory functions in directx. you may be able to fill (update) an existing texture from memory (a jpg buffer), or you may have to create and release. the thing is your data is jpg, not directx texture format. so you need to translate somehow. apparently load and create texture support jpg translation. as i said i use bmp myself.

but if create from memory will only make new textures, not update existing ones, then you'll have to release and create with each new frame. or create once, then convert jpg -> texture yourself, then lock and copy. and release once at end.

me personally, i'd do the screen capture to a texture format (a direct draw surface) in ram, then transmit that, read it into an identical data structure on the receiving end, and then just draw it as the texture on a sprite. OTOH, jpg probably gets you really nice compression for transmitting....


But lock and unlock, i just don´t get how to do it:S

once again, those will be api calls in your graphics library.

here's a directx code snippet for lock and unlock on a buffer (not a texture) as an example. textures work the same way:

 

void copy_ground_mesh()
{
LPVOID p;
mesh[FIRSTGROUNDBUF].mesh2.vb->Lock(0,0,&p,D3DLOCK_READONLY);                            
memcpy(gvert2,p,sizeof(vertex0)*GVERT2SIZE);
mesh[FIRSTGROUNDBUF].mesh2.vb->Unlock();                                   
}
 


I am guessing, something like this makes a Texture with Null data.

using (tex = new Texture(device, -2, -2, 1, SharpDX.Direct3D9.Usage.None, SharpDX.Direct3D9.Format.X8R8G8B8, SharpDX.Direct3D9.Pool.Default))

we're both guessing there. time to start reading the docs and find out how sharp dx works. if you discover that you can mix and match sharpdx and real directx code, then i could look it up and probably have an answer in about 5-10 minutes using directx code. or surely someone else here uses sharp dx.


The alteration occurs either on Displaying the Texture, or Converting to a Texture.

well that's a good thing actually. that will be easier to fix than a jpg->texture translation error, once you learn how sharp dx works.

sounds like its time for some quality time with the sharp dx docs.

although it takes a long time (days or weeks), i always read all the relevant parts of the directx docs before i start coding any directx code.

sounds like you haven't done this yet with sharp dx.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

I have been going through the Documentation, but i can´t find anything that you suggest.
http://sharpdx.org/documentation/api/t-sharpdx-direct3d9-texture

Either i am blind, or it´s called something different.

I don´t really get if i am supposed to work with inPtr as you seemed to mentioned that.

The texture object has a LockRectangle method in SharpDX as well as in native D3D. This method takes as a parameter the mip level that you want to lock; the largest mip level (which you typically want to lock) is 0.

Use the overloads that have an "out DataStream" parameter to avoid using IntPtr.

If you would use IntPtr, you could copy data from your own array to the memory address pointed to by the pointer (that is what a pointer does) by using Marshal.Copy (from .net class library) or CopyMemory (import from Win32 API).

If you use the DataStream, you can write your data directly therein from your source array.

In D3D, resource locking has several caveats, especially regarding the pool and usage with which the resource was created. Be sure to read the official D3D docs from MSDN to understand the possible issues. In particular, you cannot lock default pool resources in D3D9 (unless they are marked dynamic), because they are often physically allocated in GPU memory only and D3D runtime simply doesn't have access to the data directly.

Niko Suni

Ah ,well i have tried using LockRectangle, but i don´t understand how to use DataStream, as to create a DataStream i need a Pointer.
http://sharpdx.org/documentation/api/t-sharpdx-datastream
The Blob buffer need´s a Pointer.

So i need to make a Data Stream with a pointer first, and i don´t get, what am i supposed to point to?

As for the Dynamic and such, that i have gotten to work in terms of what my GPU is capable of.

You get a datastream when you call LockRectangle; you don't have to initialize one yourself.

DataStream textureData = null;

tex.LockRectangle (0, LockFlags.None, out textureData); // lock the first mip level, no flags

// textureData is now initialized and ready to take in your data

tex.UnlockRectangle(0); // be sure to call this once you're done with the stream

Niko Suni

In case you ever do need to allocate a native blob of memory in .net, you can use Marshal.AllocHGlobal method to get a memory chunk (and an IntPtr so you can manipulate it).

Many interoperation scenarios in .net involve using IntPtr, so it is useful to become familiar with it.

Niko Suni

So, something like this:

texture.LockRectangle(0, LockFlags.None, out DStream);
DStream.Write(TextureData, 0, TextureData.Length);
texture.UnlockRectangle(0);

where TextureData is a byte[] with my jpeg image?

And probably, have hard time with the IntPtr, do i use them to get handle for processes.

Texture data would be a byte array that contains the raw texel data. It doesn't understand JPEG at all.

Before this, you need to decode the jpeg file to a pixel array. This is how you'd do it in .net. It is admittely unfortunate that D3D does not supply this as a convenience (with, for example, something like UpdateSurfaceFromMemory), since it supports similar operations with texture creation.

Once you get the BitmapSource from the decoder, you could use its CopyPixels method with the destination IntPtr (that you get from locking the texture surface) to neatly move your stuff to D3D texture.

The byte layout of the data is expected to match the actual format of the resource (which you specify during its creation). For example, R8G8B8A8 format expects one pixel to be 4 bytes, with red, green, blue and alpha adjacent to each other. Pixels are adjacent to each other in memory. Scanlines (horizontal lines pixels) may or may not be adjacent to each other in memory; the "pitch" you get from locking the rectangle is the distance between adjacent scanlines, and is always AT LEAST width * numBytesPerPixel (but don't rely on any specific pitch).

Niko Suni

This topic is closed to new replies.

Advertisement