# DX11 D3D11DeviceContext::DrawIndexed Does'nt Draw anything

This topic is 1247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

my program is Directx Program that draws a container cube within it smaller cubes....these smaller cubes fall by time i hope you understand what i mean...

The program isn't complete yet ...it should draws the container only ....but it draws nothing ...only the background color is visible... i only included what i think is needed ...

this is the routines that initialize the program

bool Game::init(HINSTANCE hinst,HWND _hw){
Directx11 ::init(hinst , _hw);
}


Directx11::init()

bool Directx11::init(HINSTANCE hinst,HWND hw){
_hinst=hinst;_hwnd=hw;
RECT rc;
GetClientRect(_hwnd,&rc);
height= rc.bottom - rc.top;
width = rc.right - rc.left;
UINT flags=0;
#ifdef _DEBUG
flags |=D3D11_CREATE_DEVICE_DEBUG;
#endif
HR(D3D11CreateDevice(0,_driverType,0,flags,0,0,D3D11_SDK_VERSION,&d3dDevice,&_featureLevel,&d3dDeviceContext));
if (d3dDevice == 0 || d3dDeviceContext == 0)
return 0;
DXGI_SWAP_CHAIN_DESC sdesc;
ZeroMemory(&sdesc,sizeof(DXGI_SWAP_CHAIN_DESC));
sdesc.Windowed=true;
sdesc.BufferCount=1;
sdesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
sdesc.BufferDesc.Height=height;
sdesc.BufferDesc.Width=width;
sdesc.BufferDesc.Scaling=DXGI_MODE_SCALING_UNSPECIFIED;
sdesc.BufferDesc.ScanlineOrdering=DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sdesc.OutputWindow=_hwnd;
sdesc.BufferDesc.RefreshRate.Denominator=1;
sdesc.BufferDesc.RefreshRate.Numerator=60;
sdesc.Flags=0;
sdesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
if (m4xMsaaEnable)
{
sdesc.SampleDesc.Count=4;
sdesc.SampleDesc.Quality=m4xMsaaQuality-1;
}
else
{
sdesc.SampleDesc.Count=1;
sdesc.SampleDesc.Quality=0;
}
IDXGIDevice *Device=0;
HR(d3dDevice->QueryInterface(__uuidof(IDXGIDevice),reinterpret_cast <void**> (&Device)));
IDXGIFactory* fac=0;
fac->CreateSwapChain(d3dDevice,&sdesc,&swapchain);
ReleaseCOM(Device);
ReleaseCOM(fac);
ID3D11Texture2D *back = 0;
HR(swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),reinterpret_cast <void**> (&back)));
HR(d3dDevice->CreateRenderTargetView(back,0,&RenderTarget));
D3D11_TEXTURE2D_DESC Tdesc;
ZeroMemory(&Tdesc,sizeof(D3D11_TEXTURE2D_DESC));
Tdesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
Tdesc.ArraySize = 1;
Tdesc.Format= DXGI_FORMAT_D24_UNORM_S8_UINT;
Tdesc.Height= height;
Tdesc.Width = width;
Tdesc.Usage = D3D11_USAGE_DEFAULT;
Tdesc.MipLevels=1;
if (m4xMsaaEnable)
{
Tdesc.SampleDesc.Count=4;
Tdesc.SampleDesc.Quality=m4xMsaaQuality-1;
}
else
{
Tdesc.SampleDesc.Count=1;
Tdesc.SampleDesc.Quality=0;
}
HR(d3dDevice->CreateTexture2D(&Tdesc,0,&depthview));
HR(d3dDevice->CreateDepthStencilView(depthview,0,&depth));
d3dDeviceContext->OMSetRenderTargets(1,&RenderTarget,depth);
D3D11_VIEWPORT vp;
vp.TopLeftX=0.0f;
vp.TopLeftY=0.0f;
vp.Width = static_cast <float> (width);
vp.Height= static_cast <float> (height);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
return true;


this is the routine that creates the container and the smaller cubes meshes

bool Game::GeoBuild(){
CreateInputLayout();
CreateRasterizerState();
CreateBox(30.0,30.0,30.0,Build);
CreateBox(3.0,3.0,3.0,Piese);
SetBuild(&B);

build_vers_index = 0;
build_vers_count= Build.vertices.size();
build_ind_index= 0;
build_ind_count=Build.indices.size();

piese_vers_index = build_vers_count;
piese_vers_count= Piese.vertices.size();
piese_ind_index= build_ind_count;
piese_ind_count=Piese.indices.size();

UINT k=0;
std::vector <Vertex> Vers(piese_vers_count + build_vers_count);
for (UINT i = 0; i < build_vers_count; i++,k++)
{
XMStoreFloat4(&Build.vertices[i].Color,(XMVECTOR)Colors::Red);
Vers[k]=Build.vertices[i];
}
for (UINT i = 0; i < piese_vers_count; i++ , k++)
{
XMStoreFloat4(&Piese.vertices[i].Color,(XMVECTOR)Colors::Green);
Vers[k]=Piese.vertices[i];
}

D3D11_BUFFER_DESC desc;
desc.BindFlags=D3D11_BIND_VERTEX_BUFFER;
desc.ByteWidth=sizeof(Vertex) * (piese_vers_count + build_vers_count);
desc.CPUAccessFlags=0;
desc.MiscFlags=0;
desc.Usage=D3D11_USAGE_IMMUTABLE;

D3D11_SUBRESOURCE_DATA Data;
ZeroMemory(&Data,sizeof(D3D11_SUBRESOURCE_DATA));
Data.pSysMem=&Vers[0];

HR(d3dDevice->CreateBuffer(&desc,&Data,&vertices));
std::vector<UINT> inds(piese_ind_count + build_ind_count);
k=0;
for (UINT i = 0; i < build_ind_count; i++,k++)
{
inds[k]=Build.indices[i];
}
for (UINT i = 0; i < piese_ind_count; i++ , k++)
{
inds[k]=Piese.indices[i];
}

desc.BindFlags=D3D11_BIND_INDEX_BUFFER;
desc.ByteWidth=sizeof(UINT) * inds.size();
desc.CPUAccessFlags=0;
desc.MiscFlags=0;
desc.Usage=D3D11_USAGE_IMMUTABLE;
ZeroMemory(&Data,sizeof(D3D11_SUBRESOURCE_DATA));
Data.pSysMem=&inds[0];
HR(d3dDevice->CreateBuffer(&desc,&Data,&indices));

return true;
}


SetBuild() Prepare the matrices inside the container for the smaller cubes ....i didnt program it to draw the smaller cubes yet

and this the function that draws the scene

void Game::Render(){
d3dDeviceContext->ClearRenderTargetView(RenderTarget,reinterpret_cast <const float*> (&Colors::LightSteelBlue));
d3dDeviceContext->ClearDepthStencilView(depth,D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);

d3dDeviceContext-> IASetInputLayout(_layout);
d3dDeviceContext-> IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dDeviceContext->IASetIndexBuffer(indices,DXGI_FORMAT_R32_UINT,0);
UINT strides=sizeof(Vertex),off=0;
d3dDeviceContext->IASetVertexBuffers(0,1,&vertices,&strides,&off);

D3DX11_TECHNIQUE_DESC des;
Tech->GetDesc(&des);Floor * Lookup;
std::vector<XMFLOAT4X4> filled;
XMMATRIX vp = V * P;XMMATRIX wvp;XMFLOAT4X4 fwvp;
for (UINT i = 0; i < des.Passes; i++)
{
wvp = XMLoadFloat4x4(&(B.Memory[0].Pieces[0])) * vp;
XMStoreFloat4x4(&fwvp,wvp);
HR(Tech->GetPassByIndex(i)->Apply(0,d3dDeviceContext));
d3dDeviceContext->DrawIndexed(build_ind_count,build_ind_index,build_vers_index);

UINT r1=B.GetSize(),r2=filled.size();
for (UINT j = 0; j < r1; j++)
{
Lookup = &B.Memory[j];
for (UINT r = 0; r < Lookup->filledindeces.size(); r++)
{
filled.push_back(Lookup->Pieces[Lookup->filledindeces[r]]);
}
}
for (UINT j = 0; j < r2; j++)
{
ShadeMat->SetMatrix( reinterpret_cast<const float*> (&filled[i]));
Tech->GetPassByIndex(i)->Apply(0,d3dDeviceContext);
d3dDeviceContext->DrawIndexed(piese_ind_count,piese_ind_index,piese_vers_index);
}
}
HR(swapchain->Present(0,0));
}


##### Share on other sites

Some common things to check are:

1. Your geometry is being transformed out of the viewport (possibly due to matrices being incorrect).

2. Your geometry is being culled by the back face culling check in the rasterizer (test this by disabling back face culling).

3. Your pixels are being discarded by the depth test (are you clearing the depth buffer each frame?).

Most of these cases can be found quickly with the graphics debugger (on Win8) or perhaps PIX (on some Win7 systems without the platform update).

##### Share on other sites

yes i am clearing the depth stencil buffer each frame is there any problem?...

my rasterizer state  is setted on CULL_MODE_FRONT .....because i want to see what in the container.....

i can't use PIX it always crashes even with the correctly working programs (i am using win7)..... i checked camera and matrices multiple Times and i even increased the Length of camera radius

but stills nothing ...

Thanks for your time

##### Share on other sites

You have some unusual code snippets. Are these what you intend?

UINT strides=sizeof(Vertex),off=0;
// did you mean
// UINT off = 0;
...
Tech->GetDesc(&des);Floor * Lookup; // "Floor * Lookup" does nothing

As Jason Z mentioned, have you checked that the geometry you create is within the view volume?

my rasterizer state  is setted on CULL_MODE_FRONT

Have you defined CULL_MODE_FRONT yourself, or do you intend D3D11_CULL_FRONT?

I would suggest you try rendering the cube normally - i.e., D3D11_CULL_BACK - and get that working.

##### Share on other sites

You have some unusual code snippets. Are these what you intend?
UINT strides=sizeof(Vertex),off=0;
// did you mean
// UINT off = 0;
...
Tech->GetDesc(&des);Floor * Lookup; // "Floor * Lookup" does nothing

As Jason Z mentioned, have you checked that the geometry you create is within the view volume?

yes i mean off =0, Lookup is variable to lookup the matrices of the smaller cubes there is no smaller cubes now so it should do nothing

Have you defined CULL_MODE_FRONT yourself, or do you intend D3D11_CULL_FRONT?

I would suggest you try rendering the cube normally - i.e., D3D11_CULL_BACK - and get that working.

i set it to the default rasterizer state (RSSetState(0)) also changed the camera position to (0.0,0.0,0.0) and and the focus to (15.0,15.0,15.0) and radius to 50.0

is there anything else beside what jason said that can fail drawing ...i am new to this so there might be some deep details i missed...thanks

##### Share on other sites

Thanks Jason Z ..and Buckeye

it is solved ....The Problem is simply

in GeoBuild() (Mentioned in the Post )

std::vector<UINT> inds(piese_ind_count + build_ind_count); //cause of the bug
std::vector<UINT> inds                                     //works fine


i dont know why and how but i guess std::vector constructor fills its element by some values which corrupt the index buffer....

thanks all for your time

##### Share on other sites

Just  a follow up, if you pass an integer to the std::vector constructor, it will create a vector with that many elements in it that are default constructed if you don't pass a second value.  This is called the 'fill' constructor.  Since UINT doesn't have a default constructor mechanism, you end up with the contents of memory in your vector.

Edited by Jason Z

##### Share on other sites

Since UINT doesn't have a default constructor mechanism, you end up with the contents of memory in your vector.

The parameter is declared as const value_type& val = value_type(), so it will perform zero initialization for built-in types:

int a; // garbage
int b = int(); // zero initialized


##### Share on other sites

Since UINT doesn't have a default constructor mechanism, you end up with the contents of memory in your vector.

The parameter is declared as const value_type& val = value_type(), so it will perform zero initialization for built-in types:

int a; // garbage
int b = int(); // zero initialized


Good call - I just tried it out and you are 100% correct.  I wonder how the OP was getting garbage in the vector then... I suppose if he has all 0's in the index buffer, and then pushes back all of his indices, it would look like a ton of senseless indices, but I don't know...

Thanks for the correction.

• ### Similar Content

• Hi, can somebody please tell me in clear simple steps how to debug and step through an hlsl shader file?
I already did Debug > Start Graphics Debugging > then captured some frames from Visual Studio and
double clicked on the frame to open it, but no idea where to go from there.

I've been searching for hours and there's no information on this, not even on the Microsoft Website!
They say "open the  Graphics Pixel History window" but there is no such window!
Then they say, in the "Pipeline Stages choose Start Debugging"  but the Start Debugging option is nowhere to be found in the whole interface.
Also, how do I even open the hlsl file that I want to set a break point in from inside the Graphics Debugger?

All I want to do is set a break point in a specific hlsl file, step thru it, and see the data, but this is so unbelievably complicated

• I finally ported Rastertek's tutorial # 42 on soft shadows and blur shading. This tutorial has a ton of really useful effects and there's no working version anywhere online.
Unfortunately it just draws a black screen. Not sure what's causing it. I'm guessing the camera or ortho matrix transforms are wrong, light directions, or maybe texture resources not being properly initialized.  I didnt change any of the variables though, only upgraded all types and functions DirectX3DVector3 to XMFLOAT3, and used DirectXTK for texture loading. If anyone is willing to take a look at what might be causing the black screen, maybe something pops out to you, let me know, thanks.

Also, for reference, here's tutorial #40 which has normal shadows but no blur, which I also ported, and it works perfectly.

• By xhcao
Is Direct3D 11 an api function like glMemoryBarrier in OpenGL? For example, if binds a texture to compute shader, compute shader writes some values to texture, then dispatchCompute, after that, read texture content to CPU side. I know, In OpenGL, we could call glMemoryBarrier before reading to assure that texture all content has been updated by compute shader.
How to handle incoherent memory access in Direct3D 11? Thank you.
• By _Engine_
Atum engine is a newcomer in a row of game engines. Most game engines focus on render
techniques in features list. The main task of Atum is to deliver the best toolset; that’s why,
as I hope, Atum will be a good light weighted alternative to Unity for indie games. Atum already
has fully workable editor that has an ability to play test edited scene. All system code has
simple ideas behind them and focuses on easy to use functionality. That’s why code is minimized
as much as possible.
Currently the engine consists from:
- Scene Editor with ability to play test edited scene;
- Powerful system for binding properties into the editor;
- Render system based on DX11 but created as multi API; so, adding support of another GAPI
is planned;
- Controls system based on aliases;
- Font system based on stb_truetype.h;
- Support of PhysX 3.0, there are samples in repo that use physics;
- Network code which allows to create server/clinet; there is some code in repo which allows
to create a simple network game
I plan to use this engine in multiplayer game - so, I definitely will evolve the engine. Also
I plan to add support for mobile devices. And of course, the main focus is to create a toolset
that will ease games creation.
Link to repo on source code is - https://github.com/ENgineE777/Atum
Video of work process in track based editor can be at follow link:

• I made a spotlight that
1. Projects 3d models onto a render target from each light POV to simulate shadows
2. Cuts a circle out of the square of light that has been projected onto the render target
as a result of the light frustum, then only lights up the pixels inside that circle
(except the shadowed parts of course), so you dont see the square edges of the projected frustum.

After doing an if check to see if the dot product of light direction and light to vertex vector is greater than .95
to get my initial cutoff, I then multiply the light intensity value inside the resulting circle by the same dot product value,
which should range between .95 and 1.0.

This should give the light inside that circle a falloff from 100% lit to 0% lit toward the edge of the circle. However,
there is no falloff. It's just all equally lit inside the circle. Why on earth, I have no idea. If someone could take a gander
and let me know, please help, thank you so much.