bool Material::loadAndInitializeMaterial(const string& effectName, ID3D11Device* pd3dDevice, const InputLayoutDescription& inputDescription) { bool succesfull = true; ID3D11InputLayout* layout; m_shader = new Shader(effectName, pd3dDevice); succesfull = m_shader->isShaderInitialized(); if (!succesfull) { return succesfull; } D3DX11_PASS_DESC PassDesc; m_shader->getEffect()->GetTechniqueByIndex(0)->GetPassByIndex( 0 )->GetDesc( &PassDesc ); HRESULT hr = pd3dDevice->CreateInputLayout(inputDescription.getElementDescriptions(), inputDescription.getElementCount(), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &layout ); if(FAILED(hr)) showErrorDialog("Failed to create input layout"); m_layout = layout; layout->Release(); return succesfull; }
int Release() { if (VS) VS->Release(); if (PS) PS->Release(); if (inputLayout) inputLayout->Release(); if (vertexBuffer) vertexBuffer->Release(); return 0; }
void CPUTInputLayoutCacheDX11::Apply(CPUTMesh* pMesh, CPUTMaterial* pMaterial) { ID3D11Device* pDevice = CPUT_DX11::GetDevice(); D3D11_INPUT_ELEMENT_DESC *pDXLayout = ((CPUTMeshDX11*)pMesh)->GetLayoutDescription(); CPUTVertexShaderDX11 *pVertexShader = ((CPUTMaterialDX11*)pMaterial)->GetVertexShader(); ID3D11InputLayout *pInputLayout; if (CPUT_SUCCESS == GetLayout(pDevice, pDXLayout, pVertexShader, &pInputLayout)) { CPUT_DX11::GetContext()->IASetInputLayout(pInputLayout); pInputLayout->Release(); } }
void MFVertex_DestroyVertexDeclarationPlatformSpecific(MFVertexDeclaration *pDeclaration) { ID3D11InputLayout *pVertexLayout = (ID3D11InputLayout*)pDeclaration->pPlatformData; pVertexLayout->Release(); }
bool DX11ResourceManager::initializeDefaultSurfaceEffect( const MString& effectsLocation, ID3D11Device* D3D, const MString& effectName, const MString& vsName, const MString& psName, const D3D11_INPUT_ELEMENT_DESC* layout, int numLayoutElements ) // // Description: // Initialize default surface effects found in a given directory. // { HRESULT hres; MString effectLocation = effectsLocation + "\\" + effectName + ".hlsl"; ID3DBlob* pVSBlob = NULL; hres = CompileShaderFromFile( effectLocation.asChar(), vsName.asChar(), "vs_4_0", &pVSBlob ); if (FAILED(hres)) { MGlobal::displayInfo("Failed to compile vertex shader " + vsName + " in file: " + effectLocation); return false; } ID3D11VertexShader* pVertexShader = NULL; hres = D3D->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &pVertexShader ); if (FAILED(hres)) { MGlobal::displayInfo("Failed to create vertex shader " + vsName + " in file: " + effectLocation); pVSBlob->Release(); return false; } ID3D11InputLayout* pVertexLayout = NULL; hres = D3D->CreateInputLayout( layout, numLayoutElements, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &pVertexLayout ); pVSBlob->Release(); if (FAILED(hres)) { MGlobal::displayInfo("Failed to create input layout for file: " + effectLocation); return false; } ID3DBlob* pPSBlob = NULL; hres = CompileShaderFromFile( effectLocation.asChar(), psName.asChar(), "ps_4_0", &pPSBlob ); if (FAILED(hres)) { MGlobal::displayInfo("Failed to compile pixel shader " + psName + " in file: " + effectLocation); pVertexShader->Release(); pVertexLayout->Release(); return false; } ID3D11PixelShader* pPixelShader = NULL; hres = D3D->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &pPixelShader ); pPSBlob->Release(); if (FAILED(hres)) { MGlobal::displayInfo("Failed to create pixel shader " + psName + " in file: " + effectLocation); pVertexShader->Release(); pVertexLayout->Release(); return false; } // Create a new effect item // MGlobal::displayInfo("Maya default pixel shader loaded: " + effectLocation); SurfaceEffectItem *pei = new SurfaceEffectItem; if (pei) { pei->fVertexShader = pVertexShader; pei->fPixelShader = pPixelShader; pei->fInputLayout = pVertexLayout; m_SurfaceEffectItemList[ effectName.asChar() ] = pei; } return true; }
bool SetupRendering(osvr::renderkit::GraphicsLibrary library) { // Make sure our pointers are filled in correctly. if (library.D3D11 == nullptr) { std::cerr << "SetupRendering: No D3D11 GraphicsLibrary, this should " "not happen" << std::endl; return false; } ID3D11Device* device = library.D3D11->device; ID3D11DeviceContext* context = library.D3D11->context; // Setup vertex shader auto hr = device->CreateVertexShader(g_triangle_vs, sizeof(g_triangle_vs), nullptr, vertexShader.GetAddressOf()); if (FAILED(hr)) { return false; } // Setup pixel shader hr = device->CreatePixelShader(g_triangle_ps, sizeof(g_triangle_ps), nullptr, pixelShader.GetAddressOf()); if (FAILED(hr)) { return false; } // Set the input layout ID3D11InputLayout* vertexLayout; D3D11_INPUT_ELEMENT_DESC layout[] = { {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; hr = device->CreateInputLayout(layout, _countof(layout), g_triangle_vs, sizeof(g_triangle_vs), &vertexLayout); if (SUCCEEDED(hr)) { context->IASetInputLayout(vertexLayout); vertexLayout->Release(); vertexLayout = nullptr; } // Create vertex buffer SimpleVertex vertices[3]; vertices[0].Pos.x = 0.0f; vertices[0].Pos.y = 0.5f; vertices[0].Pos.z = 0.5f; vertices[1].Pos.x = 0.5f; vertices[1].Pos.y = -0.5f; vertices[1].Pos.z = 0.5f; vertices[2].Pos.x = -0.5f; vertices[2].Pos.y = -0.5f; vertices[2].Pos.z = 0.5f; CD3D11_BUFFER_DESC bufferDesc(sizeof(SimpleVertex) * _countof(vertices), D3D11_BIND_VERTEX_BUFFER); D3D11_SUBRESOURCE_DATA subResData = {vertices, 0, 0}; hr = device->CreateBuffer(&bufferDesc, &subResData, &g_vertexBuffer); // Describe how depth and stencil tests should be performed. // In particular, that they should not be for this 2D example // where we want to render a triangle no matter what. D3D11_DEPTH_STENCIL_DESC depthStencilDescription = {}; depthStencilDescription.DepthEnable = false; depthStencilDescription.StencilEnable = false; // Create depth stencil state and set it. hr = device->CreateDepthStencilState(&depthStencilDescription, &g_depthStencilState); if (FAILED(hr)) { std::cerr << "SetupRendering: Could not create depth/stencil state" << std::endl; return false; } return true; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow) { DXTInputHandlerDefault inputHandler; DXTWindowEventHandlerDefault eventHandler; DXTWindow window(hInstance, &inputHandler, &eventHandler); DXTRenderParams params; params.Extent = { 800, 600 }; params.UseVSync = true; params.Windowed = true; HRESULT result; result = window.Initialize(params, "DXT Example (DirectX 11)"); if (SUCCEEDED(result)) { ID3D11Device* device; ID3D11DeviceContext* context; IDXGISwapChain* swapChain; result = DXTInitDevice(params, &window, &swapChain, &device, &context); if (SUCCEEDED(result)) { eventHandler.SetSwapChain(swapChain); FLOAT clearColor[] = { 0.5f, 0.5f, 1.0f, 1.0f }; D3D11_INPUT_ELEMENT_DESC inputDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, sizeof(float) * 3, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, sizeof(float) * 5, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; UINT elementCount = 3; UINT stride = 8 * sizeof(FLOAT); UINT offset = 0; UINT indexCount = 0; FLOAT deltaTime = 0.016f; DXTSphericalCamera camera; DXTFirstPersonCameraController cameraController(&camera, &inputHandler); inputHandler.AddInputInterface(&cameraController); cameraController.Velocity = 40.0f; cameraController.RotationVelocity = 0.005f; camera.Position = DirectX::XMFLOAT3(20.0f, 20.0f, 20.0f); camera.LookAt(DirectX::XMFLOAT3(0.0f, 0.0f, 0.0f)); ID3D11RenderTargetView* renderTargetView; ID3D11Texture2D* depthBuffer; ID3D11DepthStencilView* depthBufferView; ID3D11VertexShader* vertexShader; ID3D11PixelShader* pixelShader; DXTBytecodeBlob vertexBytecode; ID3D11DepthStencilState* depthState; ID3D11RasterizerState* rasterizerState; ID3D11Buffer* vertexBuffer; ID3D11Buffer* indexBuffer; ID3D11InputLayout* inputLayout; ID3D11Buffer* transformBuffer; DXTCreateRenderTargetFromBackBuffer(swapChain, device, &renderTargetView); DXTCreateDepthStencilBuffer(device, params.Extent.Width, params.Extent.Height, DXGI_FORMAT_D24_UNORM_S8_UINT, &depthBuffer, &depthBufferView); DXTVertexShaderFromFile(device, "VertexShader.cso", &vertexShader, &vertexBytecode); DXTPixelShaderFromFile(device, "PixelShader.cso", &pixelShader); DXTCreateDepthStencilStateDepthTestEnabled(device, &depthState); DXTCreateRasterizerStateSolid(device, &rasterizerState); DXTLoadStaticMeshFromFile(device, "mesh.ase", DXTVertexAttributePosition | DXTVertexAttributeUV | DXTVertexAttributeNormal, DXTIndexTypeShort, &vertexBuffer, &indexBuffer, &indexCount); DXTCreateBuffer(device, sizeof(DirectX::XMFLOAT4X4) * 2, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE, D3D11_USAGE_DYNAMIC, &transformBuffer); device->CreateInputLayout(inputDesc, elementCount, vertexBytecode.Bytecode, vertexBytecode.BytecodeLength, &inputLayout); vertexBytecode.Destroy(); window.Present(false); while (!window.QuitMessageReceived()) { window.MessagePump(); if (inputHandler.IsKeyDown(VK_ESCAPE)) break; cameraController.Update(deltaTime); XMFLOAT4X4 ViewProj; XMFLOAT4X4 World; camera.GetViewProjectionMatrix(&ViewProj, params.Extent); XMStoreFloat4x4(&World, XMMatrixIdentity()); D3D11_MAPPED_SUBRESOURCE subres; context->Map(transformBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &subres); XMFLOAT4X4* ptr = (XMFLOAT4X4*)subres.pData; ptr[0] = World; ptr[1] = ViewProj; context->Unmap(transformBuffer, 0); D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (FLOAT)params.Extent.Width, (FLOAT)params.Extent.Height, 0.0f, 1.0f }; context->ClearRenderTargetView(renderTargetView, clearColor); context->ClearDepthStencilView(depthBufferView, D3D11_CLEAR_DEPTH, 1.0f, 0); context->OMSetDepthStencilState(depthState, 0); context->OMSetRenderTargets(1, &renderTargetView, depthBufferView); context->RSSetState(rasterizerState); context->RSSetViewports(1, &viewport); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); context->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset); context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); context->IASetInputLayout(inputLayout); context->VSSetShader(vertexShader, nullptr, 0); context->PSSetShader(pixelShader, nullptr, 0); context->VSSetConstantBuffers(0, 1, &transformBuffer); context->DrawIndexed(indexCount, 0, 0); swapChain->Present(1, 0); } swapChain->SetFullscreenState(false, nullptr); transformBuffer->Release(); depthBufferView->Release(); depthBuffer->Release(); inputLayout->Release(); vertexBuffer->Release(); indexBuffer->Release(); depthState->Release(); rasterizerState->Release(); vertexShader->Release(); pixelShader->Release(); renderTargetView->Release(); swapChain->Release(); context->Release(); device->Release(); } window.Destroy(); } }