void D3D11EffectMaterial::SetVectorByName(const char* szName, const math::Vector3& v) { ID3DX11EffectVariable* pVal = m_pEffect->GetVariableByName(szName); pVal->AsVector()->SetFloatVector(v.v); //m_pEffect->GetVariableByName(szName)->AsScalar()->SetFloatArray(v.v, 0, 3); }
void Effect::SetParam(const string& name, const void* data, U32 offset, U32 count) { auto it = structVars.find(name); _ASSERT(it != structVars.end()); ID3DX11EffectVariable* var = it->second; DXCall(var->SetRawValue(data, offset, count)); }
ID3DX11EffectVariable* CUniformParameterBuilder::findAnnotationByName(ID3DX11EffectVariable *effectVariable, const char* name) { // The latest effect 11 library is very verbose when an annotation // is not found by name. This version will stay quiet if the // annotation is not found. if (mAnnotationIndex.empty()) { D3DX11_EFFECT_VARIABLE_DESC varDesc; effectVariable->GetDesc(&varDesc); for (uint32_t idx = 0; idx < varDesc.Annotations; ++idx) { ID3DX11EffectVariable* var = effectVariable->GetAnnotationByIndex(idx); if (var) { D3DX11_EFFECT_VARIABLE_DESC varDesc; var->GetDesc(&varDesc); mAnnotationIndex.insert(TAnnotationIndex::value_type(std::string(varDesc.Name), idx)); } } mAnnotationIndex.insert(TAnnotationIndex::value_type(std::string("Done!"), 0)); } TAnnotationIndex::const_iterator index = mAnnotationIndex.find(std::string(name)); if (index != mAnnotationIndex.end()) return effectVariable->GetAnnotationByIndex((*index).second); else return 0; }
bool Material::bind(RenderContext* context) { if( this == context->getBoundMaterial() ) { return false; } ID3DX11Effect* effect = m_shader->getEffect(); context->getImmediateContext()->IASetInputLayout( m_layout ); for(std::map<string, ResourceID>::iterator iter = m_textureRefs.begin(); iter!=m_textureRefs.end(); ++iter) { ID3DX11EffectVariable* var = effect->GetVariableByName(iter->first.c_str()); if(var->IsValid()) { ID3DX11EffectShaderResourceVariable* texResource = var->AsShaderResource(); Texture* tex = TextureManager::singleton()->getTexture(iter->second); if(tex) { texResource->SetResource(tex->getShaderResourceView()); } else { texResource->SetResource(0); } } } return true; }
void D3D11EffectMaterial::SetCBBySemantic(const char* szName, GPUBufferPtr pCB) { ID3DX11EffectVariable* pVal = m_pEffect->GetVariableBySemantic(szName); if(pVal) { pVal->AsConstantBuffer()->SetConstantBuffer(((D3D11Buffer*)pCB.get())->GetD3D11BufferInterface()); } }
void D3D11EffectMaterial::SetIntBySemantic(const char* szName, int v) { ID3DX11EffectVariable* pVal = m_pEffect->GetVariableBySemantic(szName); if(pVal) { pVal->AsScalar()->SetInt(v); } }
void D3D11EffectMaterial::SetVectorBySemantic(const char* szName, const math::Vector2& v) { ID3DX11EffectVariable* pVal = m_pEffect->GetVariableBySemantic(szName); if(pVal) { pVal->AsVector()->SetFloatVector(v.v); } }
void D3D11EffectMaterial::SetMatrixBySemantic(const char* szSemantic, const math::Matrix44& mat) { ID3DX11EffectVariable* pVal = m_pEffect->GetVariableBySemantic(szSemantic); if(pVal) { pVal->AsMatrix()->SetMatrix(mat.m); } }
void D3D11EffectMaterial::SetTextureBySemantic(const char* szName, TexturePtr pTex) { ID3D11ShaderResourceView* pView = NULL; pView = ((D3D11Texture*)pTex.get())->GetShaderResourceView(); ID3DX11EffectVariable* pVal = m_pEffect->GetVariableBySemantic(szName); if(pVal) { pVal->AsShaderResource()->SetResource(pView); } }
void Effect::ExtractGlobalVars(D3DX11_EFFECT_DESC& effectDesc) { for(U32 i = 0; i < effectDesc.GlobalVariables; ++i) { ID3DX11EffectVariable* pVar = dx11Effect->GetVariableByIndex(i); D3DX11_EFFECT_VARIABLE_DESC varDesc; DXCall(pVar->GetDesc(&varDesc)); ID3DX11EffectType* pType = pVar->GetType(); D3DX11_EFFECT_TYPE_DESC typeDesc; DXCall(pType->GetDesc(&typeDesc)); AddVariable(pVar, &varDesc, &typeDesc); } }
void D3D11EffectMaterial::SetCBByName(const char* szName, void* buffer, int size) { ID3DX11EffectVariable* pVal = m_pEffect->GetVariableByName(szName); if(pVal) { pVal->SetRawValue(buffer, 0, size); //ID3D11Buffer* pD3DBuffer = nullptr; //pVal->AsConstantBuffer()->GetConstantBuffer(&pD3DBuffer); //pVal->AsConstantBuffer()->SetConstantBuffer(((D3D11Buffer*)pCB.get())->GetD3D11BufferInterface()); //D3D11_MAPPED_SUBRESOURCE desc; //m_pContext->Map(pD3DBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &desc); //memcpy(desc.pData, buffer, size); //m_pContext->Unmap(pD3DBuffer, 0); } }
void TextureModelDemo::Initialize() { SetCurrentDirectory(Utility::ExecutableDirectory().c_str()); // Compile the shader UINT shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3DCOMPILE_DEBUG; shaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = nullptr; ID3D10Blob* errorMessages = nullptr; HRESULT hr = D3DCompileFromFile(L"..\\source\\Library\\Content\\Effects\\TextureMapping.fx", nullptr, nullptr, nullptr, "fx_5_0", shaderFlags, 0, &compiledShader, &errorMessages); if (FAILED(hr)) { char* errorMessage = (errorMessages != nullptr ? (char*)errorMessages->GetBufferPointer() : "D3DX11CompileFromFile() failed"); GameException ex(errorMessage, hr); ReleaseObject(errorMessages); throw ex; } // Create an effect object from the compiled shader hr = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, mGame->Direct3DDevice(), &mEffect); if (FAILED(hr)) { throw GameException("D3DX11CreateEffectFromMemory() failed.", hr); } ReleaseObject(compiledShader); // Look up the technique, pass, and WVP variable from the effect mTechnique = mEffect->GetTechniqueByName("main11"); if (mTechnique == nullptr) { throw GameException("ID3DX11Effect::GetTechniqueByName() could not find the specified technique.", hr); } mPass = mTechnique->GetPassByName("p0"); if (mPass == nullptr) { throw GameException("ID3DX11EffectTechnique::GetPassByName() could not find the specified pass.", hr); } ID3DX11EffectVariable* variable = mEffect->GetVariableByName("WorldViewProjection"); if (variable == nullptr) { throw GameException("ID3DX11Effect::GetVariableByName() could not find the specified variable.", hr); } mWvpVariable = variable->AsMatrix(); if (mWvpVariable->IsValid() == false) { throw GameException("Invalid effect variable cast."); } variable = mEffect->GetVariableByName("ColorTexture"); if (variable == nullptr) { throw GameException("ID3DX11Effect::GetVariableByName() could not find the specified variable.", hr); } mColorTextureVariable = variable->AsShaderResource(); if (mColorTextureVariable->IsValid() == false) { throw GameException("Invalid effect variable cast."); } // Create the input layout D3DX11_PASS_DESC passDesc; mPass->GetDesc(&passDesc); D3D11_INPUT_ELEMENT_DESC inputElementDescriptions[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; if (FAILED(hr = mGame->Direct3DDevice()->CreateInputLayout(inputElementDescriptions, ARRAYSIZE(inputElementDescriptions), passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mInputLayout))) { throw GameException("ID3D11Device::CreateInputLayout() failed.", hr); } // Load the model std::unique_ptr<Model> model(new Model(*mGame, "..\\source\\Library\\Content\\Models\\Sphere.obj", true)); // Create the vertex and index buffers Mesh* mesh = model->Meshes().at(0); CreateVertexBuffer(mGame->Direct3DDevice(), *mesh, &mVertexBuffer); mesh->CreateIndexBuffer(&mIndexBuffer); mIndexCount = mesh->Indices().size(); // Load the texture std::wstring textureName = L"..\\source\\Library\\Content\\Textures\\EarthComposite.jpg"; if (FAILED(hr = DirectX::CreateWICTextureFromFile(mGame->Direct3DDevice(), mGame->Direct3DDeviceContext(), textureName.c_str(), nullptr, &mTextureShaderResourceView))) { throw GameException("CreateWICTextureFromFile() failed.", hr); } }
bool cSkyBoxEffect::Init( ID3D11Device* pDevice, ID3D11DeviceContext* pContext ) { if ( !Base::LoadFXFile( mEffectFileName, pDevice ) ) { return false; } cMeshGenerator gen; cMeshGenerator::cMesh mesh; gen.CreateSphere( 10000.f, 20, 20, mesh ); unsigned int numVerts = mesh.VerticesA.size(); unsigned int numInds = mesh.Indices.size(); if ( !mBuffers.Init( pDevice, numVerts, numInds ) ) { return false; } // map the vertices sVert_P* verts = mBuffers.BeginVertexMapping( pContext ); for ( unsigned int idxVert = 0; idxVert < numVerts; idxVert++ ) { verts[idxVert].Position = mesh.VerticesA[idxVert].Position; verts[idxVert].PositionPad = 1.f; } mBuffers.FinishVertexMapping( pContext ); // map the indices unsigned int* inds = mBuffers.BeginIndexMapping( pContext ); for ( unsigned int idxIndex = 0; idxIndex < numInds; idxIndex++ ) { inds[idxIndex] = mesh.Indices[idxIndex]; } mBuffers.FinishIndexMapping( pContext ); mTechnique = mEffect->GetTechniqueByIndex(0); if ( !mTechnique->IsValid() ) { return false; } D3DX11_PASS_DESC passDesc; mTechnique->GetPassByIndex(0)->GetDesc(&passDesc); pDevice->CreateInputLayout( sVert_P::GetElementDescPtr(), sVert_P::NUM_ELEMENTS, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mLayout ); mTransform = mEffect->GetVariableByName("WorldViewProj"); if ( mTransform->IsValid() ) { NecessaryPerFrame.push_back( new cMatrixEffectVariable( mTransform ) ); } ID3DX11EffectVariable* boxVar = mEffect->GetVariableByName("CubeMap"); if ( boxVar->IsValid() ) { NecessaryPerFrame.push_back( new cShaderResourceEffectVariable( boxVar ) ); } mSRV = boxVar->AsShaderResource(); return true; }
void hwRendererHelperDX::initializeDrawTextureEffect() { if( fDXContext == NULL ) return; // Create the effect if( fDrawTextureEffect == NULL ) { const char* simpleShaderCode = "Texture2D myTexture; \r\n" \ "SamplerState SamplerLinearWrap \r\n" \ "{ \r\n" \ " Filter = MIN_MAG_MIP_LINEAR; \r\n" \ " AddressU = Wrap; \r\n" \ " AddressV = Wrap; \r\n" \ "}; \r\n" \ "struct APP_TO_VS \r\n" \ "{ \r\n" \ " float3 Pos : POSITION; \r\n" \ " float2 TextCoord : TEXTCOORD; \r\n" \ "}; \r\n" \ "struct VS_TO_PS \r\n" \ "{ \r\n" \ " float4 Pos : SV_Position; \r\n" \ " float2 TextCoord : TEXTCOORD; \r\n" \ "}; \r\n" \ "VS_TO_PS BasicVS(APP_TO_VS IN) \r\n" \ "{ \r\n" \ " VS_TO_PS OUT; \r\n" \ " OUT.Pos = float4(IN.Pos, 1.0f); \r\n" \ " OUT.TextCoord = IN.TextCoord; \r\n" \ " return OUT; \r\n" \ "} \r\n" \ "float4 BasicPS(VS_TO_PS IN) : SV_Target \r\n" \ "{ \r\n" \ " float4 color = myTexture.Sample(SamplerLinearWrap, IN.TextCoord); \r\n" \ " return color; \r\n" \ "} \r\n" \ "technique10 simple \r\n" \ "{ \r\n" \ " pass p0 \r\n" \ " { \r\n" \ " SetVertexShader( CompileShader( vs_4_0, BasicVS() ) ); \r\n" \ " SetGeometryShader( NULL ); \r\n" \ " SetPixelShader( CompileShader( ps_4_0, BasicPS() ) ); \r\n" \ " } \r\n" \ "} \r\n"; const unsigned int simpleShaderLength = (unsigned int)strlen(simpleShaderCode); const D3D10_SHADER_MACRO macros[] = { { "DIRECT3D_VERSION", "0xb00" }, { NULL, NULL } }; #ifdef _DEBUG const unsigned int flags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION; #else const unsigned int flags = 0; #endif ID3DBlob *shader = NULL; ID3DBlob *error = NULL; #if _MSC_VER >= 1700 HRESULT hr = D3DCompile((char*)simpleShaderCode, simpleShaderLength, NULL, macros, NULL, "", "fx_5_0", flags, 0, &shader, &error); #else HRESULT hr = D3DX11CompileFromMemory((char*)simpleShaderCode, simpleShaderLength, NULL, macros, NULL, "", "fx_5_0", flags, 0, NULL, &shader, &error, NULL); #endif if( SUCCEEDED( hr ) && shader != NULL ) { hr = D3DX11CreateEffectFromMemory(shader->GetBufferPointer(), shader->GetBufferSize(), 0, fDXDevice, &fDrawTextureEffect); if( SUCCEEDED( hr ) && fDrawTextureEffect != NULL ) { ID3DX11EffectVariable* textureVariable = fDrawTextureEffect->GetVariableByName( "myTexture" ); if( textureVariable != NULL ) { fDrawTextureShaderVariable = textureVariable->AsShaderResource(); } ID3DX11EffectTechnique* technique = fDrawTextureEffect->GetTechniqueByIndex(0); if( technique ) { fDrawTexturePass = technique->GetPassByIndex(0); } } } else { MString errorStr( (error && error->GetBufferSize() > 0) ? (char*) error->GetBufferPointer() : (char*) NULL ); MGlobal::displayWarning( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kDxErrorEffect, errorStr ) ); } if( shader != NULL ) { shader->Release(); } } // Create the vertex buffers and the input layout if( fDrawTextureInputLayout == NULL && fDrawTexturePass != NULL ) { fDrawTextureVertexBuffersCount = 0; D3D11_INPUT_ELEMENT_DESC inputDesc[MAX_VERTEX_BUFFERS]; HRESULT hr; // Create the position stream { const float position[] = { -1, -1, 0, // bottom-left -1, 1, 0, // top-left 1, 1, 0, // top-right 1, -1, 0 }; // bottom-right const D3D11_BUFFER_DESC bufDesc = { sizeof(position), D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER, 0, 0, 0 }; const D3D11_SUBRESOURCE_DATA bufData = { position, 0, 0 }; ID3D11Buffer *buffer = NULL; hr = fDXDevice->CreateBuffer(&bufDesc, &bufData, &buffer); if( SUCCEEDED( hr ) && buffer != NULL ) { inputDesc[fDrawTextureVertexBuffersCount].SemanticName = "POSITION"; inputDesc[fDrawTextureVertexBuffersCount].SemanticIndex = 0; inputDesc[fDrawTextureVertexBuffersCount].Format = DXGI_FORMAT_R32G32B32_FLOAT; inputDesc[fDrawTextureVertexBuffersCount].InputSlot = fDrawTextureVertexBuffersCount; inputDesc[fDrawTextureVertexBuffersCount].AlignedByteOffset = 0; inputDesc[fDrawTextureVertexBuffersCount].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputDesc[fDrawTextureVertexBuffersCount].InstanceDataStepRate = 0; fDrawTextureVertexBuffers[fDrawTextureVertexBuffersCount] = buffer; fDrawTextureVertexBuffersStrides[fDrawTextureVertexBuffersCount] = 3 * sizeof(float); fDrawTextureVertexBuffersOffsets[fDrawTextureVertexBuffersCount] = 0; ++fDrawTextureVertexBuffersCount; } } // Create the texture coord stream if( SUCCEEDED( hr ) ) { const float textCoord[] = { 0, 1, // bottom-left 0, 0, // top-left 1, 0, // top-right 1, 1 }; // bottom-right const D3D11_BUFFER_DESC bufDesc = { sizeof(textCoord), D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER, 0, 0, 0 }; const D3D11_SUBRESOURCE_DATA bufData = { textCoord, 0, 0 }; ID3D11Buffer *buffer = NULL; hr = fDXDevice->CreateBuffer(&bufDesc, &bufData, &buffer); if( SUCCEEDED( hr ) && buffer != NULL ) { inputDesc[fDrawTextureVertexBuffersCount].SemanticName = "TEXTCOORD"; inputDesc[fDrawTextureVertexBuffersCount].SemanticIndex = 0; inputDesc[fDrawTextureVertexBuffersCount].Format = DXGI_FORMAT_R32G32_FLOAT; inputDesc[fDrawTextureVertexBuffersCount].InputSlot = fDrawTextureVertexBuffersCount; inputDesc[fDrawTextureVertexBuffersCount].AlignedByteOffset = 0; inputDesc[fDrawTextureVertexBuffersCount].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputDesc[fDrawTextureVertexBuffersCount].InstanceDataStepRate = 0; fDrawTextureVertexBuffers[fDrawTextureVertexBuffersCount] = buffer; fDrawTextureVertexBuffersStrides[fDrawTextureVertexBuffersCount] = 2 * sizeof(float); fDrawTextureVertexBuffersOffsets[fDrawTextureVertexBuffersCount] = 0; ++fDrawTextureVertexBuffersCount; } } if( SUCCEEDED( hr ) ) { D3DX11_PASS_DESC descPass; fDrawTexturePass->GetDesc(&descPass); hr = fDXDevice->CreateInputLayout(inputDesc, fDrawTextureVertexBuffersCount, descPass.pIAInputSignature, descPass.IAInputSignatureSize, &fDrawTextureInputLayout); if( FAILED( hr ) ) { MGlobal::displayWarning( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kDxErrorInputLayout ) ); } } } // Create the index buffer if( fDrawTextureIndexBuffer == NULL && fDrawTextureVertexBuffersCount > 0 && fDrawTextureInputLayout != NULL ) { const unsigned int indices[] = { 0, 1, 3, 3, 2, 1 }; fDrawTextureIndexBufferCount = _countof(indices); const D3D11_BUFFER_DESC bufDesc = { sizeof(indices), D3D11_USAGE_IMMUTABLE, D3D11_BIND_INDEX_BUFFER, 0, 0, 0 }; const D3D11_SUBRESOURCE_DATA bufData = { indices, 0, 0 }; fDXDevice->CreateBuffer(&bufDesc, &bufData, &fDrawTextureIndexBuffer); } }
void LightDemo::DrawScene() { m_dxImmediateContext->ClearRenderTargetView(m_renderTargetView.Get(), reinterpret_cast<const float*>(&oc::Colors::LightSteelBlue)); m_dxImmediateContext->ClearDepthStencilView(m_depthStencilView.Get(), D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0); m_dxImmediateContext->IASetInputLayout(m_inputLayout.Get()); m_dxImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); uint32 stride = sizeof(Vertex); uint32 offset = 0; // Set constants XMMATRIX view = XMLoadFloat4x4(&m_view); XMMATRIX proj = XMLoadFloat4x4(&m_proj); XMMATRIX viewProj = view*proj; // Set per frame constants. m_fxDirLight->SetRawValue(&m_dirLight, 0, sizeof(m_dirLight)); m_fxPointLight->SetRawValue(&m_pointLight, 0, sizeof(m_pointLight)); m_fxSpotLight->SetRawValue(&m_spotLight, 0, sizeof(m_spotLight)); m_fxEyePosW->SetRawValue(&m_camPosition, 0, sizeof(m_camPosition)); D3DX11_TECHNIQUE_DESC techDesc; m_tech->GetDesc(&techDesc); for(uint32 p = 0; p < techDesc.Passes; ++p) { //Draw the land m_dxImmediateContext->IASetVertexBuffers(0, 1, m_landVB.GetAddressOf(), &stride, &offset); m_dxImmediateContext->IASetIndexBuffer(m_landIB.Get(), DXGI_FORMAT_R32_UINT, 0); //Set per object constants XMMATRIX world = XMLoadFloat4x4(&m_landWorld); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world); XMMATRIX worldViewProj = world*viewProj; m_fxWorld->SetMatrix(reinterpret_cast<float*>(&world)); m_fxWorldInvTranspose->SetMatrix(reinterpret_cast<float*>(&worldInvTranspose)); m_fxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj)); m_fxMaterial->SetRawValue(&m_landMat, 0, sizeof(m_landMat)); m_tech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get()); m_dxImmediateContext->DrawIndexed(m_landIndexCount, 0, 0); //Draw the wave m_dxImmediateContext->IASetVertexBuffers(0, 1, m_wavesVB.GetAddressOf(), &stride, &offset); m_dxImmediateContext->IASetIndexBuffer(m_wavesIB.Get(), DXGI_FORMAT_R32_UINT, 0); world = XMLoadFloat4x4(&m_wavesWorld); worldInvTranspose = MathHelper::InverseTranspose(world); worldViewProj = world*viewProj; m_fxWorld->SetMatrix(reinterpret_cast<float*>(&world)); m_fxWorldInvTranspose->SetMatrix(reinterpret_cast<float*>(&worldInvTranspose)); m_fxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj)); m_fxMaterial->SetRawValue(&m_wavesMat, 0, sizeof(m_wavesMat)); m_tech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get()); m_dxImmediateContext->DrawIndexed(3*m_waves.TriangleCount(), 0, 0); } HR(m_swapChain->Present(0, 0)); }
void CubeDemo::initialize() { SetCurrentDirectory(Utility::ExecutableDirectory().c_str()); UINT shaderFlags = 0; #if defined(DEBUG) || defined(_DEBUG) shaderFlags |= D3DCOMPILE_DEBUG; shaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3D10Blob * compiledShader = nullptr; ID3D10Blob * errorMessages = nullptr; HRESULT hr = D3DCompileFromFile(L"Content\\Effects\\BasicEffect.fx", nullptr, nullptr, nullptr, "fx_5_0", shaderFlags, 0, &compiledShader, &errorMessages); //if (errorMessages != nullptr) //{ // char * message = (char*)errorMessages->GetBufferPointer(); // GameException ex((wchar_t*)errorMessages->GetBufferPointer(), hr); // ReleaseObject(errorMessages); // throw ex; // //ReleaseObject(compiledShader); //} if (FAILED(hr)) { throw GameException(L"D3DX11CompileFromFile() failed.", hr); } hr = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, mGame->device(), &mEffect); if (FAILED(hr)) { throw GameException(L"D3DX11CreateEffectFromMemory() failed", hr); } ReleaseObject(compiledShader); mTechnique = mEffect->GetTechniqueByName("main11"); if (mTechnique == nullptr) { throw GameException(L"ID3D11Effect::GetTechniqueByName() unable to find techique main11.", hr); } mPass = mTechnique->GetPassByName("p0"); if (mPass == nullptr) { throw GameException(L"ID3D11EffectTechnique::GetPassByName() unable to find pass p0", hr); } ID3DX11EffectVariable * variable = mEffect->GetVariableByName("WorldViewProjection"); if (variable == nullptr) { throw GameException(L"ID3DX11Effect::GetVariableByName() unable to find variable WorldViewProjection"); } mWvpVariable = variable->AsMatrix(); if (!mWvpVariable->IsValid()) { throw GameException(L"Invaild effect variable cast"); } D3DX11_PASS_DESC passDesc; mPass->GetDesc(&passDesc); D3D11_INPUT_ELEMENT_DESC inputElementDescriptions[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; if (FAILED(hr = mGame->device()->CreateInputLayout(inputElementDescriptions, ARRAYSIZE(inputElementDescriptions), passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mInputLayout))) { throw GameException(L"ID3D11Device::CreateInputLayout() failed", hr); } BasicEffectVertex vertices[] = { BasicEffectVertex(XMFLOAT4(-1.0f, 1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::RED))), BasicEffectVertex(XMFLOAT4(1.0f, 1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::GREEN))), BasicEffectVertex(XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::BLACK))) , BasicEffectVertex(XMFLOAT4(-1.0f, 1.0f, 1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::WHITE))), BasicEffectVertex(XMFLOAT4(-1.0f,- 1.0f, 1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::YELLOW))), BasicEffectVertex(XMFLOAT4(+1.0f, -1.0f, +1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::BLACK))), BasicEffectVertex(XMFLOAT4(1.0f, -1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::CYAN))), BasicEffectVertex(XMFLOAT4(-1.0f, -1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<float*>(&Colors::GREEN))) }; D3D11_BUFFER_DESC vertexBufferDesc; ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc)); vertexBufferDesc.ByteWidth = sizeof(BasicEffectVertex)* ARRAYSIZE(vertices); vertexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE; vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; D3D11_SUBRESOURCE_DATA vertexSubResourceData; ZeroMemory(&vertexSubResourceData, sizeof(vertexSubResourceData)); vertexSubResourceData.pSysMem = vertices; if (FAILED(mGame->device()->CreateBuffer(&vertexBufferDesc, &vertexSubResourceData, &mVertexBuffer))) { throw GameException(L"ID3D11Device::CreateBuffer() failed"); } UINT indices[] = { 0,1,2, 0,2,3, 4,5,6, 4,6,7, 3,2,5, 3,5,4, 2,1,6, 2,6,5, 1,7,6, 1,0,7, 0,3,4, 0,4,7, }; D3D11_BUFFER_DESC indexBufferDesc; ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc)); indexBufferDesc.ByteWidth = sizeof(indices); indexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE; indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; D3D11_SUBRESOURCE_DATA indexSubResourceData; ZeroMemory(&indexSubResourceData, sizeof(indexSubResourceData)); indexSubResourceData.pSysMem = indices; if (FAILED(mGame->device()->CreateBuffer(&indexBufferDesc, &indexSubResourceData, &mIndexBuffer))) { throw GameException(L"ID3D11Device::CreateBuffer() failed"); } mCamera->setPosition(0.0f, 0.0f, 5.0f); }
void CubeDemo::Initialize() { SetCurrentDirectory(Utility::ExecutableDirectory().c_str()); // Compile the shader UINT shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3DCOMPILE_DEBUG; shaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = nullptr; ID3D10Blob* errorMessages = nullptr; HRESULT hr = D3DCompileFromFile(L"C:\\Users\\Nick\\Source\\Repos\\DirectXTest\\source\\Library\\Content\\BasicEffect.fx", nullptr, nullptr, nullptr, "fx_5_0", shaderFlags, 0, &compiledShader, &errorMessages); if (FAILED(hr)) { char* errorMessage = (errorMessages != nullptr ? (char*)errorMessages->GetBufferPointer() : "D3DX11CompileFromFile() failed"); GameException ex(errorMessage, hr); ReleaseObject(errorMessages); throw ex; } // Create an effect object from the compiled shader hr = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, mGame->Direct3DDevice(), &mEffect); if (FAILED(hr)) { throw GameException("D3DX11CreateEffectFromMemory() failed.", hr); } ReleaseObject(compiledShader); // Look up the technique, pass, and WVP variable from the effect mTechnique = mEffect->GetTechniqueByName("main11"); if (mTechnique == nullptr) { throw GameException("ID3DX11Effect::GetTechniqueByName() could not find the specified technique.", hr); } mPass = mTechnique->GetPassByName("p0"); if (mPass == nullptr) { throw GameException("ID3DX11EffectTechnique::GetPassByName() could not find the specified pass.", hr); } ID3DX11EffectVariable* variable = mEffect->GetVariableByName("WorldViewProjection"); if (variable == nullptr) { throw GameException("ID3DX11Effect::GetVariableByName() could not find the specified variable.", hr); } mWvpVariable = variable->AsMatrix(); if (mWvpVariable->IsValid() == false) { throw GameException("Invalid effect variable cast."); } // Create the input layout D3DX11_PASS_DESC passDesc; mPass->GetDesc(&passDesc); D3D11_INPUT_ELEMENT_DESC inputElementDescriptions[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; if (FAILED(hr = mGame->Direct3DDevice()->CreateInputLayout(inputElementDescriptions, ARRAYSIZE(inputElementDescriptions), passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mInputLayout))) { throw GameException("ID3D11Device::CreateInputLayout() failed.", hr); } // Create the vertex and index buffers BasicEffectVertex vertices[] = { BasicEffectVertex(XMFLOAT4(-1.0f, +1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::Green))), BasicEffectVertex(XMFLOAT4(+1.0f, +1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::Yellow))), BasicEffectVertex(XMFLOAT4(+1.0f, +1.0f, +1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::White))), BasicEffectVertex(XMFLOAT4(-1.0f, +1.0f, +1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::BlueGreen))), BasicEffectVertex(XMFLOAT4(-1.0f, -1.0f, +1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::Blue))), BasicEffectVertex(XMFLOAT4(+1.0f, -1.0f, +1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::Purple))), BasicEffectVertex(XMFLOAT4(+1.0f, -1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::Red))), BasicEffectVertex(XMFLOAT4(-1.0f, -1.0f, -1.0f, 1.0f), XMFLOAT4(reinterpret_cast<const float*>(&ColorHelper::Black))) }; D3D11_BUFFER_DESC vertexBufferDesc; ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc)); vertexBufferDesc.ByteWidth = sizeof(BasicEffectVertex) * ARRAYSIZE(vertices); vertexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE; vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; D3D11_SUBRESOURCE_DATA vertexSubResourceData; ZeroMemory(&vertexSubResourceData, sizeof(vertexSubResourceData)); vertexSubResourceData.pSysMem = vertices; if (FAILED(mGame->Direct3DDevice()->CreateBuffer(&vertexBufferDesc, &vertexSubResourceData, &mVertexBuffer))) { throw GameException("ID3D11Device::CreateBuffer() failed."); } UINT indices[] = { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 3, 2, 5, 3, 5, 4, 2, 1, 6, 2, 6, 5, 1, 7, 6, 1, 0, 7, 0, 3, 4, 0, 4, 7 }; D3D11_BUFFER_DESC indexBufferDesc; ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc)); indexBufferDesc.ByteWidth = sizeof(UINT) * ARRAYSIZE(indices); indexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE; indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; D3D11_SUBRESOURCE_DATA indexSubResourceData; ZeroMemory(&indexSubResourceData, sizeof(indexSubResourceData)); indexSubResourceData.pSysMem = indices; if (FAILED(mGame->Direct3DDevice()->CreateBuffer(&indexBufferDesc, &indexSubResourceData, &mIndexBuffer))) { throw GameException("ID3D11Device::CreateBuffer() failed."); } }