[D3D9] buffer to texture

Results 1 to 15 of 15
  1. #1
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    shout [D3D9] buffer to texture

    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?


  2. #2
    Valued Member Keristrasza is offline
    MemberRank
    Jun 2015 Join Date
    128Posts

    Re: [D3D9] buffer to texture

    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

  3. #3
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    Re: [D3D9] buffer to texture

    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 by iFinn; 18-05-17 at 12:44 AM. Reason: rename src to buffer

  4. #4
    Valued Member Keristrasza is offline
    MemberRank
    Jun 2015 Join Date
    128Posts

    Re: [D3D9] buffer to texture

    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?

  5. #5
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    Re: [D3D9] buffer to texture

    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
    https://github.com/Asunaya/RefinedGu...ae2367f07e6L84


    Would there be some other way to render the received buffer without using IDirect3DTexture9?
    Last edited by iFinn; 18-05-17 at 04:58 AM.

  6. #6
    Valued Member grandao is offline
    MemberRank
    Feb 2008 Join Date
    RJ - BrasilLocation
    128Posts

    Re: [D3D9] buffer to texture

    I think you should better explain what you're trying to achieve, the fix Keristrasza pointed should have fixed copy to texture problem.

  7. #7
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    Re: [D3D9] buffer to texture

    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:

    https://pastebin.com/TpSSKzs8

    CEF Api DOC
    Last edited by iFinn; 18-05-17 at 05:54 PM.

  8. #8
    Valued Member Keristrasza is offline
    MemberRank
    Jun 2015 Join Date
    128Posts

    Re: [D3D9] buffer to texture

    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 this 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 this, 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.)

  9. #9
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    Re: [D3D9] buffer to texture

    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 OpenGL with DX9?
    Last edited by iFinn; 20-05-17 at 03:17 AM.

  10. #10
    Valued Member grandao is offline
    MemberRank
    Feb 2008 Join Date
    RJ - BrasilLocation
    128Posts

    Re: [D3D9] buffer to texture

    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.

  11. #11
    Valued Member Keristrasza is offline
    MemberRank
    Jun 2015 Join Date
    128Posts

    Re: [D3D9] buffer to texture

    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

  12. #12
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    Re: [D3D9] buffer to texture

    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:
    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?

  13. #13
    Valued Member grandao is offline
    MemberRank
    Feb 2008 Join Date
    RJ - BrasilLocation
    128Posts

    Re: [D3D9] buffer to texture

    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)

  14. #14
    Valued Member Keristrasza is offline
    MemberRank
    Jun 2015 Join Date
    128Posts

    Re: [D3D9] buffer to texture

    Quote Originally Posted by iFinn
    even buffer in BGRA using ARGB in texture
    D3D9's ARGB formats *are* BGRA, D3D9 stores the channels in opposite order. See remarks here: https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx.

    Quote Originally Posted by iFinn
    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?

    Quote Originally Posted by grandao
    (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

  15. #15
    Member iFinn is offline
    MemberRank
    Dec 2013 Join Date
    62Posts

    Re: [D3D9] buffer to texture

    Quote Originally Posted by Keristrasza View Post
    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 by iFinn; 21-05-17 at 08:10 PM.



Advertisement