void Effect::ExtractPassData(D3DX11_EFFECT_DESC& effectDesc) { for(U32 g = 0; g < effectDesc.Groups; ++g) { ID3DX11EffectGroup* group = dx11Effect->GetGroupByIndex(g); D3DX11_GROUP_DESC groupDesc; DXCall(group->GetDesc(&groupDesc)); for(U32 t = 0; t < groupDesc.Techniques; ++t) { ID3DX11EffectTechnique* pTechnique = group->GetTechniqueByIndex(t); D3DX11_TECHNIQUE_DESC techniqueDesc; DXCall(pTechnique->GetDesc(&techniqueDesc)); for(U32 p = 0; p < techniqueDesc.Passes; ++p) { ID3DX11EffectPass* pass = pTechnique->GetPassByIndex(p); D3DX11_PASS_DESC passDesc; DXCall(pass->GetDesc(&passDesc)); D3DX11_PASS_SHADER_DESC passShaderDesc; DXCall(pass->GetVertexShaderDesc(&passShaderDesc)); D3DX11_EFFECT_SHADER_DESC shaderDesc; DXCall(passShaderDesc.pShaderVariable->GetShaderDesc(passShaderDesc.ShaderIndex, &shaderDesc)); for(U32 i = 0; i < shaderDesc.NumInputSignatureEntries; ++i) { D3D11_SIGNATURE_PARAMETER_DESC signatureDesc; DXCall(passShaderDesc.pShaderVariable->GetInputSignatureElementDesc(passShaderDesc.ShaderIndex, i, &signatureDesc)); } groups[groupDesc.Name].techniques[techniqueDesc.Name].passes[passDesc.Name].data.pass = pass; groups[groupDesc.Name].techniques[techniqueDesc.Name].passes[passDesc.Name].data.inputSignature = passDesc.pIAInputSignature; groups[groupDesc.Name].techniques[techniqueDesc.Name].passes[passDesc.Name].data.inputSignatureSize = passDesc.IAInputSignatureSize; } } } }
void D3D11EffectMaterial::ApplyPass(int index) { ID3DX11EffectPass* pPass = m_pTech->GetPassByIndex(index); pPass->Apply(0, m_pContext); }
void Sky::Draw(ID3D11DeviceContext* dc, const Camera& camera) { // center Sky about eye in world space XMFLOAT3 eyePos = camera.GetPosition(); XMMATRIX T = XMMatrixTranslation(eyePos.x, eyePos.y, eyePos.z); XMMATRIX WVP = XMMatrixMultiply(T, camera.ViewProj()); Effects::SkyFX->SetWorldViewProj(WVP); Effects::SkyFX->SetCubeMap(mCubeMapSRV); UINT stride = sizeof(XMFLOAT3); UINT offset = 0; dc->IASetVertexBuffers(0, 1, &mVB, &stride, &offset); dc->IASetIndexBuffer(mIB, DXGI_FORMAT_R16_UINT, 0); dc->IASetInputLayout(InputLayouts::Pos); dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); D3DX11_TECHNIQUE_DESC techDesc; Effects::SkyFX->SkyTech->GetDesc( &techDesc ); for(UINT p = 0; p < techDesc.Passes; ++p) { ID3DX11EffectPass* pass = Effects::SkyFX->SkyTech->GetPassByIndex(p); pass->Apply(0, dc); dc->DrawIndexed(mIndexCount, 0, 0); } }
void GpuWaves::Disturb(ID3D11DeviceContext* dc, UINT i, UINT j, float magnitude) { D3DX11_TECHNIQUE_DESC techDesc; // The grid element to displace. Effects::WaveSimFX->SetDisturbIndex(i, j); // The magnitude of the displacement. Effects::WaveSimFX->SetDisturbMag(magnitude); // Displace the current solution heights to generate a wave. Effects::WaveSimFX->SetCurrSolOutput(mWavesCurrSolUAV); Effects::WaveSimFX->DisturbWavesTech->GetDesc( &techDesc ); for(UINT p = 0; p < techDesc.Passes; ++p) { ID3DX11EffectPass* pass = Effects::WaveSimFX->DisturbWavesTech->GetPassByIndex(p); pass->Apply(0, dc); // One thread group kicks off one thread, which displaces the height of one // vertex and its neighbors. dc->Dispatch(1, 1, 1); } // Unbind output from compute shader so we can use it as a shader input (a resource cannot be bound // as an output and input). ID3D11UnorderedAccessView* nullUAV[1] = { 0 }; dc->CSSetUnorderedAccessViews( 0, 1, nullUAV, 0 ); }
void GaussianMain::ShowImage(ID3D11DeviceContext* pd3dImmediateContext) { // Output to default render target ID3D11RenderTargetView* rtv_array[1] = {g_pRTV_Default}; pd3dImmediateContext->OMSetRenderTargets(1, rtv_array, g_pDSV_Default); pd3dImmediateContext->RSSetViewports(1, g_VP_Default); ID3DX11EffectTechnique* pTech = g_pFX_Render->GetTechniqueByName("Tech_ShowImage"); ID3DX11EffectPass* pPass = g_bColorBlur ? pTech->GetPassByName("Pass_ShowColorImage") : pTech->GetPassByName("Pass_ShowMonoImage"); if (g_bColorBlur) g_pFX_Render->GetVariableByName("g_texColorInput")->AsShaderResource()->SetResource(g_pSRV_Output); else g_pFX_Render->GetVariableByName("g_texMonoInput")->AsShaderResource()->SetResource(g_pSRV_Output); UINT strides = sizeof(SCREEN_VERTEX); UINT offsets = 0; ID3D11Buffer* pBuffers[1] = {g_pScreenQuadVB}; pd3dImmediateContext->IASetInputLayout(g_pQuadLayout); pd3dImmediateContext->IASetVertexBuffers(0, 1, pBuffers, &strides, &offsets); pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); pPass->Apply(0, pd3dImmediateContext); pd3dImmediateContext->Draw( 4, 0 ); }
void Terrain::Draw(ID3D11DeviceContext* dc, const Camera& cam, DirectionalLight lights[3]) { dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_4_CONTROL_POINT_PATCHLIST); dc->IASetInputLayout(InputLayouts::Terrain); UINT stride = sizeof(Vertex::Terrain); UINT offset = 0; dc->IASetVertexBuffers(0, 1, &mQuadPatchVB, &stride, &offset); dc->IASetIndexBuffer(mQuadPatchIB, DXGI_FORMAT_R16_UINT, 0); XMMATRIX viewProj = cam.ViewProj(); XMMATRIX world = XMLoadFloat4x4(&mWorld); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world); XMMATRIX worldViewProj = world*viewProj; XMMATRIX ShadowTransform = world * XMLoadFloat4x4(&d3d->m_ShadowTransform); XMFLOAT4 worldPlanes[6]; ExtractFrustumPlanes(worldPlanes, viewProj); // Set per frame constants. Effects::TerrainFX->SetViewProj(viewProj); Effects::TerrainFX->SetEyePosW(cam.GetPosition()); Effects::TerrainFX->SetDirLights(lights); Effects::TerrainFX->SetFogColor(Colors::Silver); Effects::TerrainFX->SetFogStart(15.0f); Effects::TerrainFX->SetFogRange(175.0f); Effects::TerrainFX->SetMinDist(20.0f); Effects::TerrainFX->SetMaxDist(400.0f); Effects::TerrainFX->SetMinTess(0.0f); Effects::TerrainFX->SetMaxTess(6.0f); Effects::TerrainFX->SetTexelCellSpaceU(1.0f / mInfo.HeightmapWidth); Effects::TerrainFX->SetTexelCellSpaceV(1.0f / mInfo.HeightmapHeight); Effects::TerrainFX->SetWorldCellSpace(mInfo.CellSpacing); Effects::TerrainFX->SetWorldFrustumPlanes(worldPlanes); Effects::TerrainFX->SetLayerMapArray(mLayerMapArraySRV); Effects::TerrainFX->SetBlendMap(mBlendMapSRV); Effects::TerrainFX->SetHeightMap(mHeightMapSRV); Effects::TerrainFX->SetShadowMap(d3d->GetShadowMap()); Effects::TerrainFX->SetShadowTransform(ShadowTransform); Effects::TerrainFX->SetMaterial(mMat); ID3DX11EffectTechnique* tech = Effects::TerrainFX->Light1Tech; D3DX11_TECHNIQUE_DESC techDesc; tech->GetDesc( &techDesc ); for(UINT i = 0; i < techDesc.Passes; ++i) { ID3DX11EffectPass* pass = tech->GetPassByIndex(i); pass->Apply(0, dc); dc->DrawIndexed(mNumPatchQuadFaces*4, 0, 0); } dc->HSSetShader(0, 0, 0); dc->DSSetShader(0, 0, 0); }
void RenderTerrain(ID3D11DeviceContext* pContext, const D3DXMATRIX& mProj, const D3D11_VIEWPORT& vp, const char* passOverride=NULL) { SetMatrices(GetApp()->ActiveCam_, mProj); g_HwTessellation = false; if (g_HwTessellation) { pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_4_CONTROL_POINT_PATCHLIST); pContext->IASetIndexBuffer(g_TileQuadListIB, DXGI_FORMAT_R32_UINT, 0); } else { pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); pContext->IASetIndexBuffer(g_TileTriStripIB, DXGI_FORMAT_R32_UINT, 0); } const char* passName = "ShadedTriStrip"; const bool wire = true; if (wire) passName = "Wireframe"; if (g_HwTessellation) { if (wire) passName = "HwTessellatedWireframe"; else passName = "HwTessellated"; } if (passOverride != NULL) passName = passOverride; ID3DX11EffectPass* pPass = g_pTesselationTechnique->GetPassByName(passName); if (!pPass) return; // Shouldn't happen unless the FX file is broken (like wrong pass name). SetViewport(pContext, vp); for (int i=0; i!=g_nRings ; ++i) { const TileRing* pRing = g_pTileRings[i]; pRing->SetRenderingState(pContext); g_HeightMapVar->SetResource(g_pHeightMapSRV); g_GradientMapVar->SetResource(g_pGradientMapSRV); g_pTileSizeVar->SetFloat(pRing->tileSize()); // Need to apply the pass after setting its vars. pPass->Apply(0, pContext); // Instancing is used: one tiles is one instance and the index buffer describes all the // NxN patches within one tile. const int nIndices = (g_HwTessellation)? QUAD_LIST_INDEX_COUNT: TRI_STRIP_INDEX_COUNT; pContext->DrawIndexedInstanced(nIndices, pRing->nTiles(), 0, 0, 0); } }
void EffectSystemD3D11::ApplyEffect(int effectID, const char *techName, const char *passName) { ASSERT_EFFECT(effectID); ID3DX11EffectPass *pass = nullptr; pass = mEffects[effectID]->GetTechniqueByName(techName)->GetPassByName(passName); if (pass->IsValid()) { pass->Apply(0, mContext); } }
HRESULT Shader::Apply( unsigned int pass ) { ID3DX11EffectPass* p = this->zTechnique->GetPassByIndex(pass); if (p) { p->Apply(0, this->zDeviceContext); if (this->zInputLayout) { this->zDeviceContext->IASetInputLayout(this->zInputLayout); } return S_OK; } return E_FAIL; }
HRESULT Shader::Apply(unsigned int pass) { ID3DX11EffectPass* p = m_pTechnique->GetPassByIndex(pass); if(p) { p->Apply(0, mDevice3D->GetDeviceContext()); if(m_pInputLayout) { mDevice3D->GetDeviceContext()->IASetInputLayout(m_pInputLayout); } return S_OK; } return E_FAIL; }
VertexDeclaration::VertexDeclaration(D3D11_INPUT_ELEMENT_DESC arrInputDesc [], UINT numElements, ID3DX11Effect* pEffect, std::string strTechniqueName, ID3D11Device* pD3D11Device) { ID3DX11EffectTechnique* pTestEffectTechnique; pTestEffectTechnique = pEffect->GetTechniqueByName( strTechniqueName.c_str()); ID3DX11EffectPass* pTestEffectPass = pTestEffectTechnique->GetPassByIndex( 0 ); D3DX11_PASS_SHADER_DESC passDesc; D3DX11_EFFECT_SHADER_DESC shaderDesc; pTestEffectPass->GetVertexShaderDesc( &passDesc ); passDesc.pShaderVariable->GetShaderDesc( passDesc.ShaderIndex, &shaderDesc ); HRESULT d3dResult = pD3D11Device->CreateInputLayout( arrInputDesc, numElements, shaderDesc.pBytecode, shaderDesc.BytecodeLength, &m_pInputLayout ); if( FAILED( d3dResult ) ) { DXTRACE_MSG( TEXT("Error creating the input layout!") ); return; } }
void RawDataViewer::ShowRawVoxeldata(ID3D11ShaderResourceView* pData, float fIndex) { // Find a slice by an index, and then render it on a full-screen quad. mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); mTargetVar->SetResource(pData); mIndexVar->SetFloat(fIndex); mDeviceContext->IASetInputLayout(mVertexSlipLayout); UINT stride = sizeof(VertexSlip); UINT offset = 0; mDeviceContext->IASetVertexBuffers(0, 1, &mVB, &stride, &offset); ID3DX11EffectPass* pass = mRttTech->GetPassByIndex(0); pass->Apply(0, mDeviceContext); mDeviceContext->Draw(6, 0); }
void DX11SpriteBatch::EndBatch(ID3D11DeviceContext* dc) { assert(mInitialized); u32 viewportCount = 1; D3D11_VIEWPORT vp; dc->RSGetViewports(&viewportCount, &vp); mScreenWidth = vp.Width; mScreenHeight = vp.Height; u32 stride = sizeof(MeshData::SpriteVertex); u32 offset = 0; dc->IASetInputLayout(mInputLayout); dc->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0); dc->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset); dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); DX11Effects::SpriteFX->SetSpriteMap(mBatchTexSRV); ID3DX11EffectPass* pass = DX11Effects::SpriteFX->SpriteTech->GetPassByIndex(0); pass->Apply(0, dc); u32 spritesToDraw = (u32)mSpriteList.size(); u32 startIndex = 0; while( spritesToDraw > 0 ) { if( spritesToDraw <= BatchSize ) { DrawBatch(dc, startIndex, spritesToDraw); spritesToDraw = 0; } else { DrawBatch(dc, startIndex, BatchSize); startIndex += BatchSize; spritesToDraw -= BatchSize; } } RJE_SAFE_RELEASE(mBatchTexSRV); }
void h2GBufferDebugDraw::RenderGBuffer() { m_Renderer->m_D3d11ImmediateContext->ClearDepthStencilView( m_Renderer->m_DepthBufferDSV, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0 ); m_Renderer->m_D3d11ImmediateContext->IASetInputLayout( m_PosTexLayout ); m_Renderer->m_D3d11ImmediateContext->OMSetRenderTargets( 1, &m_Renderer->m_BackbufferRTV, m_Renderer->m_DepthBufferDSV ); ID3DX11EffectShaderResourceVariable * fxTexSamplerVar; fxTexSamplerVar = m_Fx->GetVariableByName( "texSampler" )->AsShaderResource(); fxTexSamplerVar->SetResource( m_Renderer->m_GBuffer->GetBufferByType( m_CurrentBuffer )->m_SRV ); unsigned int stride = sizeof( h2RenderingEngine::vertexPosTex_t ); unsigned int offset = 0; m_Renderer->m_D3d11ImmediateContext->IASetVertexBuffers( 0, 1, &m_Renderer->m_FullScrQuadVB, &stride, &offset ); const h2SceneObject * cameraObj = h2Engine::GetScene()->GetMainCamera(); const h2Camera * camera = static_cast<h2Camera *>( cameraObj->GetComponent( "h2Camera" ) ); h2Matrix44 projMatrix = h2Matrix44::OrthographicD3D( m_Renderer->m_Width, m_Renderer->m_Height, camera->zNear, camera->zFar ); ID3DX11EffectMatrixVariable * fxPVar = m_Fx->GetVariableByName( "orthoProjection" )->AsMatrix(); fxPVar->SetMatrix( projMatrix.GetDataPtr() ); ID3DX11EffectTechnique * tech = nullptr; switch ( m_CurrentBuffer ) { case DEPTH_BUFFER: tech = m_Fx->GetTechniqueByName( "DepthBufferDebugDrawTech" ); break; case NORMAL_BUFFER: tech = m_Fx->GetTechniqueByName( "NormalBufferDebugDrawTech" ); break; } ID3DX11EffectPass * pass = tech->GetPassByIndex( 0 ); pass->Apply( 0, m_Renderer->m_D3d11ImmediateContext ); m_Renderer->m_D3d11ImmediateContext->Draw( 6, 0 ); ID3D11ShaderResourceView * emptySRV = nullptr; m_Renderer->m_D3d11ImmediateContext->PSSetShaderResources(0, 1, &emptySRV); }
void Entity::DrawShadow(ID3DX11EffectTechnique* activeTech, ID3D11DeviceContext* context, const Camera& camera, XMFLOAT4X4 lightView, XMFLOAT4X4 lightProj) { XMMATRIX view = XMLoadFloat4x4(&lightView); XMMATRIX proj = XMLoadFloat4x4(&lightProj); XMMATRIX viewProj = XMMatrixMultiply(view, proj); ID3DX11EffectTechnique* smapTech = Effects::BuildShadowMapFX->BuildShadowMapAlphaClipTech; D3DX11_TECHNIQUE_DESC techDesc; smapTech->GetDesc(&techDesc); for (UINT p = 0; p < techDesc.Passes; ++p) { ID3DX11EffectPass* pass = activeTech->GetPassByIndex(p); XMMATRIX world = XMLoadFloat4x4(&mWorld); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world); XMMATRIX worldViewProj = world*view*proj; Effects::BuildShadowMapFX->SetWorld(world); Effects::BuildShadowMapFX->SetWorldInvTranspose(worldInvTranspose); Effects::BuildShadowMapFX->SetWorldViewProj(worldViewProj); Effects::BuildShadowMapFX->SetDiffuseMap(mTexSRV); if (!useTexTrans){ Effects::BuildShadowMapFX->SetTexTransform(XMMatrixScaling(origTexScale.x, origTexScale.y, origTexScale.z)); } if (mBasicTexTrans) { Effects::BuildShadowMapFX->SetTexTransform(XMMatrixTranslation(texTrans.x, texTrans.y, texTrans.z)*XMMatrixScaling(origTexScale.x, origTexScale.y, origTexScale.z)); } if (mUseAnimation) { XMMATRIX Scale; mAnim->Flipped() ? Scale = XMMatrixScaling(-origTexScale.x, origTexScale.y, origTexScale.z) : Scale = XMMatrixScaling(origTexScale.x, origTexScale.y, origTexScale.z); Effects::BuildShadowMapFX->SetTexTransform(XMMatrixTranslation(mAnim->GetX(), mAnim->GetY(), texTrans.z)*Scale); } pass->Apply(0, context); context->DrawIndexed(mIndexCount, mIndexOffset, mVertexOffset); } }
bool D3D11EffectMaterial::SetVertexFormat(const VertexFormat& format) { if(m_pIL != NULL) { m_pIL->Release(); } int nElem = format.GetElementCount(); D3D11_INPUT_ELEMENT_DESC* layout = new D3D11_INPUT_ELEMENT_DESC[nElem]; for(int i = 0; i < nElem; ++i) { const VertexElement& e = format.GetElement(i); switch(e.semantic) { case VertexElement::POSITION: layout[i].SemanticName = "POSITION"; break; case VertexElement::NORMAL: layout[i].SemanticName = "NORMAL"; break; case VertexElement::COLOR: layout[i].SemanticName = "COLOR"; break; case VertexElement::POSITION_T: layout[i].SemanticName = "POSITIONT"; break; case VertexElement::TEXCOORD: layout[i].SemanticName = "TEXCOORD"; break; default: break; } layout[i].Format = D3D11Format::Convert(e.type); layout[i].SemanticIndex = e.element_slot; layout[i].InputSlot = 0; layout[i].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT ; layout[i].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; layout[i].InstanceDataStepRate = 0; } ID3DX11EffectTechnique* pTech = m_pEffect->GetTechniqueByIndex(0); ID3DX11EffectPass* pPass = pTech->GetPassByIndex(0); D3DX11_PASS_DESC pass; ZeroMemory(&pass, sizeof(pass)); HRESULT hr = pPass->GetDesc(&pass); hr = m_pDevice->CreateInputLayout( layout, nElem, pass.pIAInputSignature, pass.IAInputSignatureSize, &m_pIL ); if(FAILED(hr)) { delete[] layout; return false; } delete[] layout; return true; }
void GpuWaves::Update(ID3D11DeviceContext* dc, float dt) { static float t = 0; // Accumulate time. t += dt; // Only update the simulation at the specified time step. if( t >= mTimeStep ) { D3DX11_TECHNIQUE_DESC techDesc; Effects::WaveSimFX->SetWaveConstants(mK); Effects::WaveSimFX->SetPrevSolInput(mWavesPrevSolSRV); Effects::WaveSimFX->SetCurrSolInput(mWavesCurrSolSRV); Effects::WaveSimFX->SetNextSolOutput(mWavesNextSolUAV); Effects::WaveSimFX->UpdateWavesTech->GetDesc( &techDesc ); for(UINT p = 0; p < techDesc.Passes; ++p) { ID3DX11EffectPass* pass = Effects::WaveSimFX->UpdateWavesTech->GetPassByIndex(p); pass->Apply(0, dc); // How many groups do we need to dispatch to cover the wave grid. // Note that mNumRows and mNumCols should be divisible by 16 // so there is no remainder. UINT numGroupsX = mNumCols / 16; UINT numGroupsY = mNumRows / 16; dc->Dispatch(numGroupsX, numGroupsY, 1); } // Unbind the input textures from the CS for good housekeeping. ID3D11ShaderResourceView* nullSRV[1] = { 0 }; dc->CSSetShaderResources( 0, 1, nullSRV ); // Unbind output from compute shader (we are going to use this output as an input in the next pass, // and a resource cannot be both an output and input at the same time. ID3D11UnorderedAccessView* nullUAV[1] = { 0 }; dc->CSSetUnorderedAccessViews( 0, 1, nullUAV, 0 ); // Disable compute shader. dc->CSSetShader(0, 0, 0); // // Ping-pong buffers in preparation for the next update. // The previous solution is no longer needed and becomes the target of the next solution in the next update. // The current solution becomes the previous solution. // The next solution becomes the current solution. // ID3D11ShaderResourceView* srvTemp = mWavesPrevSolSRV; mWavesPrevSolSRV = mWavesCurrSolSRV; mWavesCurrSolSRV = mWavesNextSolSRV; mWavesNextSolSRV = srvTemp; ID3D11UnorderedAccessView* uavTemp = mWavesPrevSolUAV; mWavesPrevSolUAV = mWavesCurrSolUAV; mWavesCurrSolUAV = mWavesNextSolUAV; mWavesNextSolUAV = uavTemp; t = 0.0f; // reset time } }
void EndOfDirectX11::Render() { if( d3dContext_ == 0 ) { return; } float clearColor[] = { 0.0f, 0.0f, 0.25f, 1.0f }; d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor ); d3dContext_->ClearDepthStencilView( depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0 ); unsigned int stride = sizeof( VertexPos ); unsigned int offset = 0; d3dContext_->IASetInputLayout( inputLayout_ ); d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset ); d3dContext_->IASetIndexBuffer( indexBuffer_, DXGI_FORMAT_R16_UINT, 0 ); d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); ID3DX11EffectShaderResourceVariable * colorMap = 0; colorMap = effect_->GetVariableByName( "colorMap" )->AsShaderResource(); colorMap->SetResource( colorMap_ ); ID3DX11EffectShaderResourceVariable* colorMap2; colorMap2 = effect_->GetVariableByName( "secondMap" )->AsShaderResource( ); colorMap2->SetResource( secondMap_ ); ID3DX11EffectSamplerVariable * samplerState = 0; samplerState = effect_->GetVariableByName( "colorSampler" )->AsSampler(); samplerState->SetSampler( 0, samplerState_ ); ID3DX11EffectMatrixVariable * worldMatrix = 0; worldMatrix = effect_->GetVariableByName( "worldMatrix" )->AsMatrix(); worldMatrix->SetMatrix( ( float * )&worldMat_ ); ID3DX11EffectMatrixVariable * viewMatrix = 0; viewMatrix = effect_->GetVariableByName( "viewMatrix" )->AsMatrix(); viewMatrix->SetMatrix( ( float * )&viewMatrix_ ); ID3DX11EffectMatrixVariable * projMatrix = 0; projMatrix = effect_->GetVariableByName( "projMatrix" )->AsMatrix(); projMatrix->SetMatrix( ( float * )&projMatrix_ ); ID3DX11EffectTechnique * colorInvTechnique = 0; colorInvTechnique = effect_->GetTechniqueByName( "MultiTexture" ); D3DX11_TECHNIQUE_DESC techDesc; colorInvTechnique->GetDesc( &techDesc ); for( unsigned int p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass * pass = colorInvTechnique->GetPassByIndex( p ); if( pass != 0 ) { pass->Apply( 0, d3dContext_ ); d3dContext_->DrawIndexed( 36, 0, 0 ); } } swapChain_->Present( 0, 0 ); }
bool EndOfDirectX11::LoadContent() { HRESULT result = 0; ID3DBlob* buffer = 0; ID3DBlob* vsBuffer = 0; ID3DBlob* psBuffer = 0; bool compileResult = CompileD3DShader( "MultiTexture.fx", 0, "fx_5_0", &buffer ); if( compileResult == false ) { DXTRACE_MSG( "Error compiling the effect shader!" ); return false; } result = D3DX11CreateEffectFromMemory( buffer->GetBufferPointer( ), buffer->GetBufferSize( ), 0, d3dDevice_, &effect_ ); if( FAILED(result) ) { DXTRACE_MSG( "error creating the effect shader" ); if( buffer ) { buffer->Release(); } return false; } buffer->Release(); D3D11_INPUT_ELEMENT_DESC solidColorLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout ); ID3DX11EffectTechnique * colorInvTechnique = 0; colorInvTechnique = effect_->GetTechniqueByName( "MultiTexture" ); ID3DX11EffectPass * effectPass = colorInvTechnique->GetPassByIndex( 0 ); D3DX11_PASS_SHADER_DESC passDesc; D3DX11_EFFECT_SHADER_DESC shaderDesc; effectPass->GetVertexShaderDesc( &passDesc ); passDesc.pShaderVariable->GetShaderDesc( passDesc.ShaderIndex, &shaderDesc ); result = d3dDevice_->CreateInputLayout( solidColorLayout, totalLayoutElements, shaderDesc.pBytecode, shaderDesc.BytecodeLength, &inputLayout_ ); if( FAILED( result ) ) { DXTRACE_MSG( "Error creating the input layout" ); return false; } VertexPos vertices[] = { { XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, { XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, { XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, { XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, { XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, { XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, { XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, { XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, { XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, { XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, { XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, { XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, { XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, { XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, { XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, { XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, { XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, { XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, { XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, { XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, { XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, { XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, { XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, { XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, }; D3D11_BUFFER_DESC vertexDesc; ZeroMemory( &vertexDesc, sizeof( vertexDesc ) ); vertexDesc.Usage = D3D11_USAGE_DEFAULT; vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexDesc.ByteWidth = sizeof( VertexPos ) * 24; D3D11_SUBRESOURCE_DATA resourceData; ZeroMemory( &resourceData, sizeof( resourceData ) ); resourceData.pSysMem = vertices; result = d3dDevice_->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer_ ); if( FAILED( result ) ) { DXTRACE_MSG( "Failed to create vertex buffer!" ); return false; } WORD indices[] = { 3, 1, 0, 2, 1, 3, 6, 4, 5, 7, 4, 6, 11, 9, 8, 10, 9, 11, 14, 12, 13, 15, 12, 14, 19, 17, 16, 18, 17, 19, 22, 20, 21, 23, 20, 22 }; D3D11_BUFFER_DESC indexDesc; ZeroMemory( &indexDesc, sizeof( indexDesc ) ); indexDesc.Usage = D3D11_USAGE_DEFAULT; indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; indexDesc.ByteWidth = sizeof( WORD ) * 36; indexDesc.CPUAccessFlags = 0; resourceData.pSysMem = indices; result = d3dDevice_->CreateBuffer( &indexDesc, &resourceData, &indexBuffer_ ); if( FAILED( result ) ) { DXTRACE_MSG( "Error in creating index buffer" ); return false; } result = D3DX11CreateShaderResourceViewFromFile( d3dDevice_, "decal.dds", 0, 0, &colorMap_, 0 ); if( FAILED( result ) ) { DXTRACE_MSG( "Error in creating ShaderResource by decal.dds" ); return false; } result = D3DX11CreateShaderResourceViewFromFile( d3dDevice_, "decal2.dds", 0, 0, &secondMap_, 0 ); if( FAILED( result ) ) { DXTRACE_MSG( "Error in creating shader resource by decal2.dds" ); return false; } D3D11_SAMPLER_DESC colorMapDesc; ZeroMemory( &colorMapDesc, sizeof( colorMapDesc ) ); colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX; result = d3dDevice_->CreateSamplerState( &colorMapDesc, &samplerState_ ); if( FAILED(result) ) { return false; } D3D11_BUFFER_DESC constDesc; ZeroMemory( &constDesc, sizeof( constDesc ) ); constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constDesc.ByteWidth = sizeof( XMMATRIX ); constDesc.Usage = D3D11_USAGE_DEFAULT; result = d3dDevice_->CreateBuffer( &constDesc, 0, &worldCB_ ); if( FAILED( result ) ) { DXTRACE_MSG( "Error in creating world constant buffer" ); return false; } result = d3dDevice_->CreateBuffer( &constDesc, 0, &viewCB_ ); if( FAILED( result ) ) { DXTRACE_MSG( "Error in creating view constant buffer" ); return false; } result = d3dDevice_->CreateBuffer( &constDesc, 0, &projCB_ ); if( FAILED( result ) ) { DXTRACE_MSG( "Error in creating project constant buffer" ); return false; } viewMatrix_ = XMMatrixIdentity(); projMatrix_ = XMMatrixPerspectiveFovLH( XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f ); XMMATRIX rotationMat = XMMatrixRotationY( XM_PIDIV4 * 0.5 ); XMMATRIX translationMat = XMMatrixTranslation( 0.0f, 0.0f, 6.0f ); worldMat_ = rotationMat * translationMat; return true; }
bool ColorInversionDemo::LoadContent() { ID3DBlob * buffer = 0; //ColorInversionDemo //bool compileResult = CompileD3DShader( "ColorInversion.fx" , 0 , "fx_5_0" , &buffer ); //ColorShiftingDemo bool compileResult = CompileD3DShader( "ColorShift.fx" , 0 , "fx_5_0" , &buffer ); if( compileResult == false ) { DXTRACE_MSG( "±àÒëeffect shader ʧ°Ü£¡" ); return false; } HRESULT d3dResult ; d3dResult = D3DX11CreateEffectFromMemory( buffer ->GetBufferPointer() , buffer ->GetBufferSize() , 0 , d3dDevice_ , &effect_ ); if( FAILED( d3dResult )) { DXTRACE_MSG( "´´½¨effect shader ʧ°Ü£¡" ); if( buffer ) { buffer ->Release(); } return false; } D3D11_INPUT_ELEMENT_DESC solidColorLayout[ ] = { { "POSITION" , 0 , DXGI_FORMAT_R32G32B32_FLOAT , 0 , 0 , D3D11_INPUT_PER_VERTEX_DATA , 0 } , { "TEXTURE" , 0 , DXGI_FORMAT_R32G32_FLOAT , 0 , 12 , D3D11_INPUT_PER_VERTEX_DATA , 0 } }; unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout ); ID3DX11EffectTechnique * colorInvTechnique; /*** //ColorInversionDemo colorInvTechnique = effect_ ->GetTechniqueByName( "ColorInversion" ); ID3DX11EffectPass * effectPass = colorInvTechnique ->GetPassByIndex( 0 ); */ colorInvTechnique = effect_ ->GetTechniqueByName( "ColorShift" ); ID3DX11EffectPass * effectPass = colorInvTechnique ->GetPassByIndex( 0 ); D3DX11_PASS_SHADER_DESC passDesc; D3DX11_EFFECT_SHADER_DESC shaderDesc; effectPass->GetVertexShaderDesc( &passDesc ); passDesc.pShaderVariable ->GetShaderDesc( passDesc.ShaderIndex , &shaderDesc ); d3dResult = d3dDevice_ ->CreateInputLayout( solidColorLayout , totalLayoutElements , shaderDesc.pBytecode , shaderDesc.BytecodeLength , &inputLayout_ ); buffer ->Release(); if( FAILED( d3dResult )) { DXTRACE_MSG( "´´½¨ÊäÈë²ãʧ°Ü£¡" ); return false; } VertexPos vertices[ ] = { { XMFLOAT3( -1.0f , 1.0f , -1.0f ) , XMFLOAT2( 0.0f , 0.0f ) } , { XMFLOAT3( 1.0f , 1.0f , -1.0f ) , XMFLOAT2( 1.0f , 0.0f ) } , { XMFLOAT3( 1.0f , 1.0f , 1.0f ) , XMFLOAT2( 1.0f , 1.0f ) } , { XMFLOAT3( -1.0f , 1.0f , 1.0f ) , XMFLOAT2( 0.0f , 1.0f ) } , { XMFLOAT3( -1.0f , -1.0f , -1.0f ) , XMFLOAT2( 0.0f , 0.0f ) } , { XMFLOAT3( 1.0f , -1.0f , -1.0f ) , XMFLOAT2( 1.0f , 0.0f ) } , { XMFLOAT3( 1.0f , -1.0f , 1.0f ) , XMFLOAT2( 1.0f , 1.0f ) } , { XMFLOAT3( -1.0f , -1.0f , 1.0f ) , XMFLOAT2( 0.0f , 1.0f ) } , { XMFLOAT3( -1.0f , -1.0f , 1.0f ) , XMFLOAT2( 0.0f , 0.0f ) } , { XMFLOAT3( -1.0f , -1.0f , -1.0f ) , XMFLOAT2( 1.0f , 0.0f ) } , { XMFLOAT3( -1.0f , 1.0f , -1.0f ) , XMFLOAT2( 1.0f , 1.0f ) } , { XMFLOAT3( -1.0f , 1.0f , 1.0f ) , XMFLOAT2( 0.0f , 1.0f ) } , { XMFLOAT3( 1.0f , -1.0f , 1.0f ) , XMFLOAT2( 0.0f , 0.0f ) } , { XMFLOAT3( 1.0f , -1.0f , -1.0f ) , XMFLOAT2( 1.0f , 0.0f ) } , { XMFLOAT3( 1.0f , 1.0f , -1.0f ) , XMFLOAT2( 1.0f , 1.0f ) } , { XMFLOAT3( 1.0f , 1.0f , 1.0f ) , XMFLOAT2( 0.0f , 1.0f ) } , { XMFLOAT3( -1.0f , -1.0f , -1.0f ) , XMFLOAT2( 0.0f , 0.0f ) } , { XMFLOAT3( 1.0f , -1.0f , -1.0f ) , XMFLOAT2( 1.0f , 0.0f ) } , { XMFLOAT3( 1.0f , 1.0f , -1.0f ) , XMFLOAT2( 1.0f , 1.0f ) } , { XMFLOAT3( -1.0f , 1.0f , -1.0f ) , XMFLOAT2( 0.0f , 1.0f ) } , { XMFLOAT3( -1.0f , -1.0f , 1.0f ) , XMFLOAT2( 0.0f , 0.0f ) } , { XMFLOAT3( 1.0f , -1.0f , 1.0f ) , XMFLOAT2( 1.0f , 0.0f ) } , { XMFLOAT3( 1.0f , 1.0f , 1.0f ) , XMFLOAT2( 1.0f , 1.0f ) } , { XMFLOAT3( -1.0f , 1.0f , 1.0f ) , XMFLOAT2( 0.0f , 1.0f ) } }; D3D11_BUFFER_DESC vertexDesc; ZeroMemory( &vertexDesc , sizeof( vertexDesc )); vertexDesc.Usage = D3D11_USAGE_DEFAULT; vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexDesc.ByteWidth = sizeof( VertexPos ) * 24; D3D11_SUBRESOURCE_DATA resourceData; ZeroMemory( &resourceData , sizeof( resourceData )); resourceData.pSysMem = vertices; d3dResult = d3dDevice_ ->CreateBuffer( &vertexDesc , &resourceData , &vertexBuffer_ ); if( FAILED( d3dResult )) { DXTRACE_MSG( "´´½¨¶¥µã»º³åÇøʧ°Ü£¡" ); return false; } WORD indices[ ] = { 3 , 1 , 0 , 2 , 1 , 3 , 6 , 4 , 5 , 7 , 4 , 6 , 11 , 9 , 8 , 10 , 9 , 11 , 14 , 12 , 13 , 15 , 12 , 14 , 19 , 17 , 16 , 18 , 17 , 19 , 22 , 20 , 21 ,23 , 20 , 22 }; D3D11_BUFFER_DESC indexDesc; ZeroMemory( &indexDesc , sizeof( indexDesc )); indexDesc.Usage = D3D11_USAGE_DEFAULT ; indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; indexDesc.ByteWidth = sizeof( WORD ) * 36 ; indexDesc.CPUAccessFlags = 0; resourceData.pSysMem = indices; d3dResult =d3dDevice_ ->CreateBuffer( &indexDesc , &resourceData , &indexBuffer_ ); if( FAILED( d3dResult )) { DXTRACE_MSG( "´´½¨Ë÷Òý»º³åÇøʧ°Ü£¡" ); return false; } d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_ , "decal.dds" , 0 , 0 , &colorMap_ , 0 ); if( FAILED( d3dResult )) { DXTRACE_MSG( "¼ÓÔØÎÆÀíͼÏñʧ°Ü£¡" ); return false; } D3D11_SAMPLER_DESC colorMapDesc; ZeroMemory( &colorMapDesc , sizeof( colorMapDesc )); colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX; d3dResult = d3dDevice_ ->CreateSamplerState( &colorMapDesc ,&colorMapSampler_ ); if( FAILED( d3dResult )) { DXTRACE_MSG( "´´½¨²ÉÑùÆ÷ʧ°Ü£¡" ); return false; } viewMatrix_ = XMMatrixIdentity(); projMatrix_ = XMMatrixPerspectiveFovLH( XM_PIDIV4 , 800.0f / 600.0f , 0.01f , 100.0f ); return true; }
void App::drawScene( void ) { mD3DImmediateContext->ClearRenderTargetView( mRenderTargetView, reinterpret_cast<const float*>( &Colors::Black ) ); mD3DImmediateContext->ClearDepthStencilView( mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.f, 0 ); mD3DImmediateContext->IASetInputLayout( InputLayouts::Basic32 ); mD3DImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); float blendFactor [] = { 0.f, 0.f, 0.f, 0.f }; const UINT stride = sizeof( Vertex::Basic32 ); const UINT offset = 0; XMMATRIX view = XMLoadFloat4x4( &mView ); XMMATRIX proj = XMLoadFloat4x4( &mProj ); const XMMATRIX viewProj = view * proj; // Set per-frame constants. Effects::BasicFX->SetDirLights( mDirLights ); Effects::BasicFX->SetEyePosW( mEyePosW ); Effects::BasicFX->SetFogColor( Colors::Black ); Effects::BasicFX->SetFogStart( 2.f ); Effects::BasicFX->SetFogRange( 40.f ); ID3DX11EffectTechnique* tech = Effects::BasicFX->Light0TexTech; ID3DX11EffectTechnique* activeSkullTech = Effects::BasicFX->Light0TexTech; switch ( mRenderOptions ) { case RenderOptions::Lighting: tech = Effects::BasicFX->Light3Tech; activeSkullTech = Effects::BasicFX->Light3Tech; break; case RenderOptions::Textures: tech = Effects::BasicFX->Light3TexTech; activeSkullTech = Effects::BasicFX->Light3Tech; break; case RenderOptions::TexturesAndFog: tech = Effects::BasicFX->Light3TexFogTech; activeSkullTech = Effects::BasicFX->Light3FogTech; break; } D3DX11_TECHNIQUE_DESC techDesc; // // Draw floor and walls to back buffer as normal. tech->GetDesc( &techDesc ); for ( UINT p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass* pass = tech->GetPassByIndex( p ); mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mRoomVB, &stride, &offset ); // ??? // Set per-object constants. XMMATRIX world = XMLoadFloat4x4( &mRoomWorld ); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world ); XMMATRIX worldViewProj = world * viewProj; Effects::BasicFX->SetWorld( world ); Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose ); Effects::BasicFX->SetWorldViewProj( worldViewProj ); Effects::BasicFX->SetTexTransform( XMMatrixIdentity() ); Effects::BasicFX->SetMaterial( mRoomMat ); // Floor. Effects::BasicFX->SetDiffuseMap( mFloorDiffuseMapSRV ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->Draw( 6, 0 ); // Wall. Effects::BasicFX->SetDiffuseMap( mWallDiffuseMapSRV ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->Draw( 18, 6 ); } // // Draw the skull to the back buffer as normal. activeSkullTech->GetDesc( &techDesc ); for ( UINT p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass* pass = activeSkullTech->GetPassByIndex( p ); mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mSkullVB, &stride, &offset ); mD3DImmediateContext->IASetIndexBuffer( mSkullIB, DXGI_FORMAT_R32_UINT, 0 ); XMMATRIX world = XMLoadFloat4x4( &mSkullWorld ); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world ); XMMATRIX worldViewProj = world*view*proj; Effects::BasicFX->SetWorld( world ); Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose ); Effects::BasicFX->SetWorldViewProj( worldViewProj ); Effects::BasicFX->SetMaterial( mSkullMat ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->DrawIndexed( mSkullIndexCount, 0, 0 ); } // // Draw the mirror into the stencil buffer only. tech->GetDesc( &techDesc ); for ( UINT p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass* pass = tech->GetPassByIndex( 0 ); mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mRoomVB, &stride, &offset ); // Set per object constants. XMMATRIX world = XMLoadFloat4x4( &mRoomWorld ); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world ); XMMATRIX worldViewProj = world*view*proj; Effects::BasicFX->SetWorld( world ); Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose ); Effects::BasicFX->SetWorldViewProj( worldViewProj ); Effects::BasicFX->SetTexTransform( XMMatrixIdentity() ); // Do not write to render target. mD3DImmediateContext->OMSetBlendState( RenderStates::NoRenderTargetWritesBS, blendFactor, 0xffffffff ); // Render visible mirror pixels to stencil buffer. // Do not write mirror depth to depth buffer to avoid occluding the reflection. mD3DImmediateContext->OMSetDepthStencilState( RenderStates::MarkMirrorDSS, 1 ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->Draw( 6, 24 ); // Restore states. mD3DImmediateContext->OMSetBlendState( nullptr, blendFactor, 0xffffffff ); mD3DImmediateContext->OMSetDepthStencilState( nullptr, 0 ); } // // Draw skull reflection to back buffer. activeSkullTech->GetDesc( &techDesc ); for ( UINT p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass* pass = activeSkullTech->GetPassByIndex( p ); mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mSkullVB, &stride, &offset ); mD3DImmediateContext->IASetIndexBuffer( mSkullIB, DXGI_FORMAT_R32_UINT, 0 ); XMVECTOR mirrorPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f ); // xy plane XMMATRIX R = XMMatrixReflect( mirrorPlane ); //XMMATRIX world = XMMatrixRotationY( MathHelper::Pi ) * XMLoadFloat4x4( &mSkullWorld ) * R; XMMATRIX world = XMLoadFloat4x4( &mSkullWorld ) * R; XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world ); XMMATRIX worldViewProj = world*view*proj; Effects::BasicFX->SetWorld( world ); Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose ); Effects::BasicFX->SetWorldViewProj( worldViewProj ); Effects::BasicFX->SetMaterial( mSkullMat ); // Cache old light directions, then reflect them. XMFLOAT3 oldLightDirs[3]; for ( int i = 0; i < 3; ++i ) { oldLightDirs[i] = mDirLights[i].direction; XMVECTOR dir = XMLoadFloat3( &mDirLights[i].direction ); XMVECTOR reflection = XMVector3TransformNormal( dir, R ); XMStoreFloat3( &mDirLights[i].direction, reflection ); } Effects::BasicFX->SetDirLights( mDirLights ); // Cull clockwise triangles for reflection. mD3DImmediateContext->RSSetState( RenderStates::CullClockwiseRS ); // Only draw reflection into visible mirror pixels as marked by stencil buffer. mD3DImmediateContext->OMSetDepthStencilState( RenderStates::DrawReflectionDSS, 1 ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->DrawIndexed( mSkullIndexCount, 0, 0 ); // Restore default states. mD3DImmediateContext->RSSetState( nullptr ); mD3DImmediateContext->OMSetDepthStencilState( nullptr, 0 ); // Restore light directions. for ( int i = 0; i < 3; ++i ) { mDirLights[i].direction = oldLightDirs[i]; } Effects::BasicFX->SetDirLights( mDirLights ); } // Draw mirror to back buffer. tech->GetDesc( &techDesc ); for ( UINT p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass* pass = tech->GetPassByIndex( p ); mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mRoomVB, &stride, &offset ); // Set per object constants. XMMATRIX world = XMLoadFloat4x4( &mRoomWorld ); XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world ); XMMATRIX worldViewProj = world*view*proj; Effects::BasicFX->SetWorld( world ); Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose ); Effects::BasicFX->SetWorldViewProj( worldViewProj ); Effects::BasicFX->SetTexTransform( XMMatrixIdentity() ); Effects::BasicFX->SetMaterial( mMirrorMat ); Effects::BasicFX->SetDiffuseMap( mMirrorDiffuseMapSRV ); // Mirror mD3DImmediateContext->OMSetBlendState( RenderStates::TransparentBS, blendFactor, 0xffffffff ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->Draw( 6, 24 ); } // Draw skull shadow. activeSkullTech->GetDesc( &techDesc ); for ( UINT p = 0; p < techDesc.Passes; ++p ) { ID3DX11EffectPass* pass = activeSkullTech->GetPassByIndex( p ); mD3DImmediateContext->IASetVertexBuffers( 0, 1, &mSkullVB, &stride, &offset ); mD3DImmediateContext->IASetIndexBuffer( mSkullIB, DXGI_FORMAT_R32_UINT, 0 ); XMVECTOR shadowPlane = XMVectorSet( 0.f, 1.f, 0.f, 0.f ); XMVECTOR toMainLight = -XMLoadFloat3( &mDirLights[0].direction ); XMMATRIX S = XMMatrixShadow( shadowPlane, toMainLight ); XMMATRIX shadowOffsetY = XMMatrixTranslation( 0.f, 0.001f, 0.f ); // Set per-object constants. XMMATRIX world = XMLoadFloat4x4( &mSkullWorld ) * S * shadowOffsetY; XMMATRIX worldInvTranspose = MathHelper::InverseTranspose( world ); XMMATRIX worldViewProj = world*view*proj; Effects::BasicFX->SetWorld( world ); Effects::BasicFX->SetWorldInvTranspose( worldInvTranspose ); Effects::BasicFX->SetWorldViewProj( worldViewProj ); Effects::BasicFX->SetMaterial( mShadowMat ); mD3DImmediateContext->OMSetDepthStencilState( RenderStates::NoDoubleBlendDSS, 0 ); pass->Apply( 0, mD3DImmediateContext ); mD3DImmediateContext->DrawIndexed( mSkullIndexCount, 0, 0 ); // Restore default states. mD3DImmediateContext->OMSetBlendState( 0, blendFactor, 0xffffffff ); mD3DImmediateContext->OMSetDepthStencilState( 0, 0 ); } HR( mSwapChain->Present( 0, 0 ) ); }
void GaussianMain::ApplyGaussianFilter(ID3D11DeviceContext* pd3dImmediateContext) { D3D11_TEXTURE2D_DESC tex_desc; g_pTex_Scene->GetDesc(&tex_desc); float box_width = CalculateBoxFilterWidth(g_FilterRadius, g_NumApproxPasses); float half_box_width = box_width * 0.5f; float frac_half_box_width = (half_box_width + 0.5f) - (int)(half_box_width + 0.5f); float inv_frac_half_box_width = 1.0f - frac_half_box_width; float rcp_box_width = 1.0f / box_width; // Step 1. Vertical passes: Each thread group handles a colomn in the image ID3DX11EffectTechnique* pTech = g_pFX_GaussianCol->GetTechniqueByName("Tech_GaussianFilter"); // Input texture g_pFX_GaussianCol->GetVariableByName("g_texInput")->AsShaderResource()->SetResource(g_pSRV_Scene); // Output texture g_pFX_GaussianCol->GetVariableByName("g_rwtOutput")->AsUnorderedAccessView()->SetUnorderedAccessView(g_pUAV_Output); g_pFX_GaussianCol->GetVariableByName("g_NumApproxPasses")->AsScalar()->SetInt(g_NumApproxPasses - 1); g_pFX_GaussianCol->GetVariableByName("g_HalfBoxFilterWidth")->AsScalar()->SetFloat(half_box_width); g_pFX_GaussianCol->GetVariableByName("g_FracHalfBoxFilterWidth")->AsScalar()->SetFloat(frac_half_box_width); g_pFX_GaussianCol->GetVariableByName("g_InvFracHalfBoxFilterWidth")->AsScalar()->SetFloat(inv_frac_half_box_width); g_pFX_GaussianCol->GetVariableByName("g_RcpBoxFilterWidth")->AsScalar()->SetFloat(rcp_box_width); // Select pass ID3DX11EffectPass* pPass = g_bColorBlur ? pTech->GetPassByName("Pass_GaussianColor") : pTech->GetPassByName("Pass_GaussianMono"); pPass->Apply(0, pd3dImmediateContext); pd3dImmediateContext->Dispatch(tex_desc.Width, 1, 1); // Unbound CS resource and output ID3D11ShaderResourceView* srv_array[] = {NULL, NULL, NULL, NULL}; pd3dImmediateContext->CSSetShaderResources(0, 4, srv_array); ID3D11UnorderedAccessView* uav_array[] = {NULL, NULL, NULL, NULL}; pd3dImmediateContext->CSSetUnorderedAccessViews(0, 4, uav_array, NULL); // Step 2. Horizontal passes: Each thread group handles a row in the image pTech = g_pFX_GaussianRow->GetTechniqueByName("Tech_GaussianFilter"); // Input texture g_pFX_GaussianRow->GetVariableByName("g_texInput")->AsShaderResource()->SetResource(g_pSRV_Scene); // Output texture g_pFX_GaussianRow->GetVariableByName("g_rwtOutput")->AsUnorderedAccessView()->SetUnorderedAccessView(g_pUAV_Output); g_pFX_GaussianRow->GetVariableByName("g_NumApproxPasses")->AsScalar()->SetInt(g_NumApproxPasses - 1); g_pFX_GaussianRow->GetVariableByName("g_HalfBoxFilterWidth")->AsScalar()->SetFloat(half_box_width); g_pFX_GaussianRow->GetVariableByName("g_FracHalfBoxFilterWidth")->AsScalar()->SetFloat(frac_half_box_width); g_pFX_GaussianRow->GetVariableByName("g_InvFracHalfBoxFilterWidth")->AsScalar()->SetFloat(inv_frac_half_box_width); g_pFX_GaussianRow->GetVariableByName("g_RcpBoxFilterWidth")->AsScalar()->SetFloat(rcp_box_width); // Select pass pPass = g_bColorBlur ? pTech->GetPassByName("Pass_GaussianColor") : pTech->GetPassByName("Pass_GaussianMono"); pPass->Apply(0, pd3dImmediateContext); pd3dImmediateContext->Dispatch(tex_desc.Height, 1, 1); // Unbound CS resource and output pd3dImmediateContext->CSSetShaderResources(0, 4, srv_array); pd3dImmediateContext->CSSetUnorderedAccessViews(0, 4, uav_array, NULL); }