Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[D3D9] buffer to texture

Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
I'm trying to pass the buffer to a texture so I can draw it, the only way I found it was this:
Code:
definitions(...)
IDirect3DTexture9* tx;

Init(...)

RGetDevice()->CreateTexture(RGetScreenWidth(), RGetScreenHeight(), 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, &tx, NULL);

void(.., int width, int height, void* buffer){
D3DLOCKED_RECT d3dLockedRect;
tx->LockRect(0, &d3dLockedRect, 0, 0);
memcpy(d3dLockedRect.pBits, buffer, width *height* 4);
tx->UnlockRect(0);}
By doing this, the game crashThe site documentation informs you that the size is width * height * 4
"On Windows |buffer| will be width*height*4 bytes in size and represents a BGRA image with an upper-left origin"
Is there any way to pass the buffer to the texture?
 
Junior Spellweaver
Joined
Jun 14, 2015
Messages
123
Reaction score
20
D3DFMT_A4R4G4B4 is 16 bits per pixel, so the memory would be width * height * 2 bytes wide, not width * height * 4

edit: also, you should really use the D3DLOCKED_RECT::pitch value when copying
 
Upvote 0
Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
What would Pitch be?I took a look at some forums, I came up with this code:
for (int j = 0; j < height; ++j){
auto dst = (unsigned char*)d3dLockedRect.pBits + j *d3dLockedRect.Pitch;
memcpy(dst, buffer, width * 2);
buffer+= width*2;
}
But it did not work, I tried others, could you show me how to use it so I can copy the buffer to pBits?
 
Last edited:
Upvote 0
Junior Spellweaver
Joined
Jun 14, 2015
Messages
123
Reaction score
20
Sorry, I guess I wasn't clear -- The memory in the TEXTURE is too small to hold the stuff from the "buffer" variable. The "* 4" part was fine, you just need to create the texture with D3DFMT_A8R8G8B8 instead.

The copying code looks correct to me, apart from the fact that you seem to using two different variables "buffer" and "src"; you'd want to copy from "src" if you're adding to that.

Not sure what you mean by "didn't work" though, does it still crash? Does it just not render correctly?
 
Upvote 0
Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
Sorry, I forgot to rename "src" when I pasted the code here, "buffer" would be right

Does it just not render correctly?

I think it is not, he started drawing a white square and the game crashes
I'm using this function to render



Would there be some other way to render the received buffer without using IDirect3DTexture9?
 
Last edited:
Upvote 0
Junior Spellweaver
Joined
Feb 4, 2008
Messages
122
Reaction score
148
I think you should better explain what you're trying to achieve, the fix Keristrasza pointed should have fixed copy to texture problem.
 
Upvote 0
Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
A long time ago, I saw in a post here talking about things that could change in gunz, one of them was to substitute MINT for CEF or Scaleform, I tried to find something, but I did not find anything that pleased me, a little while ago, in a github Which I follow very closely, "Refined GunZ" where @Keristrasza and @grandao work, a user commented on a issue suggesting to substitute MINT for CEF, I researched further and found that CEF allows to use HTML5 and CSS in applications, and has OSR support (offscreen Rendering), I found some teaching tutorials, however only for OpenGL and DX11 / DX12, I wanted to try for DX9, in addition, CEF allows you to use a separate executable to render without weighing your application, in this case, consuming memory ores for Gunz
Here is my attempt to copy the buffer to the texture:



 
Last edited:
Upvote 0
Junior Spellweaver
Joined
Jun 14, 2015
Messages
123
Reaction score
20
You seem to be mixing width and height up a few times in the copying code (at least, in the part that is uncommented).

Something like ought to work for copying.

If the rendering method doesn't work for some reason, you could also just try rendering it with StretchRect like , which should work regardless of w/e render states and stuff is set.

(Haven't tested any of the above code at all, so sorry if there are errors.)
 
Upvote 0
Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
I discovered the reason for crash, I arranged with:
Invoke([width, height, buffer]() {copy_buffer(width, height, buffer);});

I also searched several ways to copy the buffer, but they were not functional, some failed or turned whiteI tried the rendering method using StretchRect but nothing appears
Is it possible to integrate with DX9?
 
Last edited:
Upvote 0
Junior Spellweaver
Joined
Feb 4, 2008
Messages
122
Reaction score
148
To check if you buffer copy is right just do the copy and then save the texture to a file to chek if it is ok.
Use :

D3DXSaveTextureToFile("dump.png", D3DXIFF_PNG, my_d3d_texture, nullptr)

If it is ok then your rendering code is the problem.
 
Upvote 0
Junior Spellweaver
Joined
Jun 14, 2015
Messages
123
Reaction score
20
I discovered the reason for crash, I arranged with:Invoke([width, height, buffer]() {copy_buffer(width, height, buffer);});

yeah that makes sense if it's rendering on a different thread or process, didn't think of that. creating the device with D3DCREATE_MULTITHREADED should also make it at least not crash when copying in the callback.

grandao's suggestion is good for checking the texture after copying. you could also save the "buffer" memory before copying with RSaveAsBmp or RSaveAsJpeg or something like that to see what CEF is actually giving you.

Is it possible to integrate OpenGL with DX9?

not really :p I think it would be harder than what you're already trying to do, at least
 
Upvote 0
Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
Really is a great idea, I thought about it at a time using RSaveAsJpeg, but it was not right for the same reason of crash with copy_buffer, the way to copy the texture buffer is correct, see the result:
OrIgRTT - [D3D9] buffer to texture - RaGEZONE Forums
Using 1024x768 resolution, with no problem, even buffer in BGRA using ARGB in texture
But why can I render a texture with TexturedQuad, but not this one?
 

Attachments

You must be registered for see attachments list
Upvote 0
Junior Spellweaver
Joined
Feb 4, 2008
Messages
122
Reaction score
148
So the copy is working and there is no crash anymore?
(You should also release your texture when the device resets, otherwise it will crash soon or later)
 
Upvote 0
Junior Spellweaver
Joined
Jun 14, 2015
Messages
123
Reaction score
20
iFinn said:
even buffer in BGRA using ARGB in texture

D3D9's ARGB formats *are* BGRA, D3D9 stores the channels in opposite order. See remarks here: .

iFinn said:
But why can I render a texture with TexturedQuad, but not this one?

I dunno, presumably because some leftover render or texture state is messing with it, could be anything. What happens when it renders? Nothing appears, or just solid white, or what?

Could you try creating the texture with D3DPOOL_DEFAULT (I forgot that was a requirement for StretchRect) and using the StretchRect method?

grandao said:
(You should also release your texture when the device resets, otherwise it will crash soon or later)

D3DPOOL_MANAGED makes D3D9 release and reacquire it automatically
 
Upvote 0
Newbie Spellweaver
Joined
Dec 13, 2013
Messages
63
Reaction score
10
I dunno, presumably because some leftover render or texture state is messing with it, could be anything. What happens when it renders? Nothing appears, or just solid white, or what?

It varies a lot from each site accessed, but the return is always a solid color, eg:
Ragezone: a light blue tone
Youtube: white, however, a soft red tone appears during video playback

I can not use tx->lock with CreateTexture using 'D3DPOOL_DEFAULT', DXGetErrorDescriptionA returns me: Invalid call
I searched the internet and saw that 'D3DPOOL_DEFAULT' does not support this function

edit:
@Keristrasza and @grandao, thanks for helping, about rendering texture, I have a GUI that in my client and I have managed to rendering through it
 
Last edited:
Upvote 0
Back
Top