void FBXObject::SetupDrawConstantBuffer() { static std::vector<XMFLOAT4X4> mBoneTransforms(128); for(std::size_t i = 0; i < this->AnimController.Mesh.second->mNumBones; ++i) { XMMATRIX invBind = XMLoadFloat4x4(&this->AnimController.Mesh.second->mSkeleton[i].invBindPose); XMMATRIX currPose = XMLoadFloat4x4(&this->AnimController.CurrentGlobalPose[i]); XMMATRIX total = invBind * currPose; XMMATRIX invTotal = XMMatrixTranspose( total); XMFLOAT4X4 inv; XMStoreFloat4x4(&inv, invTotal); mBoneTransforms[i] = inv; } cBuffer::cbChangeEveryFrame cb; XMFLOAT4X4 world = this->object.CalculateMatrix(); cb.mWorld = XMLoadFloat4x4(&world); cb.colour.diffuse = this->object.Colour.Diffuse; cb.colour.ambient = this->object.Colour.Ambient; cb.colour.spec = this->object.Colour.Spec; ID3D11DeviceContext* pImmediateContext = DX11App::getInstance()->direct3d.pImmediateContext; pImmediateContext->UpdateSubresource( this->pCBChangesEveryFrame.second, 0, NULL, &cb, 0, 0 ); pImmediateContext->VSSetConstantBuffers( 2, 1, &(this->pCBChangesEveryFrame.second) ); pImmediateContext->PSSetConstantBuffers( 2, 1, &(this->pCBChangesEveryFrame.second) ); pImmediateContext->UpdateSubresource( this->pAnimBonesBuffer.second, 0, NULL, &(mBoneTransforms.front()), 0, 0 ); pImmediateContext->VSSetConstantBuffers( 3, 1, &(this->pAnimBonesBuffer.second) ); }
void ParticleManager::RenderParticles(DxGraphics* pDxGraphics, Camera& cam) { cam.CalculateViewMatrix(); XMMATRIX tWorld = XMMatrixTranspose(m_worldMat); XMMATRIX tView = XMMatrixTranspose(cam.GetViewMatrix()); XMMATRIX tProj = XMMatrixTranspose(cam.GetProjMatrix()); ID3D11DeviceContext* pCon = pDxGraphics->GetImmediateContext(); UINT stride = sizeof(Particle); UINT offset = 0; pCon->IASetVertexBuffers(0, 1, &m_vBuffer, &stride, &offset); pCon->IASetInputLayout(m_inputLayout); pCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); pCon->VSSetShader(m_vShader, 0, 0); pCon->PSSetShader(m_pShader, 0, 0); pCon->UpdateSubresource(m_camWorldBuffer, 0, NULL, &tWorld, 0, 0); pCon->UpdateSubresource(m_camViewBuffer, 0, NULL, &tView, 0, 0); pCon->UpdateSubresource(m_camProjBuffer, 0, NULL, &tProj, 0, 0); pCon->VSSetConstantBuffers(0, 1, &m_camWorldBuffer); pCon->VSSetConstantBuffers(1, 1, &m_camViewBuffer); pCon->VSSetConstantBuffers(2, 1, &m_camProjBuffer); pCon->Draw(m_particleList.size(), 0); pCon->VSSetShader(NULL, 0, 0); pCon->GSSetShader(NULL, 0, 0); pCon->PSSetShader(NULL, 0, 0); }
static void DoRendering (const float* worldMatrix, const float* identityMatrix, float* projectionMatrix, const MyVertex* verts) { // Does actual rendering of a simple triangle #if SUPPORT_D3D11 // D3D11 case if (s_DeviceType == kUnityGfxRendererD3D11 && EnsureD3D11ResourcesAreCreated()) { ID3D11DeviceContext* ctx = NULL; g_D3D11Device->GetImmediateContext (&ctx); ID3D11RenderTargetView* pCurrentRenderTarget; ID3D11DepthStencilView* pCurrentDepthStencil; // Get the current render targets ctx->OMGetRenderTargets(1, &pCurrentRenderTarget, &pCurrentDepthStencil); ctx->OMSetRenderTargets(1, &g_pD3D11RenderTargetView, nullptr); /* // update native texture from code D3D11_TEXTURE2D_DESC desc; g_TexturePointer->GetDesc(&desc); unsigned char* data = new unsigned char[desc.Width*desc.Height * 4]; FillTextureFromCode(desc.Width, desc.Height, desc.Width * 4, data); ctx->UpdateSubresource(g_TexturePointer, 0, NULL, data, desc.Width * 4, 0); delete[] data; */ const float CLEAR_CLR[4] = { 1, 1, 0, 1 }; // Yellow ctx->ClearRenderTargetView(g_pD3D11RenderTargetView, CLEAR_CLR); // Restore the original render target ctx->OMSetRenderTargets(1, &pCurrentRenderTarget, pCurrentDepthStencil); // update constant buffer - just the world matrix in our case ctx->UpdateSubresource (g_D3D11CB, 0, NULL, worldMatrix, 64, 0); // set shaders ctx->VSSetConstantBuffers (0, 1, &g_D3D11CB); ctx->VSSetShader (g_D3D11VertexShader, NULL, 0); ctx->PSSetShader (g_D3D11PixelShader, NULL, 0); // update vertex buffer ctx->UpdateSubresource (g_D3D11VB, 0, NULL, verts, sizeof(verts[0])*3, 0); // set input assembler data and draw ctx->IASetInputLayout (g_D3D11InputLayout); ctx->IASetPrimitiveTopology (D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); UINT stride = sizeof(MyVertex); UINT offset = 0; ctx->IASetVertexBuffers (0, 1, &g_D3D11VB, &stride, &offset); ctx->Draw (3, 0); ctx->Release(); } #endif }
//------------------------------------------------------------------------------------ void Water::_FinalCompose() { ID3D11DeviceContext* pContext = m_pRenderSystem->GetDeviceContext(); pContext->UpdateSubresource( m_pCB_VS, 0, NULL, &m_constantBufVS, 0, 0 ); pContext->VSSetConstantBuffers( 1, 1, &m_pCB_VS ); pContext->UpdateSubresource( m_pCB_PS, 0, NULL, &m_constantBufPS, 0, 0 ); pContext->PSSetConstantBuffers( 2, 1, &m_pCB_PS ); m_waterMesh->GetSubMesh(0)->SetMaterial(m_pFinalComposeMaterial); m_pEntity->Render(); }
Void D3D11Texture1D::Update( const D3D11SubResourceIndex * pIndex, UInt iX, UInt iWidth, const Void * pSrcData, D3D11DeferredContext * pContext ) const { DebugAssert( IsCreated() ); DebugAssert( CanUpdate() ); DebugAssert( !m_bLocked ); UInt iSubResource = _GetSubResourceIndex( pIndex, m_hTextureDesc.MipLevels, m_hTextureDesc.ArraySize ); DebugAssert( iX + iWidth <= _GetBound(m_hTextureDesc.Width, pIndex->iMipSlice) ); D3D11_BOX hBox; hBox.left = iX; hBox.right = iX + iWidth; hBox.top = 0; hBox.bottom = 1; hBox.front = 0; hBox.back = 1; UInt iStride = _GetStride( m_hTextureDesc.Format ); UInt iPitch = iStride * _GetBound( m_hTextureDesc.Width, pIndex->iMipSlice ); ID3D11DeviceContext * pDeviceContext = m_pRenderer->m_pImmediateContext; if ( pContext != NULL && pContext->IsCreated() ) pDeviceContext = pContext->m_pDeferredContext; pDeviceContext->UpdateSubresource( m_pTexture, iSubResource, &hBox, pSrcData, iPitch, 0 ); }
Void D3D11TextureCube::Update( const D3D11SubResourceIndex * pIndex, D3D11TextureCubeFace iFace, const D3D11Rectangle * pRect, const Void * pSrcData, D3D11DeferredContext * pContext ) const { DebugAssert( IsCreated() ); DebugAssert( CanUpdate() ); DebugAssert( !m_bLocked ); UInt iSubResource = _GetSubResourceIndex( pIndex, iFace, m_hTextureDesc.MipLevels, m_hTextureDesc.ArraySize ); DebugAssert( pRect->iLeft >= 0 && pRect->iLeft < pRect->iRight && pRect->iRight <= (signed)_GetBound(m_hTextureDesc.Width,pIndex->iMipSlice) ); DebugAssert( pRect->iTop >= 0 && pRect->iTop < pRect->iBottom && pRect->iBottom <= (signed)_GetBound(m_hTextureDesc.Height,pIndex->iMipSlice) ); D3D11_BOX hBox; hBox.left = pRect->iLeft; hBox.right = pRect->iRight; hBox.top = pRect->iTop; hBox.bottom = pRect->iBottom; hBox.front = 0; hBox.back = 1; UInt iStride = _GetStride( m_hTextureDesc.Format ); UInt iPitch = iStride * _GetBound( m_hTextureDesc.Width, pIndex->iMipSlice ); UInt iSlice = iPitch * _GetBound( m_hTextureDesc.Height, pIndex->iMipSlice ); ID3D11DeviceContext * pDeviceContext = m_pRenderer->m_pImmediateContext; if ( pContext != NULL && pContext->IsCreated() ) pDeviceContext = pContext->m_pDeferredContext; pDeviceContext->UpdateSubresource( m_pTexture, iSubResource, &hBox, pSrcData, iPitch, iSlice ); }
Void D3D11Texture3D::Update( const D3D11SubResourceIndex * pIndex, const D3D11Box * pBox, const Void * pSrcData, D3D11DeferredContext * pContext ) const { DebugAssert( IsCreated() ); DebugAssert( CanUpdate() ); DebugAssert( !m_bLocked ); UInt iSubResource = _GetSubResourceIndex( pIndex, m_hTextureDesc.MipLevels, 1 ); DebugAssert( pBox->iLeft < pBox->iRight && pBox->iRight <= _GetBound(m_hTextureDesc.Width,pIndex->iMipSlice) ); DebugAssert( pBox->iTop < pBox->iBottom && pBox->iBottom <= _GetBound(m_hTextureDesc.Height,pIndex->iMipSlice) ); DebugAssert( pBox->iFront < pBox->iBack && pBox->iBack <= _GetBound(m_hTextureDesc.Depth,pIndex->iMipSlice) ); D3D11_BOX hBox; hBox.left = pBox->iLeft; hBox.right = pBox->iRight; hBox.top = pBox->iTop; hBox.bottom = pBox->iBottom; hBox.front = pBox->iFront; hBox.back = pBox->iBack; UInt iStride = _GetStride( m_hTextureDesc.Format ); UInt iPitch = iStride * _GetBound( m_hTextureDesc.Width, pIndex->iMipSlice ); UInt iSlice = iPitch * _GetBound( m_hTextureDesc.Height, pIndex->iMipSlice ); ID3D11DeviceContext * pDeviceContext = m_pRenderer->m_pImmediateContext; if ( pContext != NULL && pContext->IsCreated() ) pDeviceContext = pContext->m_pDeferredContext; pDeviceContext->UpdateSubresource( m_pTexture, iSubResource, &hBox, pSrcData, iPitch, iSlice ); }
// //------------------------------------------------------------------------ CPUTResult CPUTGuiControllerDX11::UpdateUberBuffers() { ID3D11DeviceContext* pImmediateContext = CPUT_DX11::GetContext(); ASSERT(pImmediateContext, "CPUTGuiControllerDX11::UpdateUberBuffers - Context pointer is NULL"); ID3D11Buffer* pVB = mpUberBuffer->GetVertexBuffer(); pImmediateContext->UpdateSubresource(pVB, 0, NULL, (void *)mpMirrorBuffer, sizeof(CPUTGUIVertex)*mUberBufferIndex+1, 0); mpUberBuffer->SetNumVertices(mUberBufferIndex); return CPUT_SUCCESS; }
//------------------------------------------------------------------------------------ void Water::_RenderWaterDepth() { ID3D11DeviceContext* pContext = m_pRenderSystem->GetDeviceContext(); pContext->UpdateSubresource( m_pCB_Depth, 0, NULL, &m_constantBufDepth, 0, 0 ); pContext->VSSetConstantBuffers( 1, 1, &m_pCB_Depth ); pContext->PSSetConstantBuffers( 1, 1, &m_pCB_Depth ); m_pRT_Depth->Update(m_pWaterDepthMaterial); }
void GSDevice11::SetupVS(VSSelector sel, const VSConstantBuffer* cb) { hash_map<uint32, GSVertexShader11 >::const_iterator i = m_vs.find(sel); if(i == m_vs.end()) { string str[4]; str[0] = format("%d", sel.bppz); str[1] = format("%d", sel.tme); str[2] = format("%d", sel.fst); str[3] = format("%d", sel.rtcopy); D3D_SHADER_MACRO macro[] = { {"VS_BPPZ", str[0].c_str()}, {"VS_TME", str[1].c_str()}, {"VS_FST", str[2].c_str()}, {"VS_RTCOPY", str[3].c_str()}, {NULL, NULL}, }; D3D11_INPUT_ELEMENT_DESC layout[] = { {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"TEXCOORD", 1, DXGI_FORMAT_R32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"POSITION", 0, DXGI_FORMAT_R16G16_UINT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"POSITION", 1, DXGI_FORMAT_R32_UINT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"TEXCOORD", 2, DXGI_FORMAT_R16G16_UINT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"COLOR", 1, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; GSVertexShader11 vs; vector<unsigned char> shader; theApp.LoadResource(IDR_TFX_FX, shader); CompileShader((const char *)shader.data(), shader.size(), "tfx.fx", nullptr, "vs_main", macro, &vs.vs, layout, countof(layout), &vs.il); m_vs[sel] = vs; i = m_vs.find(sel); } if(m_vs_cb_cache.Update(cb)) { ID3D11DeviceContext* ctx = m_ctx; ctx->UpdateSubresource(m_vs_cb, 0, NULL, cb, 0, 0); } VSSetShader(i->second.vs, m_vs_cb); IASetInputLayout(i->second.il); }
void ParticleManager::UpdateParticles(float dt, DxGraphics* pDxGraphics, Camera& cam) { // Update the world mat XMMATRIX transMat = XMMatrixTranslation(0.0f, 0.0f, 0.0f); XMMATRIX rotMat = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f); XMMATRIX scaleMat = XMMatrixScaling(1.0f, 1.0f, 1.0f); m_worldMat = rotMat * scaleMat * transMat; cam.CalculateViewMatrix(); // stream out the particles which runs through the physics XMMATRIX tWorld = XMMatrixTranspose(m_worldMat); XMMATRIX tView = XMMatrixTranspose(cam.GetViewMatrix()); XMMATRIX tProj = XMMatrixTranspose(cam.GetProjMatrix()); m_massPoint.dt = dt; ID3D11DeviceContext* pCon = pDxGraphics->GetImmediateContext(); // Set the stream out buffer UINT offsetSO[1] = { 0 }; pCon->SOSetTargets(1, &m_vStream, offsetSO); UINT stride = sizeof(Particle); UINT offset = 0; pCon->IASetVertexBuffers(0, 1, &m_vBuffer, &stride, &offset); pCon->IASetInputLayout(m_inputLayout); pCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); pCon->VSSetShader(m_vUpdate, 0, 0); pCon->GSSetShader(m_gUpdate, 0, 0); pCon->PSSetShader(NULL, 0, 0); //// Update the cBuffer pCon->UpdateSubresource(m_pointBuffer, 0, NULL, &m_massPoint, 0, 0); pCon->VSSetConstantBuffers(3, 1, &m_pointBuffer); pCon->Draw(m_particleList.size(), 0); pCon->VSSetShader(NULL, 0, 0); pCon->GSSetShader(NULL, 0, 0); pCon->PSSetShader(NULL, 0, 0); // Unbind the SO ID3D11Buffer* bufferArray[1] = { 0 }; pCon->SOSetTargets(1, bufferArray, offsetSO); std::swap(m_vStream, m_vBuffer); }
void Model::Paint(const XMMATRIX &world, Camera *camera, XMFLOAT3 lightPos) { unsigned int stride = mSizeOfVertexDesc; unsigned int offset = 0; XMMATRIX view = camera->GetViewMatrix(); XMMATRIX proj = camera->GetProjectionMatrix(); MatrixBuffer buffer; buffer.world = XMMatrixTranspose(world); buffer.view = XMMatrixTranspose(view); buffer.proj = XMMatrixTranspose(proj); buffer.wvp = XMMatrixTranspose(world * view * proj); buffer.cameraPos = camera->GetPosition(); ID3D11DeviceContext *dc = GameEngine::GetInstance()->GetGraphicsManager()->GetGraphicsDeviceContext(); dc->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset); dc->IASetInputLayout(mInputLayout); dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); dc->VSSetShader(mVertexShader, 0, 0); dc->PSSetShader(mFragmentShader, 0, 0); dc->PSSetShaderResources(0, 1, &mTextureView); dc->PSSetSamplers(0, 1, &mTextureSamplerState); dc->UpdateSubresource(mMatrixBuffer, 0, 0, &buffer, 0, 0); dc->VSSetConstantBuffers(0, 1, &mMatrixBuffer); dc->UpdateSubresource(mLightBuffer, 0, 0, &lightPos, 0, 0); dc->VSSetConstantBuffers(1, 1, &mLightBuffer); if (mAnimation) { mAnimation->SetAnimationProperties(dc); } dc->Draw(mNumberOfVertices, 0); }
bool CGUIFontTTFDX::CopyCharToTexture(FT_BitmapGlyph bitGlyph, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) { FT_Bitmap bitmap = bitGlyph->bitmap; ID3D11DeviceContext* pContext = g_Windowing.GetImmediateContext(); if (m_speedupTexture && m_speedupTexture->Get() && pContext && bitmap.buffer) { CD3D11_BOX dstBox(x1, y1, 0, x2, y2, 1); pContext->UpdateSubresource(m_speedupTexture->Get(), 0, &dstBox, bitmap.buffer, bitmap.pitch, 0); return true; } return false; }
void GSDevice11::SetupGS(GSSelector sel, const GSConstantBuffer* cb) { CComPtr<ID3D11GeometryShader> gs; bool Unscale_GSShader = (sel.point == 1 || sel.line == 1) && UserHacks_unscale_pt_ln; if((sel.prim > 0 && (sel.iip == 0 || sel.prim == 3)) || Unscale_GSShader) // geometry shader works in every case, but not needed { hash_map<uint32, CComPtr<ID3D11GeometryShader> >::const_iterator i = m_gs.find(sel); if(i != m_gs.end()) { gs = i->second; } else { string str[4]; str[0] = format("%d", sel.iip); str[1] = format("%d", sel.prim); str[2] = format("%d", sel.point); str[3] = format("%d", sel.line); D3D_SHADER_MACRO macro[] = { {"GS_IIP", str[0].c_str()}, {"GS_PRIM", str[1].c_str()}, {"GS_POINT", str[2].c_str()}, {"GS_LINE", str[3].c_str()}, {NULL, NULL}, }; vector<unsigned char> shader; theApp.LoadResource(IDR_TFX_FX, shader); CompileShader((const char *)shader.data(), shader.size(), "tfx.fx", nullptr, "gs_main", macro, &gs); m_gs[sel] = gs; } } if (m_gs_cb_cache.Update(cb)) { ID3D11DeviceContext* ctx = m_ctx; ctx->UpdateSubresource(m_gs_cb, 0, NULL, cb, 0, 0); } GSSetShader(gs, m_gs_cb); }
void UnityTextureOutput::Write(const FrameData *frame) noexcept { // TODO: move this bit into constructor, if possible? ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)texture_ptr_; ID3D11Device *g_D3D11Device; d3dtex->GetDevice(&g_D3D11Device); ID3D11DeviceContext* ctx = NULL; g_D3D11Device->GetImmediateContext(&ctx); D3D11_TEXTURE2D_DESC desc; d3dtex->GetDesc(&desc); auto linelength = frame->size.width * frame->size.depth; auto buffer = (eye_ == Eye::LEFT) ? frame->buffer_left.get() : frame->buffer_right.get(); // TODO: https://gamedev.stackexchange.com/questions/60668/how-to-use-updatesubresource-and-map-unmap ? ctx->UpdateSubresource(d3dtex, 0, NULL, buffer, linelength, 0); ctx->Release(); }
bool xD11UnkwonTexture::_update(ID3D11Resource* pLockTexture , void* data , int dateLen , int rowPitch , int depthPicth , int mipmapLevel , int arraySlice) { if(pLockTexture == NULL) return false; ID3D11DeviceContext* pDeviceContext = m_pD11RenderApi->d11DeviceContext(); UINT lockResource = D3D10CalcSubresource((UINT)mipmapLevel , (UINT)arraySlice, (UINT)m_TexInfo.m_MipmapLevel); if(m_TexInfo.m_Usage == D3D11_USAGE_DEFAULT) { if(rowPitch == 0 ) rowPitch = dateLen; if(depthPicth == 0) depthPicth = dateLen; pDeviceContext->UpdateSubresource(pLockTexture ,lockResource , NULL , data ,rowPitch , depthPicth); } else //只有非DEFAULT的纹理才是可以Map的。 { char* pMapedData = NULL; int Pitch = 0; int SlicePith = 0; UINT mapFlag = 0; if(pMapedData == NULL) return false; D3D11_MAPPED_SUBRESOURCE mappedTex; mappedTex.pData = NULL; mappedTex.RowPitch = 0; mappedTex.DepthPitch = 0; pDeviceContext->Map(pLockTexture , lockResource , D3D11_MAP_WRITE_DISCARD , mapFlag , &mappedTex ); pMapedData = (char*)mappedTex.pData; Pitch = mappedTex.RowPitch; SlicePith = mappedTex.DepthPitch; memcpy(pMapedData , data , dateLen); pDeviceContext->Unmap(pLockTexture , lockResource); } return true; }
//------------------------------------------------------------------------------------ D3D11Texture::D3D11Texture(const StringVector& vecTexNames, bool bSRGB) : Texture(eTextureType_TextureArray, 0, 0, 0, ePF_Unknown, 0, true) , m_pTexture2D(nullptr) , m_pTexture3D(nullptr) , m_pRTV(nullptr) , m_pSRV(nullptr) , m_pDSV(nullptr) , m_pTexStaging(nullptr) { _AST(!vecTexNames.empty()); HRESULT hr = S_OK; // First load all texture elements std::vector<ID3D11Texture2D*> vecTexs(vecTexNames.size()); for (size_t i=0; i<vecTexNames.size(); ++i) { D3DX11_IMAGE_LOAD_INFO loadInfo; loadInfo.Width = D3DX11_FROM_FILE; loadInfo.Height = D3DX11_FROM_FILE; loadInfo.Depth = D3DX11_FROM_FILE; loadInfo.FirstMipLevel = 0; loadInfo.BindFlags = 0; loadInfo.Usage = D3D11_USAGE_STAGING; // Local res loadInfo.MipLevels = 0; loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ; loadInfo.MiscFlags = 0; loadInfo.Format = DXGI_FORMAT_FROM_FILE; loadInfo.Filter = D3DX11_FILTER_NONE; loadInfo.MipFilter = D3DX11_FILTER_LINEAR; loadInfo.pSrcInfo = 0; if (vecTexNames[i].find(".dds") != STRING::npos) { V(DirectX::CreateDDSTextureFromFileEx(g_pRenderSys->GetDevice(), EngineToUnicode(vecTexNames[i]).c_str(), 4096, D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ, 0, bSRGB, (ID3D11Resource**)&vecTexs[i], nullptr)); } else { V(D3DX11CreateTextureFromFileA(g_pRenderSys->GetDevice(), vecTexNames[i].c_str(), &loadInfo, nullptr, (ID3D11Resource**)&vecTexs[i], nullptr)); } } // Then create the texture array object D3D11_TEXTURE2D_DESC texElementDesc; vecTexs[0]->GetDesc(&texElementDesc); // Store dimension and format m_width = texElementDesc.Width; m_height = texElementDesc.Height; m_texFormat = ConvertFromDXFormat(texElementDesc.Format); D3D11_TEXTURE2D_DESC texArrayDesc; texArrayDesc.Width = texElementDesc.Width; texArrayDesc.Height = texElementDesc.Height; texArrayDesc.MipLevels = texElementDesc.MipLevels; texArrayDesc.ArraySize = vecTexs.size(); texArrayDesc.Format = texElementDesc.Format; texArrayDesc.SampleDesc.Count = 1; texArrayDesc.SampleDesc.Quality = 0; texArrayDesc.Usage = D3D11_USAGE_DEFAULT; texArrayDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; texArrayDesc.CPUAccessFlags = 0; texArrayDesc.MiscFlags = 0; V(g_pRenderSys->GetDevice()->CreateTexture2D(&texArrayDesc, 0, &m_pTexture2D)); // Fill texture array data ID3D11DeviceContext* dc = g_pRenderSys->GetDeviceContext(); for(size_t texElement=0; texElement<vecTexs.size(); ++texElement) { vecTexs[texElement]->GetDesc(&texElementDesc); for(UINT mipLevel = 0; mipLevel < texElementDesc.MipLevels; ++mipLevel) { D3D11_MAPPED_SUBRESOURCE mappedTex2D; V(dc->Map(vecTexs[texElement], mipLevel, D3D11_MAP_READ, 0, &mappedTex2D)); dc->UpdateSubresource(m_pTexture2D, D3D11CalcSubresource(mipLevel, texElement, texElementDesc.MipLevels), 0, mappedTex2D.pData, mappedTex2D.RowPitch, mappedTex2D.DepthPitch); dc->Unmap(vecTexs[texElement], mipLevel); } } CreateSRV(); for(size_t i=0; i<vecTexs.size(); ++i) vecTexs[i]->Release(); }
//------------------------------------------------------------------------------------ D3D11Texture::D3D11Texture( const StringVector& vecTexNames ) :m_pTexture2D(nullptr) ,m_pTexture3D(nullptr) ,m_pRenderSystem(g_env.pRenderSystem) ,m_rtView(nullptr) ,m_pSRV(nullptr) ,m_pDSV(nullptr) ,m_usage(0) ,m_texType(eTextureType_TextureArray) ,m_bMipMap(true) { m_pd3dDevice = m_pRenderSystem->GetDevice(); if (m_pd3dDevice) m_pd3dDevice->AddRef(); assert(!vecTexNames.empty()); HRESULT hr = S_OK; // First load all texture elements std::vector<ID3D11Texture2D*> vecTexs(vecTexNames.size()); for (size_t i=0; i<vecTexNames.size(); ++i) { D3DX11_IMAGE_LOAD_INFO loadInfo; loadInfo.Width = D3DX11_FROM_FILE; loadInfo.Height = D3DX11_FROM_FILE; loadInfo.Depth = D3DX11_FROM_FILE; loadInfo.FirstMipLevel = 0; loadInfo.BindFlags = 0; loadInfo.Usage = D3D11_USAGE_STAGING; // Local res loadInfo.MipLevels = 0; loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ; loadInfo.MiscFlags = 0; loadInfo.Format = DXGI_FORMAT_FROM_FILE; loadInfo.Filter = D3DX11_FILTER_NONE; loadInfo.MipFilter = D3DX11_FILTER_LINEAR; loadInfo.pSrcInfo = 0; V(D3DX11CreateTextureFromFileA(m_pRenderSystem->GetDevice(), vecTexNames[i].c_str(), &loadInfo, nullptr, (ID3D11Resource**)&vecTexs[i], nullptr)); } // Then create the texture array object D3D11_TEXTURE2D_DESC texElementDesc; vecTexs[0]->GetDesc(&texElementDesc); // Store dimension and format m_width = texElementDesc.Width; m_height = texElementDesc.Height; m_texFormat = ConvertFromDXFormat(texElementDesc.Format); D3D11_TEXTURE2D_DESC texArrayDesc; texArrayDesc.Width = texElementDesc.Width; texArrayDesc.Height = texElementDesc.Height; texArrayDesc.MipLevels = texElementDesc.MipLevels; texArrayDesc.ArraySize = vecTexs.size(); texArrayDesc.Format = texElementDesc.Format; texArrayDesc.SampleDesc.Count = 1; texArrayDesc.SampleDesc.Quality = 0; texArrayDesc.Usage = D3D11_USAGE_DEFAULT; texArrayDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; texArrayDesc.CPUAccessFlags = 0; texArrayDesc.MiscFlags = 0; V(m_pRenderSystem->GetDevice()->CreateTexture2D( &texArrayDesc, 0, &m_pTexture2D)); // Fill texture array data ID3D11DeviceContext* dc = m_pRenderSystem->GetDeviceContext(); for(size_t texElement=0; texElement<vecTexs.size(); ++texElement) { for(UINT mipLevel = 0; mipLevel < texElementDesc.MipLevels; ++mipLevel) { D3D11_MAPPED_SUBRESOURCE mappedTex2D; V(dc->Map(vecTexs[texElement], mipLevel, D3D11_MAP_READ, 0, &mappedTex2D)); dc->UpdateSubresource(m_pTexture2D, D3D11CalcSubresource(mipLevel, texElement, texElementDesc.MipLevels), 0, mappedTex2D.pData, mappedTex2D.RowPitch, mappedTex2D.DepthPitch); dc->Unmap(vecTexs[texElement], mipLevel); } } CreateSRV(); for(size_t i=0; i<vecTexs.size(); ++i) vecTexs[i]->Release(); }
void VRender() override { float color[4] = { 0.2f, 0.2f, 0.2f, 1.0f }; // Set up the input assembler mDeviceContext->IASetInputLayout(mInputLayout); mRenderer->VSetPrimitiveType(GPU_PRIMITIVE_TYPE_TRIANGLE); mDeviceContext->RSSetViewports(1, &mRenderer->GetViewport()); mDeviceContext->OMSetRenderTargets(1, mRenderer->GetRenderTargetView(), mRenderer->GetDepthStencilView()); mDeviceContext->ClearRenderTargetView(*mRenderer->GetRenderTargetView(), color); mDeviceContext->ClearDepthStencilView( mRenderer->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0); mDeviceContext->VSSetShader(mVertexShader, NULL, 0); mDeviceContext->PSSetShader(mPixelShader, NULL, 0); mDeviceContext->UpdateSubresource( mConstantBuffer, 0, NULL, &mMatrixBuffer, 0, 0); mDeviceContext->VSSetConstantBuffers( 0, 1, &mConstantBuffer); // Bezier mDeviceContext->UpdateSubresource(static_cast<DX11Mesh*>(mBezierMesh)->mVertexBuffer, 0, NULL, &mBezierVertices, 0, 0); mRenderer->VSetPrimitiveType(GPU_PRIMITIVE_TYPE_LINE); mRenderer->VBindMesh(mBezierMesh); mRenderer->VDrawIndexed(0, mBezierMesh->GetIndexCount()); // Handles mDeviceContext->UpdateSubresource(static_cast<DX11Mesh*>(mHandlesMesh)->mVertexBuffer, 0, NULL, &mHandlesVertices, 0, 0); mRenderer->VSetPrimitiveType(GPU_PRIMITIVE_TYPE_LINE); mRenderer->VBindMesh(mHandlesMesh); mRenderer->VDrawIndexed(0, mHandlesMesh->GetIndexCount()); // Circles mRenderer->VSetPrimitiveType(GPU_PRIMITIVE_TYPE_TRIANGLE); for (size_t i = 0; i < 4; i++) { mMatrixBuffer.mWorld = (mat4f::scale(mCircleScale) * mat4f::translate(mBezier.p[i])).transpose(); mDeviceContext->UpdateSubresource(mConstantBuffer, 0, NULL, &mMatrixBuffer, 0, 0); mRenderer->VBindMesh(mCircleMesh); mRenderer->VDrawIndexed(0, mCircleMesh->GetIndexCount()); } mRenderer->VSwapBuffers(); }
/** * Handle Stereo Drawing. ***/ void* StereoPresenter::Provoke(void* pThis, int eD3D, int eD3DInterface, int eD3DMethod, DWORD dwNumberConnected, int& nProvokerIndex) { #ifdef _DEBUG_STP { wchar_t buf[128]; wsprintf(buf, L"[STP] ifc %u mtd %u", eD3DInterface, eD3DMethod); OutputDebugString(buf); } #endif // update our global time static float fGlobalTime = 0.0f; static DWORD dwTimeStart = 0; DWORD dwTimeCur = GetTickCount(); if (dwTimeStart == 0) dwTimeStart = dwTimeCur; fGlobalTime = (dwTimeCur - dwTimeStart) / 1000.0f; // only present accepted bool bValid = false; if (((eD3DInterface == INTERFACE_IDXGISWAPCHAIN) && (eD3DMethod == METHOD_IDXGISWAPCHAIN_PRESENT)) || ((eD3DInterface == INTERFACE_IDIRECT3DDEVICE9) && (eD3DMethod == METHOD_IDIRECT3DDEVICE9_PRESENT))) bValid = true; if (!bValid) return nullptr; // clear all previous menu events ZeroMemory(&m_abMenuEvents[0], sizeof(VireioMenuEvent)* (int)VireioMenuEvent::NumberOfEvents); // main menu update ? if ((m_sMainMenu.bOnChanged) && (!m_sMenuControl.eSelectionMovement)) { m_sMainMenu.bOnChanged = false; // loop through entries for (size_t nIx = 0; nIx < m_sMainMenu.asEntries.size(); nIx++) { // entry index changed ? if (m_sMainMenu.asEntries[nIx].bOnChanged) { m_sMainMenu.asEntries[nIx].bOnChanged = false; // set new menu index.. selection to zero m_sMenuControl.nMenuIx = (INT)nIx; m_sMenuControl.unSelectionFormer = m_sMenuControl.unSelection = 0; } } } // sub menu update ? if ((m_sSubMenu.bOnChanged) && (!m_sMenuControl.eSelectionMovement)) { m_sSubMenu.bOnChanged = false; // exit ? if (m_sSubMenu.bOnExit) m_sMenuControl.nMenuIx = -1; // loop through entries for (size_t nIx = 0; nIx < m_sSubMenu.asEntries.size(); nIx++) { // entry index changed ? if (m_sSubMenu.asEntries[nIx].bOnChanged) { m_sSubMenu.asEntries[nIx].bOnChanged = false; // font ? if (nIx == ENTRY_FONT) { // get device and context ID3D11Device* pcDevice = nullptr; ID3D11DeviceContext* pcContext = nullptr; HRESULT nHr = S_OK; if ((eD3DInterface == INTERFACE_IDXGISWAPCHAIN) && (eD3DMethod == METHOD_IDXGISWAPCHAIN_PRESENT)) nHr = GetDeviceAndContext((IDXGISwapChain*)pThis, &pcDevice, &pcContext); else { if (m_ppcTexView11[0]) { if (*(m_ppcTexView11[0])) (*(m_ppcTexView11[0]))->GetDevice(&pcDevice); if (pcDevice) pcDevice->GetImmediateContext(&pcContext); else nHr = E_FAIL; if (!pcContext) nHr = E_FAIL; } else nHr = E_FAIL; } if (SUCCEEDED(nHr)) { HRESULT nHr; // get base directory std::string strVireioPath = GetBaseDir(); // add file path strVireioPath += "font//"; strVireioPath += m_sSubMenu.asEntries[nIx].astrValueEnumeration[m_sSubMenu.asEntries[nIx].unValue]; strVireioPath += ".spritefont"; OutputDebugStringA(strVireioPath.c_str()); // create font, make backup VireioFont* pcOldFont = m_pcFontSegeo128; m_pcFontSegeo128 = new VireioFont(pcDevice, pcContext, strVireioPath.c_str(), 128.0f, 1.0f, nHr, 1); if (FAILED(nHr)) { delete m_pcFontSegeo128; m_pcFontSegeo128 = pcOldFont; } else { // set new font name m_strFontName = m_sSubMenu.asEntries[nIx].astrValueEnumeration[m_sSubMenu.asEntries[nIx].unValue]; // write to ini file char szFilePathINI[1024]; GetCurrentDirectoryA(1024, szFilePathINI); strcat_s(szFilePathINI, "\\VireioPerception.ini"); WritePrivateProfileStringA("Stereo Presenter", "strFontName", m_strFontName.c_str(), szFilePathINI); } } SAFE_RELEASE(pcDevice); SAFE_RELEASE(pcContext); } } } } // get xbox controller input XINPUT_STATE sControllerState; bool bControllerAttached = false; ZeroMemory(&sControllerState, sizeof(XINPUT_STATE)); if (XInputGetState(0, &sControllerState) == ERROR_SUCCESS) { bControllerAttached = true; } if (true) { #pragma region menu hotkeys static bool bReleased = true; static bool s_bOnMenu = false; // keyboard menu on/off event + get hand poses UINT uIxHandPoses = 0, uIxPoseRequest = 0; s_bOnMenu = GetAsyncKeyState(VK_LCONTROL) && GetAsyncKeyState(0x51); for (UINT unIx = 0; unIx < 32; unIx++) { // set menu bool event if (m_apsSubMenues[unIx]) { if (m_apsSubMenues[unIx]->bOnBack) { // main menu ? exit if ((m_sMenuControl.nMenuIx == -1) && (!m_sMenuControl.eSelectionMovement)) s_bOnMenu = true; else m_abMenuEvents[VireioMenuEvent::OnExit] = TRUE; m_apsSubMenues[unIx]->bOnBack = false; } // hand poses ? if (m_apsSubMenues[unIx]->bHandPosesPresent) uIxHandPoses = unIx; if (m_apsSubMenues[unIx]->bHandPosesRequest) uIxPoseRequest = unIx; } } if ((m_apsSubMenues[uIxHandPoses]) && (m_apsSubMenues[uIxPoseRequest])) { // copy the hand pose data to the request node m_apsSubMenues[uIxPoseRequest]->sPoseMatrix[0] = m_apsSubMenues[uIxHandPoses]->sPoseMatrix[0]; m_apsSubMenues[uIxPoseRequest]->sPoseMatrix[1] = m_apsSubMenues[uIxHandPoses]->sPoseMatrix[1]; m_apsSubMenues[uIxPoseRequest]->sPosition[0] = m_apsSubMenues[uIxHandPoses]->sPosition[0]; m_apsSubMenues[uIxPoseRequest]->sPosition[1] = m_apsSubMenues[uIxHandPoses]->sPosition[1]; } // static hotkeys : LCTRL+Q - toggle vireio menu // F12 - toggle stereo output if (GetAsyncKeyState(VK_F12)) { m_bHotkeySwitch = true; } else if (s_bOnMenu) { m_bMenuHotkeySwitch = true; } else if (m_bMenuHotkeySwitch) { m_bMenuHotkeySwitch = false; m_bMenu = !m_bMenu; for (UINT unIx = 0; unIx < 32; unIx++) { // set sub menu active if menu is active if (m_apsSubMenues[unIx]) m_apsSubMenues[unIx]->bIsActive = m_bMenu; } } else if (m_bHotkeySwitch) { if (m_eStereoMode) m_eStereoMode = VireioMonitorStereoModes::Vireio_Mono; else m_eStereoMode = VireioMonitorStereoModes::Vireio_SideBySide; m_bHotkeySwitch = false; } else bReleased = true; #pragma endregion #pragma region menu events // menu is shown ? if (m_bMenu) { // handle controller if (bControllerAttached) { if (sControllerState.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) { m_abMenuEvents[VireioMenuEvent::OnExit] = TRUE; } if (sControllerState.Gamepad.wButtons & XINPUT_GAMEPAD_A) { m_abMenuEvents[VireioMenuEvent::OnAccept] = TRUE; } if (sControllerState.Gamepad.sThumbLY > 28000) m_abMenuEvents[VireioMenuEvent::OnUp] = TRUE; if (sControllerState.Gamepad.sThumbLY < -28000) m_abMenuEvents[VireioMenuEvent::OnDown] = TRUE; if (sControllerState.Gamepad.sThumbLX > 28000) m_abMenuEvents[VireioMenuEvent::OnRight] = TRUE; if (sControllerState.Gamepad.sThumbLX < -28000) m_abMenuEvents[VireioMenuEvent::OnLeft] = TRUE; } // loop through sub menues for (UINT unIx = 0; unIx < 32; unIx++) { // set bool events if (m_apsSubMenues[unIx]) { if (m_apsSubMenues[unIx]->bOnUp) m_abMenuEvents[VireioMenuEvent::OnUp] = TRUE; if (m_apsSubMenues[unIx]->bOnDown) m_abMenuEvents[VireioMenuEvent::OnDown] = TRUE; if (m_apsSubMenues[unIx]->bOnLeft) m_abMenuEvents[VireioMenuEvent::OnLeft] = TRUE; if (m_apsSubMenues[unIx]->bOnRight) m_abMenuEvents[VireioMenuEvent::OnRight] = TRUE; if (m_apsSubMenues[unIx]->bOnAccept) m_abMenuEvents[VireioMenuEvent::OnAccept] = TRUE; // clear events m_apsSubMenues[unIx]->bOnUp = false; m_apsSubMenues[unIx]->bOnDown = false; m_apsSubMenues[unIx]->bOnLeft = false; m_apsSubMenues[unIx]->bOnRight = false; m_apsSubMenues[unIx]->bOnAccept = false; m_apsSubMenues[unIx]->bOnBack = false; } } #pragma endregion #pragma region menu update/render // update UpdateMenu(fGlobalTime); // get device and context ID3D11Device* pcDevice = nullptr; ID3D11DeviceContext* pcContext = nullptr; HRESULT nHr = S_OK; if ((eD3DInterface == INTERFACE_IDXGISWAPCHAIN) && (eD3DMethod == METHOD_IDXGISWAPCHAIN_PRESENT)) nHr = GetDeviceAndContext((IDXGISwapChain*)pThis, &pcDevice, &pcContext); else { if (m_ppcTexView11[0]) { if (*(m_ppcTexView11[0])) (*(m_ppcTexView11[0]))->GetDevice(&pcDevice); if (pcDevice) pcDevice->GetImmediateContext(&pcContext); else nHr = E_FAIL; if (!pcContext) nHr = E_FAIL; } else nHr = E_FAIL; } if (FAILED(nHr)) { // release frame texture+view if (pcDevice) { pcDevice->Release(); pcDevice = nullptr; } if (pcContext) { pcContext->Release(); pcContext = nullptr; } return nullptr; } // create the depth stencil... if D3D11 if ((eD3DInterface == INTERFACE_IDXGISWAPCHAIN) && (eD3DMethod == METHOD_IDXGISWAPCHAIN_PRESENT) && (!m_pcDSGeometry11)) { ID3D11Texture2D* pcBackBuffer = nullptr; ((IDXGISwapChain*)pThis)->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pcBackBuffer); if (pcBackBuffer) { D3D11_TEXTURE2D_DESC sDesc; pcBackBuffer->GetDesc(&sDesc); pcBackBuffer->Release(); // Create depth stencil texture D3D11_TEXTURE2D_DESC descDepth; ZeroMemory(&descDepth, sizeof(descDepth)); descDepth.Width = sDesc.Width; descDepth.Height = sDesc.Height; descDepth.MipLevels = 1; descDepth.ArraySize = 1; descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; descDepth.SampleDesc.Count = 1; descDepth.SampleDesc.Quality = 0; descDepth.Usage = D3D11_USAGE_DEFAULT; descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL; descDepth.CPUAccessFlags = 0; descDepth.MiscFlags = 0; if (FAILED(pcDevice->CreateTexture2D(&descDepth, NULL, &m_pcDSGeometry11))) OutputDebugString(L"[STP] Failed to create depth stencil."); // Create the depth stencil view D3D11_DEPTH_STENCIL_VIEW_DESC descDSV; ZeroMemory(&descDSV, sizeof(descDSV)); descDSV.Format = descDepth.Format; descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; descDSV.Texture2D.MipSlice = 0; if (FAILED(pcDevice->CreateDepthStencilView(m_pcDSGeometry11, &descDSV, &m_pcDSVGeometry11))) OutputDebugString(L"[STP] Failed to create depth stencil view."); } } // get the viewport UINT dwNumViewports = 1; D3D11_VIEWPORT psViewport[16]; pcContext->RSGetViewports(&dwNumViewports, psViewport); // backup all states D3DX11_STATE_BLOCK sStateBlock; CreateStateblock(pcContext, &sStateBlock); // clear all states, set targets ClearContextState(pcContext); // set the menu texture (if present) if (m_ppcTexViewMenu) { if (*m_ppcTexViewMenu) { // set render target ID3D11RenderTargetView* pcRTView = *m_ppcTexViewMenu; pcContext->OMSetRenderTargets(1, &pcRTView, NULL); // set viewport D3D11_VIEWPORT sViewport = {}; sViewport.TopLeftX = 0; sViewport.TopLeftY = 0; sViewport.Width = 1024; sViewport.Height = 1024; sViewport.MinDepth = 0.0f; sViewport.MaxDepth = 1.0f; pcContext->RSSetViewports(1, &sViewport); // clear render target...zero alpha FLOAT afColorRgba[4] = { 0.5f, 0.4f, 0.2f, 0.4f }; pcContext->ClearRenderTargetView(*m_ppcTexViewMenu, afColorRgba); } } else if ((eD3DInterface == INTERFACE_IDXGISWAPCHAIN) && (eD3DMethod == METHOD_IDXGISWAPCHAIN_PRESENT)) { // set first active render target - the stored back buffer - get the stored private data view ID3D11Texture2D* pcBackBuffer = nullptr; ((IDXGISwapChain*)pThis)->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pcBackBuffer); ID3D11RenderTargetView* pcView = nullptr; UINT dwSize = sizeof(pcView); pcBackBuffer->GetPrivateData(PDIID_ID3D11TextureXD_RenderTargetView, &dwSize, (void*)&pcView); if (dwSize) { pcContext->OMSetRenderTargets(1, (ID3D11RenderTargetView**)&pcView, m_pcDSVGeometry11); pcView->Release(); } else { // create render target view for the back buffer ID3D11RenderTargetView* pcRTV = nullptr; pcDevice->CreateRenderTargetView(pcBackBuffer, NULL, &pcRTV); if (pcRTV) { pcBackBuffer->SetPrivateDataInterface(PDIID_ID3D11TextureXD_RenderTargetView, pcRTV); pcRTV->Release(); } } pcContext->RSSetViewports(dwNumViewports, psViewport); pcBackBuffer->Release(); // clear the depth stencil pcContext->ClearDepthStencilView(m_pcDSVGeometry11, D3D11_CLEAR_DEPTH, 1.0f, 0); } // create the font class if not present nHr = S_OK; if (!m_pcFontSegeo128) { // get base directory std::string strVireioPath = GetBaseDir(); // add file path strVireioPath += "font//"; strVireioPath += m_strFontName; strVireioPath += ".spritefont"; OutputDebugStringA(strVireioPath.c_str()); // create font m_pcFontSegeo128 = new VireioFont(pcDevice, pcContext, strVireioPath.c_str(), 128.0f, 1.0f, nHr, 1); } if (FAILED(nHr)) { delete m_pcFontSegeo128; m_pcFontSegeo128 = nullptr; } // render text (if font present) if (m_pcFontSegeo128) { m_pcFontSegeo128->SetTextAttributes(0.0f, 0.2f, 0.0001f); // set additional tremble for "accepted" event float fDepthTremble = 0.0f; if (m_sMenuControl.eSelectionMovement == MenuControl::SelectionMovement::Accepted) { float fActionTimeElapsed = (fGlobalTime - m_sMenuControl.fActionStartTime) / m_sMenuControl.fActionTime; fDepthTremble = sin(fActionTimeElapsed*PI_F) * -3.0f; } m_pcFontSegeo128->ToRender(pcContext, fGlobalTime, m_sMenuControl.fYOrigin, 30.0f, fDepthTremble); RenderMenu(pcDevice, pcContext); } else OutputDebugString(L"Failed to create font!"); // set back device ApplyStateblock(pcContext, &sStateBlock); if (pcDevice) { pcDevice->Release(); pcDevice = nullptr; } if (pcContext) { pcContext->Release(); pcContext = nullptr; } } #pragma endregion #pragma region draw stereo (optionally) // draw stereo target to screen (optionally) if ((m_eStereoMode) && (eD3DInterface == INTERFACE_IDXGISWAPCHAIN) && (eD3DMethod == METHOD_IDXGISWAPCHAIN_PRESENT)) { // DX 11 if ((m_ppcTexView11[0]) && (m_ppcTexView11[1])) { // get device and context ID3D11Device* pcDevice = nullptr; ID3D11DeviceContext* pcContext = nullptr; if (FAILED(GetDeviceAndContext((IDXGISwapChain*)pThis, &pcDevice, &pcContext))) { // release frame texture+view if (pcDevice) { pcDevice->Release(); pcDevice = nullptr; } if (pcContext) { pcContext->Release(); pcContext = nullptr; } return nullptr; } // get the viewport UINT dwNumViewports = 1; D3D11_VIEWPORT psViewport[16]; pcContext->RSGetViewports(&dwNumViewports, psViewport); // backup all states D3DX11_STATE_BLOCK sStateBlock; CreateStateblock(pcContext, &sStateBlock); // clear all states, set targets ClearContextState(pcContext); // set first active render target - the stored back buffer - get the stored private data view ID3D11Texture2D* pcBackBuffer = nullptr; ((IDXGISwapChain*)pThis)->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pcBackBuffer); ID3D11RenderTargetView* pcView = nullptr; UINT dwSize = sizeof(pcView); pcBackBuffer->GetPrivateData(PDIID_ID3D11TextureXD_RenderTargetView, &dwSize, (void*)&pcView); if (dwSize) { pcContext->OMSetRenderTargets(1, (ID3D11RenderTargetView**)&pcView, m_pcDSVGeometry11); pcView->Release(); } else { // create render target view for the back buffer ID3D11RenderTargetView* pcRTV = nullptr; pcDevice->CreateRenderTargetView(pcBackBuffer, NULL, &pcRTV); if (pcRTV) { pcBackBuffer->SetPrivateDataInterface(PDIID_ID3D11TextureXD_RenderTargetView, pcRTV); pcRTV->Release(); } } pcContext->RSSetViewports(dwNumViewports, psViewport); pcBackBuffer->Release(); // clear the depth stencil pcContext->ClearDepthStencilView(m_pcDSVGeometry11, D3D11_CLEAR_DEPTH, 1.0f, 0); // create all bool bool bAllCreated = true; // create vertex shader if (!m_pcVertexShader11) { if (FAILED(CreateVertexShaderTechnique(pcDevice, &m_pcVertexShader11, &m_pcVertexLayout11, VertexShaderTechnique::PosUV2D))) bAllCreated = false; } // create pixel shader... TODO !! add option to switch output if (!m_pcPixelShader11) { if (FAILED(CreatePixelShaderEffect(pcDevice, &m_pcPixelShader11, PixelShaderTechnique::FullscreenSimple))) bAllCreated = false; } // Create vertex buffer if (!m_pcVertexBuffer11) { if (FAILED(CreateFullScreenVertexBuffer(pcDevice, &m_pcVertexBuffer11))) bAllCreated = false; } // create constant buffer if (!m_pcConstantBufferDirect11) { if (FAILED(CreateGeometryConstantBuffer(pcDevice, &m_pcConstantBufferDirect11, (UINT)sizeof(GeometryConstantBuffer)))) bAllCreated = false; } if (bAllCreated) { // left/right eye for (int nEye = 0; nEye < 2; nEye++) { // Set the input layout pcContext->IASetInputLayout(m_pcVertexLayout11); // Set vertex buffer UINT stride = sizeof(TexturedVertex); UINT offset = 0; pcContext->IASetVertexBuffers(0, 1, &m_pcVertexBuffer11, &stride, &offset); // Set constant buffer, first update it... scale and translate the left and right image D3DXMATRIX sScale; D3DXMatrixScaling(&sScale, 0.5f, 1.0f, 1.0f); D3DXMATRIX sTrans; if (nEye == 0) D3DXMatrixTranslation(&sTrans, -0.5f, 0.0f, 0.0f); else D3DXMatrixTranslation(&sTrans, 0.5f, 0.0f, 0.0f); D3DXMatrixTranspose(&sTrans, &sTrans); D3DXMATRIX sProj; D3DXMatrixMultiply(&sProj, &sTrans, &sScale); pcContext->UpdateSubresource((ID3D11Resource*)m_pcConstantBufferDirect11, 0, NULL, &sProj, 0, 0); pcContext->VSSetConstantBuffers(0, 1, &m_pcConstantBufferDirect11); // Set primitive topology pcContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // set texture pcContext->PSSetShaderResources(0, 1, m_ppcTexView11[nEye]); // set shaders pcContext->VSSetShader(m_pcVertexShader11, 0, 0); pcContext->PSSetShader(m_pcPixelShader11, 0, 0); // Render a triangle pcContext->Draw(6, 0); } } // set back device ApplyStateblock(pcContext, &sStateBlock); if (pcDevice) { pcDevice->Release(); pcDevice = nullptr; } if (pcContext) { pcContext->Release(); pcContext = nullptr; } } } #pragma endregion } return nullptr; }
void GSDevice11::SetupPS(PSSelector sel, const PSConstantBuffer* cb, PSSamplerSelector ssel) { hash_map<uint32, CComPtr<ID3D11PixelShader> >::const_iterator i = m_ps.find(sel); if(i == m_ps.end()) { string str[21]; str[0] = format("%d", sel.fst); str[1] = format("%d", sel.wms); str[2] = format("%d", sel.wmt); str[3] = format("%d", sel.fmt); str[4] = format("%d", sel.aem); str[5] = format("%d", sel.tfx); str[6] = format("%d", sel.tcc); str[7] = format("%d", sel.atst); str[8] = format("%d", sel.fog); str[9] = format("%d", sel.clr1); str[10] = format("%d", sel.fba); str[11] = format("%d", sel.aout); str[12] = format("%d", sel.ltf); str[13] = format("%d", sel.colclip); str[14] = format("%d", sel.date); str[15] = format("%d", sel.spritehack); str[16] = format("%d", sel.tcoffsethack); str[17] = format("%d", sel.point_sampler); str[18] = format("%d", sel.shuffle); str[19] = format("%d", sel.read_ba); str[20] = format("%d", sel.p4_ultrahack); D3D11_SHADER_MACRO macro[] = { {"PS_FST", str[0].c_str()}, {"PS_WMS", str[1].c_str()}, {"PS_WMT", str[2].c_str()}, {"PS_FMT", str[3].c_str()}, {"PS_AEM", str[4].c_str()}, {"PS_TFX", str[5].c_str()}, {"PS_TCC", str[6].c_str()}, {"PS_ATST", str[7].c_str()}, {"PS_FOG", str[8].c_str()}, {"PS_CLR1", str[9].c_str()}, {"PS_FBA", str[10].c_str()}, {"PS_AOUT", str[11].c_str()}, {"PS_LTF", str[12].c_str()}, {"PS_COLCLIP", str[13].c_str()}, {"PS_DATE", str[14].c_str()}, {"PS_SPRITEHACK", str[15].c_str()}, {"PS_TCOFFSETHACK", str[16].c_str()}, {"PS_POINT_SAMPLER", str[17].c_str()}, {"PS_SHUFFLE", str[18].c_str()}, {"PS_READ_BA", str[19].c_str()}, {"PS_P4_ULTRAHACK", str[20].c_str()}, {NULL, NULL}, }; CComPtr<ID3D11PixelShader> ps; CompileShader(IDR_TFX_FX, "ps_main", macro, &ps); m_ps[sel] = ps; i = m_ps.find(sel); } if(m_ps_cb_cache.Update(cb)) { ID3D11DeviceContext* ctx = m_ctx; ctx->UpdateSubresource(m_ps_cb, 0, NULL, cb, 0, 0); } CComPtr<ID3D11SamplerState> ss0, ss1; if(sel.tfx != 4) { if(!(sel.fmt < 3 && sel.wms < 3 && sel.wmt < 3)) { ssel.ltf = 0; } hash_map<uint32, CComPtr<ID3D11SamplerState> >::const_iterator i = m_ps_ss.find(ssel); if(i != m_ps_ss.end()) { ss0 = i->second; } else { D3D11_SAMPLER_DESC sd, af; memset(&sd, 0, sizeof(sd)); af.Filter = theApp.GetConfig("MaxAnisotropy", 0) && !theApp.GetConfig("paltex", 0) ? D3D11_FILTER_ANISOTROPIC : D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; sd.Filter = ssel.ltf ? af.Filter : D3D11_FILTER_MIN_MAG_MIP_POINT; sd.AddressU = ssel.tau ? D3D11_TEXTURE_ADDRESS_WRAP : D3D11_TEXTURE_ADDRESS_CLAMP; sd.AddressV = ssel.tav ? D3D11_TEXTURE_ADDRESS_WRAP : D3D11_TEXTURE_ADDRESS_CLAMP; sd.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; sd.MinLOD = -FLT_MAX; sd.MaxLOD = FLT_MAX; sd.MaxAnisotropy = theApp.GetConfig("MaxAnisotropy", 0); sd.ComparisonFunc = D3D11_COMPARISON_NEVER; m_dev->CreateSamplerState(&sd, &ss0); m_ps_ss[ssel] = ss0; } if(sel.fmt >= 3) { ss1 = m_palette_ss; } } PSSetSamplerState(ss0, ss1, sel.date ? m_rt_ss : NULL); PSSetShader(i->second, m_ps_cb); }
void CPUTMeshDX11::UpdateVerts(void *vertData, int size) { ID3D11DeviceContext *pContext = CPUT_DX11::GetContext(); pContext->UpdateSubresource(mpVertexBuffer, 0, NULL, vertData, size, 0); }
// void ovGetCamImageForUnityNative(void* pTexPtr_Left, void* pTexPtr_Right, int qt, int useAR) CSHARP_EXPORT void ovGetCamImageForUnityNative(void* pTexPtr_Left, void* pTexPtr_Right) { if (g_ovOvrvision == NULL) return; //Get image unsigned char* pLeft = g_ovOvrvision->GetCamImageBGRA(OVR::OV_CAMEYE_LEFT); unsigned char* pRight = g_ovOvrvision->GetCamImageBGRA(OVR::OV_CAMEYE_RIGHT); int length = g_ovOvrvision->GetCamWidth() * g_ovOvrvision->GetCamHeight() * g_ovOvrvision->GetCamPixelsize(); int offsetlen = g_ovOvrvision->GetCamPixelsize(); if (g_DeviceType == 2) { //Direct11 #if SUPPORT_D3D11 ID3D11DeviceContext* ctx = NULL; ID3D11Device* device = (ID3D11Device*)g_D3D11Device; device->GetImmediateContext(&ctx); D3D11_TEXTURE2D_DESC desc_left, desc_right; ID3D11Texture2D* d3dtex_left = (ID3D11Texture2D*)pTexPtr_Left; ID3D11Texture2D* d3dtex_right = (ID3D11Texture2D*)pTexPtr_Right; d3dtex_left->GetDesc(&desc_left); d3dtex_right->GetDesc(&desc_right); ctx->UpdateSubresource(d3dtex_left, 0, NULL, pLeft, g_ovOvrvision->GetCamWidth() * offsetlen, 0); ctx->UpdateSubresource(d3dtex_right, 0, NULL, pRight, g_ovOvrvision->GetCamWidth() * offsetlen, 0); ctx->Release(); #endif } else if (g_DeviceType == 1) { //DirectX9 #if SUPPORT_D3D9 IDirect3DTexture9* d3dtex_left = (IDirect3DTexture9*)pTexPtr_Left; IDirect3DTexture9* d3dtex_right = (IDirect3DTexture9*)pTexPtr_Right; D3DSURFACE_DESC desc_left, desc_right; d3dtex_left->GetLevelDesc(0, &desc_left); d3dtex_left->GetLevelDesc(0, &desc_right); D3DLOCKED_RECT lr; d3dtex_left->LockRect(0, &lr, NULL, 0); memcpy((unsigned char*)lr.pBits, pLeft, length); d3dtex_left->UnlockRect(0); d3dtex_right->LockRect(0, &lr, NULL, 0); memcpy((unsigned char*)lr.pBits, pRight, length); d3dtex_right->UnlockRect(0); #endif } else if (g_DeviceType == 18) { //DirectX12 #if SUPPORT_D3D12 #endif } else if (g_DeviceType == 0) { //OpenGL #if SUPPORT_OPENGL uintptr_t gltex_left = (uintptr_t)pTexPtr_Left; uintptr_t gltex_right = (uintptr_t)pTexPtr_Right; glBindTexture(GL_TEXTURE_2D, (GLuint)gltex_left); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_ovOvrvision->GetCamWidth(), g_ovOvrvision->GetCamHeight(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, pLeft); glBindTexture(GL_TEXTURE_2D, (GLuint)gltex_right); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_ovOvrvision->GetCamWidth(), g_ovOvrvision->GetCamHeight(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, pRight); #endif } else if (g_DeviceType == 17) { //OpenGLCore #if SUPPORT_OPENGL uintptr_t gltex_left = (uintptr_t)pTexPtr_Left; uintptr_t gltex_right = (uintptr_t)pTexPtr_Right; glBindTexture(GL_TEXTURE_2D, gltex_left); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_ovOvrvision->GetCamWidth(), g_ovOvrvision->GetCamHeight(), GL_RGBA, GL_UNSIGNED_BYTE, pLeft); glBindTexture(GL_TEXTURE_2D, gltex_right); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_ovOvrvision->GetCamWidth(), g_ovOvrvision->GetCamHeight(), GL_RGBA, GL_UNSIGNED_BYTE, pRight); #endif } }