# DX11 DX11-Reuse or Create a new Buffer?

## Recommended Posts

Hi, so I get at a point where I can render a cube, yay. Now my program loads an .OBJ file to a vposnormaltex layout struct array. I want know how I render new stuff every time I want. Should I reuse the buffer? Or should I Create a new one(using the same ID3D11Buffer)? I notice theres this HRESULT SetPrivateData( REFGUID guid, UINT DataSize, const void *pData ); that the ID3D11Buffer inherits, but Im not sure what I should do..

##### Share on other sites
Quote:
 Original post by Icebone1000Hi, so I get at a point where I can render a cube, yay. Now my program loads an .OBJ file to a vposnormaltex layout struct array.I want know how I render new stuff every time I want. Should I reuse the buffer? Or should I Create a new one(using the same ID3D11Buffer)?

A buffer can contain any number of objects in any particular formats you want. Just as long as you have allocated enough space for it. The important thing is that you don't create buffers that are too large (a few meg is usually good), and that you properly specify their usage so that they can be placed in the best locations for them (static geometry should not be specified with dynamic/cpu read and write, for instance).
Quote:
 I notice theres thisHRESULT SetPrivateData( REFGUID guid, UINT DataSize, const void *pData);that the ID3D11Buffer inherits, but Im not sure what I should do..

That does not do what you think it does.

##### Share on other sites
The thing is that the data I want render are set at running time, my app have a open .obj menu item..Everytime a .obj file is opened I want to display the object(Im not considering yet display all .obj opened, just one by one now to make easier).

What my app do at start is set a cube, create a vertexbuffer(subresource = cube), bind it to the input assembly, and render.

What should I do when the .obj is readed?:
-Create a new dx buffer, point the vposnormaltex array, bind it to the IA?

-Re-set the dx buffer, that is alredy binded to the IA(dont know if this is how things works, I remember something like this on DX9, using 'lock' to bring GPU stuff to mem, re-set and sending back)?

##### Share on other sites
Keep the array of geometry, a pointer to the vertrex buffer, the topology type and the layout together in a class or structure.

For each of these, on every frame, you just set the input type, set the vertex buffer and render everything.

Ideally, you should keep state changes to a minimum. I suppose if your input layout is always the same, you can set it once but realistically this never really happens once you get past the initial stages.

I would suggest trying whatever works for you and when you know more, take a look at the performance considerations documentation on MSDN.

##### Share on other sites
Thanks for the info.

But I still dont know if theres a way to re-set an alredy created buffer or if I can recreate it every time I want change the data on it.

My program specificaly have 4 types of input layout( pos, posnorm, postex, postexnorm ), witch also means I have to recompile also another effect file.. heres the approach Im taking everytime a file is opened:

OPENFILENAME openFile = {0};...if( GetOpenFileName( &openFile ) ){std::ifstream File;File.open( openFile.lpstrFile,std::ios::in|std::ios::_Nocreate );if( File.is_open() ){							myOBJ.OBJReadDataFromFileStream( File );							myOBJ.OBJTransformDataToDXLayout();switch( myOBJ.OBJGetOBJType() ){case 0x00:if( DXInitEffect( TEXT("BasicTr.fx"), g_pEffect, g_pETech, g_pEPass, g_pDevice ) != S_OK ){MB( " dxerror", "effect load fail", MB_E );									}break;case 0xff:if( DXInitEffect( TEXT("BasicTransformLightTex.fx"), g_pEffect, g_pETech, g_pEPass, g_pDevice ) != S_OK ){MB( " dxerror", "effect load fail", MB_E );}break;case 0xf0:if( DXInitEffect( TEXT("BasicTransformLight.fx"), g_pEffect, g_pETech, g_pEPass, g_pDevice ) != S_OK ){MB( " dxerror", "effect load fail", MB_E );}break;case 0x0f:if( DXInitEffect( TEXT("BasicTransformTex.fx"), g_pEffect, g_pETech, g_pEPass, g_pDevice ) != S_OK ){MB( " dxerror", "effect load fail", MB_E );}break;}if( DXSetBufferResources(	g_pDevice, g_pVB, g_pIB, myOBJ.OBJGetDXVBData(), myOBJ.OBJGetDXVBDataSize() ) != S_OK ){	MB( " dxerror", "set buff", MB_E );}if( DXInitInputAssemble(	g_pDevice,g_pDIContext,myOBJ.OBJGetDXInputLayout(), myOBJ.OBJGetDXLayoutNumElms(), g_pEPass,g_pInputLayout, &g_pVB, 1, myOBJ.OBJGetDXStride(),	&g_pIB, 1 ) != S_OK ){MB( " dxerror", "init ia", MB_E );							}DXSetSCBVariablesInitialValues();//reset matrices and set textureiVcount = myOBJ.OBJGetDXVCount();bStuffToRender = TRUE;File.close();}//is open

Its working fine...but I really dont know how I would do on a game( I dont know many things in fact...)
Is normal an app call a million times pass->apply() and dc->Draw()?

I cant imagine how would be manage the transformation matrices for a bunch of different objects... And how would be sprite animation? On OpenGL what I was used to do is change the texcoord everytime, pretty automatic..cant see any similar way of doing this on DX.

##### Share on other sites
Use the map function on your device context to edit the contents of a buffer.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628282
• Total Posts
2981805
• ### Similar Content

• I was wondering if anyone could explain the depth buffer and the depth stencil state comparison function to me as I'm a little confused
So I have set up a depth stencil state where the DepthFunc is set to D3D11_COMPARISON_LESS, but what am I actually comparing here? What is actually written to the buffer, the pixel that should show up in the front?
I have these 2 quad faces, a Red Face and a Blue Face. The Blue Face is further away from the Viewer with a Z index value of -100.0f. Where the Red Face is close to the Viewer with a Z index value of 0.0f.
When DepthFunc is set to D3D11_COMPARISON_LESS the Red Face shows up in front of the Blue Face like it should based on the Z index values. BUT if I change the DepthFunc to D3D11_COMPARISON_LESS_EQUAL the Blue Face shows in front of the Red Face. Which does not make sense to me, I would think that when the function is set to D3D11_COMPARISON_LESS_EQUAL the Red Face would still show up in front of the Blue Face as the Z index for the Red Face is still closer to the viewer
Am I thinking of this comparison function all wrong?
Vertex data just in case
//Vertex date that make up the 2 faces Vertex verts[] = { //Red face Vertex(Vector4(0.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(0.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(0.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), //Blue face Vertex(Vector4(0.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(0.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(0.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), };
• By mellinoe
Hi all,
First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.

• If I do a buffer update with MAP_NO_OVERWRITE or MAP_DISCARD, can I just write to the buffer after I called Unmap() on the buffer? It seems to work fine for me (Nvidia driver), but is it actually legal to do so? I have a graphics device wrapper and I don't want to expose Map/Unmap, but just have a function like void* AllocateFromRingBuffer(GPUBuffer* buffer, uint size, uint& offset); This function would just call Map on the buffer, then Unmap immediately and then return the address of the buffer. It usually does a MAP_NO_OVERWRITE, but sometimes it is a WRITE_DISCARD (when the buffer wraps around). Previously I have been using it so that the function expected the data upfront and would copy to the buffer between Map/Unmap, but now I want to extend functionality of it so that it would just return an address to write to.

• Trying to write a multitexturing shader in DirectX11 - 3 textures work fine, but adding 4th gets sampled as black!
Could you please look at the textureClass.cpp line 79? - I'm guess its D3D11_TEXTURE2D_DESC settings are wrong,
but no idea how to set it up right. I tried changing ArraySize from 1 to 4, but does nothing. If thats not the issue, please look
at the LightShader_ps - maybe doing something wrong there? Otherwise, no idea.
// Setup the description of the texture.
textureDesc.Height = height;
textureDesc.Width = width;
textureDesc.MipLevels = 0;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
https://github.com/mister51213/DirectX11Engine/blob/master/DirectX11Engine/Texture.cpp

• I have to learn DirectX for a course I am studying. This book https://www.amazon.co.uk/Introduction-3D-Game-Programming-Directx/dp/1936420228 I felt would be great for me to learn from.
The trouble is the examples which are all offered here http://www.d3dcoder.net/d3d11.htm . They do not work for me. This is a known issue as there is a link on the examples page saying how to fix it. I'm having difficulty with doing this though. This is the page with the solution http://www.d3dcoder.net/Data/Book4/d3d11Win10.htm.
The reason why this problem is happening, the book was released before Windows 10 was released. Now when the examples are run they need slight fixes in order for them to even work. I just can't get these examples working at all.
Would anyone be able to help me get the examples working please. I am running Windows 10 also just to make this clear, so this is why the examples are experiencing the not so desired behaviour. I just wish they would work straight away but there seems to be issues with the examples from this book mainly because of it trying to run from a Windows 10 OS.
On top of this, if anyone has any suggestions with how I can learn DirectX 11 i would be most grateful. Thanks very much. I really would like to get them examples working to though from the book I mentioned.

GameDevCoder.

PS - If anyone has noticed. I asked this about 1 year ago also but this was when I was dabbling in it. Now I am actually needing to produce some stuff with DirectX so I have to get my head round this now. I felt at the time that I sort of understood what was being written to me in response to my thread back then. I had always been a little unsure though of being absolutely sure of what was happening with these troublesome examples. So I am really just trying to get to the bottom of this now. If anyone can help me work these examples out so I can see them working then hopefully I can learn DirectX 11 from them.

*SOLUTION* - I was able to get the examples running thanks to the gamedev.net community. Great work guys. I'm so please now that I can learn from this book now I have the examples running.
https://www.gamedev.net/forums/topic/693437-i-need-to-learn-directx-the-examples-for-introduction-to-3d-programming-with-directx-11-by-frank-d-luna-does-not-work-can-anyone-help-me/?do=findComment&comment=5363013

• 9
• 10
• 11
• 17
• 14