Hello,
I am new to directx programming and i am having some problems drawing my models to screen.
i am trying to impliment the DrawInstanced tutorial from rastertek : http://www.rastertek.com/dx11tut37.html into my game engine. but i am having trouble where the model is not being drawn to my screen. in the image below you can see that it is calling the vertex shader but not the pixel shader.
The pixel shader is not set to null so it should be called but as far as i can see it is not.
the code below is my messy attempt to go as bare bones as possible and no other calls ar made any ware else other than the usual clear screen stuff.
#include "TestGameState.h"
#include "DirectX\DX_ScreenHandler.h"
#include "DirectX\Texture\DX_Texture.h"
#include "DirectX\Shader\DX_Shader.h" //just the load (LoadShaderFile) function is used
#include <DirectXMath.h>
using namespace DirectX;
//model
ID3D11Buffer* m_vertexBuffer;
ID3D11Buffer* m_instanceBuffer;
int m_vertexCount;
int m_instanceCount;
DX_Texture* m_Texture;
//shader
ID3D11InputLayout* InputLayout; // the pointer to the input layout
//ID3D11Buffer* ConstantBuffer; = m_matrixBuffer // the constant buffer interface
ID3D11VertexShader* VertexShader; // the pointer to the vertex shader
ID3D11PixelShader* PixelShader; // the pointer to the pixel shader
ID3D11Buffer* m_matrixBuffer;
ID3D11SamplerState* m_sampleState;
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
unsigned int numElements;
D3D11_BUFFER_DESC matrixBufferDesc;
D3D11_SAMPLER_DESC samplerDesc;
struct VertexType
{
XMFLOAT3 position;
XMFLOAT2 texture;
};
struct InstanceType
{
XMFLOAT3 position;
};
struct MatrixBufferType
{
XMMATRIX world;
XMMATRIX view;
XMMATRIX projection;
};
bool InitModel()
{
ID3D11Device* device = TheDXScreen::Instance().GetDevice();
VertexType* vertices;
InstanceType* instances;
D3D11_BUFFER_DESC vertexBufferDesc, instanceBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, instanceData;
HRESULT result;
// Set the number of vertices in the vertex array.
m_vertexCount = 3;
// Create the vertex array.
vertices = new VertexType[m_vertexCount];
if (!vertices)
{
return false;
}
// Load the vertex array with data.
vertices[0].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); // Bottom left.
vertices[0].texture = XMFLOAT2(0.0f, 1.0f);
vertices[1].position = XMFLOAT3(0.0f, 1.0f, 0.0f); // Top middle.
vertices[1].texture = XMFLOAT2(0.5f, 0.0f);
vertices[2].position = XMFLOAT3(1.0f, -1.0f, 0.0f); // Bottom right.
vertices[2].texture = XMFLOAT2(1.0f, 1.0f);
// Set up the description of the static vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Now create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if (FAILED(result))
{
return false;
}
// Release the vertex array now that the vertex buffer has been created and loaded.
delete[] vertices;
vertices = 0;
// Set the number of instances in the array.
m_instanceCount = 4;
// Create the instance array.
instances = new InstanceType[m_instanceCount];
if (!instances)
{
return false;
}
// Load the instance array with data.
instances[0].position = XMFLOAT3(-1.5f, -1.5f, 5.0f);
instances[1].position = XMFLOAT3(-1.5f, 1.5f, 5.0f);
instances[2].position = XMFLOAT3(1.5f, -1.5f, 5.0f);
instances[3].position = XMFLOAT3(1.5f, 1.5f, 5.0f);
// Set up the description of the instance buffer.
instanceBufferDesc.Usage = D3D11_USAGE_DEFAULT;
instanceBufferDesc.ByteWidth = sizeof(InstanceType) * m_instanceCount;
instanceBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instanceBufferDesc.CPUAccessFlags = 0;
instanceBufferDesc.MiscFlags = 0;
instanceBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the instance data.
instanceData.pSysMem = instances;
instanceData.SysMemPitch = 0;
instanceData.SysMemSlicePitch = 0;
// Create the instance buffer.
result = device->CreateBuffer(&instanceBufferDesc, &instanceData, &m_instanceBuffer);
if (FAILED(result))
{
return false;
}
// Release the instance array now that the instance buffer has been created and loaded.
delete[] instances;
instances = 0;
return true;
}
bool Shader()
{
ID3D11Device* device = TheDXScreen::Instance().GetDevice();
byte* VSData = nullptr;
int VSSize = 0;
byte* PSData = nullptr;
int PSSize = 0;
bool VSCompiled = DX_Shader::LoadShaderFile("InstancedVertexShader.cso", VSData, VSSize);
bool PSCompiled = DX_Shader::LoadShaderFile("InstancedPixelShader.cso", PSData, PSSize);
if (!VSCompiled || !PSCompiled)
return false;
// encapsulate both shaders into shader objects
device->CreateVertexShader(VSData, VSSize, NULL, &VertexShader);
device->CreatePixelShader(PSData, PSSize, NULL, &PixelShader);
// Create the vertex input layout description.
// This setup needs to match the VertexType stucture in the ModelClass and in the shader.
polygonLayout[0].SemanticName = "POSITION";
polygonLayout[0].SemanticIndex = 0;
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[0].InputSlot = 0;
polygonLayout[0].AlignedByteOffset = 0;
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[0].InstanceDataStepRate = 0;
polygonLayout[1].SemanticName = "TEXCOORD";
polygonLayout[1].SemanticIndex = 0;
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[1].InputSlot = 0;
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0;
polygonLayout[2].SemanticName = "TEXCOORD";
polygonLayout[2].SemanticIndex = 1;
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[2].InputSlot = 1;
polygonLayout[2].AlignedByteOffset = 0;
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[2].InstanceDataStepRate = 1;
// Get a count of the elements in the layout.
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
HRESULT result = device->CreateInputLayout(polygonLayout, numElements, VSData, VSSize, &InputLayout);
if (FAILED(result))
{
return false;
}
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
delete VSData;
VSData = nullptr;
delete PSData;
PSData = nullptr;
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
matrixBufferDesc.MiscFlags = 0;
matrixBufferDesc.StructureByteStride = 0;
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
if (FAILED(result))
{
return false;
}
// Create a texture sampler state description.
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 0;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
// Create the texture sampler state.
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
if (FAILED(result))
{
return false;
}
return true;
}
void TestGameState::Initialize()
{
TheDXScreen::Instance().SetBackBufferColor("#6495ED");
//init model & texture
bool res = InitModel();
if (res == false)
throw "whaa epic fail";
m_Texture = new DX_Texture();
res = m_Texture->Initialize(L"roguelikeSheet_transparent.png");
if (res == false)
throw "whaa epic fail";
//shader
res = Shader();
if (res == false)
throw "whaa epic fail";
}
void TestGameState::Destroy()
{
}
void TestGameState::Pause()
{
}
void TestGameState::Resume()
{
}
void TestGameState::Update()
{
}
// Camera rotation
static float lookatZ = 0;
static float lookatY = 0;
static float Zoom = 10;
void TestGameState::Draw()
{
ID3D11Device* device = TheDXScreen::Instance().GetDevice();
ID3D11DeviceContext* deviceContext = TheDXScreen::Instance().GetContext();
lookatZ += 0.0001f;
//camera
// calculate the world matrices
XMMATRIX worldMatrix = DirectX::XMMatrixIdentity();//DirectX::XMMatrixRotationY(Time);
// calculate the view transformation
//XMVECTOR vecCamPosition = DirectX::XMVectorSet(0, 0, -10, 0);
XMVECTOR vecCamPosition = DirectX::XMVectorSet((cosf(lookatZ) * Zoom), sinf(lookatY) * Zoom, (sinf(lookatZ) * Zoom), 0);
XMVECTOR vecCamLookAt = XMVectorSet(0, 0, 0, 0);
XMVECTOR vecCamUp = XMVectorSet(0, 1, 0, 0);
XMMATRIX viewMatrix = XMMatrixLookAtLH(vecCamPosition, vecCamLookAt, vecCamUp);
// calculate the projection transformation
//CoreWindow^ Window = CoreWindow::GetForCurrentThread(); // get the window pointer
XMMATRIX projectionMatrix = XMMatrixPerspectiveFovLH(
XMConvertToRadians(45), // the field of view
(FLOAT)TheDXScreen::Instance().GetScreenSize().Width / (FLOAT)TheDXScreen::Instance().GetScreenSize().Height, // aspect ratio
1, // the near view-plane
100);
//do stuff
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
unsigned int strides[2];
unsigned int offsets[2];
ID3D11Buffer* bufferPointers[2];
// Set the buffer strides.
strides[0] = sizeof(VertexType);
strides[1] = sizeof(InstanceType);
// Set the buffer offsets.
offsets[0] = 0;
offsets[1] = 0;
// Set the array of pointers to the vertex and instance buffers.
bufferPointers[0] = m_vertexBuffer;
bufferPointers[1] = m_instanceBuffer;
// Set the vertex buffer to active in the input assembler so it can be rendered.
deviceContext->IASetVertexBuffers(0, 2, bufferPointers, strides, offsets);
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Render the model using the texture shader.
// Set the shader parameters that it will use for rendering.
{
HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
MatrixBufferType* dataPtr;
unsigned int bufferNumber;
// Lock the constant buffer so it can be written to.
result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(result))
{
int fail = 0;//break point
}
// Get a pointer to the data in the constant buffer.
dataPtr = (MatrixBufferType*)mappedResource.pData;
// Copy the matrices into the constant buffer.
dataPtr->world = worldMatrix;
dataPtr->view = viewMatrix;
dataPtr->projection = projectionMatrix;
// Unlock the constant buffer.
deviceContext->Unmap(m_matrixBuffer, 0);
// Set the position of the constant buffer in the vertex shader.
bufferNumber = 0;
// Now set the constant buffer in the vertex shader with the updated values.
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
// Set shader texture resource in the pixel shader.
m_Texture->SetTexture();
}
// Now render the prepared buffers with the shader.
{
// Set the vertex and pixel shaders that will be used to render this triangle.
deviceContext->VSSetShader(VertexShader, nullptr, 0);
deviceContext->PSSetShader(PixelShader, nullptr, 0);
// Set the vertex input layout.
deviceContext->IASetInputLayout(InputLayout);
// Set the sampler state in the pixel shader.
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
// Render the triangle.
deviceContext->DrawInstanced(m_vertexCount, m_instanceCount, 0, 0);
}
}
if you need any more information please let me know, thanks for the help.
Edit:
i had a look at microsofts attempt to help
https://msdn.microsoft.com/en-us/library/jj191650.aspx but i am pretty sure this is not my problem.
and this seems to be the problem i am having but i do not understand the solution "OMSetBlendState" i am not using that
http://www.gamedev.net/topic/649903-beginning-d3d11-missing-pixel-shader/