void MyD3DAssets::loadPSTexture(int textureIndex) { ID3D11ShaderResourceView *view = nullptr; context->base->PSGetShaderResources(textureIndex, 1, &view); if (view == nullptr) { PSTexture.free(); } else { ID3D11Resource *textureResource = nullptr; view->GetResource(&textureResource); ID3D11Texture2D* texture = nullptr; HRESULT hr = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&texture)); if (texture == nullptr || FAILED(hr)) { PSTexture.free(); g_logger->logErrorFile << "textureResource->QueryInterface failed" << endl; } else { readTexture(texture, PSTexture); texture->Release(); } textureResource->Release(); view->Release(); } }
HRESULT D3D11Object::CreateEnvironmentMap( LPCSTR sFileName, ID3D11ShaderResourceView** ppSRV ) { HRESULT hr = S_OK; ID3D11Resource* pRes = NULL; ID3D11Texture2D* pCubeTexture = NULL; ID3D11ShaderResourceView* pCubeRV = NULL; D3DX11_IMAGE_LOAD_INFO LoadInfo; LoadInfo.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; D3DX11CreateTextureFromFileA( m_pDevice, sFileName, &LoadInfo, NULL, &pRes, NULL ); if( pRes ) { printf("Create environment mapping %s\n", sFileName); D3D11_TEXTURE2D_DESC desc; ZeroMemory( &desc, sizeof( D3D11_TEXTURE2D_DESC ) ); pRes->QueryInterface( __uuidof( ID3D11Texture2D ), ( LPVOID* )&pCubeTexture ); pCubeTexture->GetDesc( &desc ); SAFE_RELEASE( pRes ); D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory( &SRVDesc, sizeof( SRVDesc ) ); SRVDesc.Format = desc.Format; SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; SRVDesc.TextureCube.MipLevels = desc.MipLevels; SRVDesc.TextureCube.MostDetailedMip = 0; hr = m_pDevice->CreateShaderResourceView( pCubeTexture, &SRVDesc, ppSRV ); } return hr; }
HRESULT LoadSkyboxFromFile(LPCSTR file, ID3D11Device* pd3dDevice) { HRESULT hr; D3DX11_IMAGE_INFO SrcInfo; hr = D3DX11GetImageInfoFromFile(file, NULL, &SrcInfo, NULL); D3DX11_IMAGE_LOAD_INFO texLoadInfo; texLoadInfo.Width = SrcInfo.Width; texLoadInfo.Height = SrcInfo.Height; texLoadInfo.Depth = SrcInfo.Depth; texLoadInfo.FirstMipLevel = 0; texLoadInfo.MipLevels = SrcInfo.MipLevels; texLoadInfo.Usage = D3D11_USAGE_DEFAULT; texLoadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE; texLoadInfo.CpuAccessFlags = 0; texLoadInfo.MiscFlags = SrcInfo.MiscFlags; texLoadInfo.Format = SrcInfo.Format; texLoadInfo.Filter = D3DX11_FILTER_TRIANGLE; texLoadInfo.MipFilter = D3DX11_FILTER_TRIANGLE; texLoadInfo.pSrcInfo = &SrcInfo; ID3D11Resource *pRes = NULL; D3DX11CreateTextureFromFile(pd3dDevice, file, &texLoadInfo, NULL, &pRes, NULL); if (pRes) { ID3D11Texture2D* texture; pRes->QueryInterface(__uuidof(ID3D11Texture2D), (LPVOID*)&texture); D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory(&SRVDesc, sizeof(SRVDesc)); SRVDesc.Format = texLoadInfo.Format; SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; SRVDesc.Texture2D.MostDetailedMip = 0; SRVDesc.Texture2D.MipLevels = texLoadInfo.MipLevels; ID3D11ShaderResourceView* textureRview; pd3dDevice->CreateShaderResourceView(texture, &SRVDesc, &textureRview); g_Skybox.OnD3D11CreateDevice(pd3dDevice, 1, texture, textureRview); // Sky box class holds references. //SAFE_RELEASE(texture); //SAFE_RELEASE(textureRview); } SAFE_RELEASE(pRes); return S_OK; }
void Tiles::Init(ID3D11Device* dev, std::wstring fileName, XMFLOAT2 tileSize) { ID3D11Resource* resource = 0; ID3D11Texture2D* texture2D = 0; dev_ = dev; D3DX11CreateShaderResourceViewFromFile(dev, fileName.c_str(), NULL, NULL, &texture_, NULL); texture_->GetResource(&resource); resource->QueryInterface(&texture2D); D3D11_TEXTURE2D_DESC desc; texture2D->GetDesc(&desc); TextureHeight = desc.Height / tileSize.y; TextureWidth = desc.Width / tileSize.x; }
void Entity::LoadTexture(ID3D11Device* device , std::wstring texFilename) { HR(D3DX11CreateShaderResourceViewFromFile(device, texFilename.c_str(), 0, 0, &mTexSRV, 0)); //GRAB TEXTURES WIDTH AND HEIGHT .. USED LATER FOR ANIMATION PURPOSES ID3D11Resource* resource = 0; ID3D11Texture2D* texture2D = 0; mTexSRV->GetResource(&resource); resource->QueryInterface(&texture2D); D3D11_TEXTURE2D_DESC desc; texture2D->GetDesc(&desc); mTexWidth = desc.Width; mTexHeight = desc.Height; ReleaseCOM(resource); ReleaseCOM(texture2D); }
//-- SetRenderTarget ---------------------------------------------------------- // //----------------------------------------------------------------------------- void Renderer::SetRenderTarget(TextureDX* texture) { if (texture == NULL || texture->GetRenderTarget() == NULL) return; ID3D11Texture2D* pTexture = texture->GetTexture(); D3D11_TEXTURE2D_DESC txDesc, dtxDesc; pTexture->GetDesc(&txDesc); ID3D11Resource* pResource = NULL; ID3D11Texture2D* pDepthTexture = NULL; g_depthView->GetResource(&pResource); pResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pDepthTexture)); pDepthTexture->GetDesc(&dtxDesc); pDepthTexture->Release(); pResource->Release(); // depth view dimension must be equals render target dimension if (txDesc.Width != dtxDesc.Width || txDesc.Height != dtxDesc.Height) { SAFE_RELEASE( g_depthView ); CD3D11_TEXTURE2D_DESC depthBufferDesc(DXGI_FORMAT_D24_UNORM_S8_UINT, txDesc.Width, txDesc.Height, 1, 1, D3D11_BIND_DEPTH_STENCIL); m_pD3D11Device->CreateTexture2D(&depthBufferDesc, NULL, &pDepthTexture); CD3D11_DEPTH_STENCIL_VIEW_DESC viewDesc(D3D11_DSV_DIMENSION_TEXTURE2D); m_pD3D11Device->CreateDepthStencilView(pDepthTexture, &viewDesc, &g_depthView); pDepthTexture->Release(); } else m_pD3D11Contex->ClearDepthStencilView(g_depthView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0, 0); ID3D11RenderTargetView* rtView = texture->GetRenderTarget(); // change view port to render target dimension like dx9 does CD3D11_VIEWPORT vp(pTexture, rtView); m_pD3D11Contex->RSSetViewports(1, &vp); m_pD3D11Contex->OMSetRenderTargets(1, &rtView, g_depthView); } // SetRenderTarget
void myD3D11DeviceContext::readRenderTarget(Bitmap &result) { ID3D11RenderTargetView *view; assets->context->base->OMGetRenderTargets(1, &view, nullptr); ID3D11Resource *resource; view->GetResource(&resource); ID3D11Texture2D *inputTexture; HRESULT hr = resource->QueryInterface(__uuidof(ID3D11Texture2D), (void **)&inputTexture); if (FAILED(hr)) { g_logger->logErrorFile << "QueryInterface failed to cast to a texture" << endl; } else { readTexture(inputTexture, result); } view->Release(); }
bool PerlinFire::LoadTexture2D(ID3D11Device * pd3dDevice, LPCWSTR fileName, ID3D11Texture2D ** tex, ID3D11ShaderResourceView ** texRV) { HRESULT hr; ID3D11Resource * pRes = NULL; D3DX11CreateTextureFromFile( pd3dDevice, fileName, NULL, NULL, &pRes, &hr ); pRes->QueryInterface( __uuidof(ID3D11Texture2D), (LPVOID*) tex); D3D11_TEXTURE2D_DESC desc; (*tex)->GetDesc(& desc); D3D11_SHADER_RESOURCE_VIEW_DESC descSRV; ZeroMemory( &descSRV, sizeof(descSRV) ); descSRV.Format = desc.Format; descSRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; descSRV.Texture2D.MipLevels = desc.MipLevels; pd3dDevice->CreateShaderResourceView( *tex, &descSRV, texRV ); pRes->Release(); return true; }
bool Texture::LoadTexture(const std::wstring& fileName, ID3D11Device* device) { DirectX::CreateWICTextureFromFile(device, fileName.c_str(), nullptr, &mTexture); if (mTexture != nullptr) { ID3D11Resource* res = nullptr; ID3D11Texture2D* texture2d = nullptr; D3D11_TEXTURE2D_DESC desc; mTexture->GetResource(&res); res->QueryInterface(&texture2d); texture2d->GetDesc(&desc); mTextureDimensions.x = (float)desc.Width; mTextureDimensions.y = (float)desc.Height; ZEPHYR_RELEASEOBJECT(res); ZEPHYR_RELEASEOBJECT(texture2d); return true; } return false; }
ModelDefinition *ModelFactory::create2D_Model(ID3D11ShaderResourceView *p_Texture) { ModelDefinition *model = new ModelDefinition(); ID3D11Resource *resource; ID3D11Texture2D *texture; D3D11_TEXTURE2D_DESC textureDesc; p_Texture->GetResource(&resource); resource->QueryInterface(&texture); texture->GetDesc(&textureDesc); Vector2 halfSize(textureDesc.Width * 0.5f, textureDesc.Height * 0.5f); SAFE_RELEASE(texture); SAFE_RELEASE(resource); create2D_VertexBuffer(model, halfSize); model->diffuseTexture.push_back(make_pair(std::string("Text"), p_Texture)); model->materialSets.resize(1); model->materialSets[0].first = "default"; model->materialSets[0].second.push_back(ModelDefinition::Material(0, 6, 0)); model->is2D = true; model->isAnimated = false; return model; }
// ---------------------------------------------------------------------------------------------- Texture* TextureLib::LoadEmbeddedTexture(ID3D11Device* device, const wchar_t* name) { assert(s_Inst != NULL); Imple* pImple = s_Inst->m_pImple; typedef std::pair<std::wstring, Texture*> NameTexPair; const wchar_t* resType = L"Texture"; HRESULT hr = E_FAIL; Texture* tex = NULL; ID3D11Resource* dxresource = NULL; ID3D11ShaderResourceView* dxTexView = NULL; uint32_t resSize = 0; uint8_t* data = (uint8_t*)ResUtil::LoadResource(resType, name, &resSize); hr = CreateWICTextureFromMemory(device, NULL, data, resSize, &dxresource, &dxTexView); free(data); if (!Logger::IsFailureLog(hr, L"Error loading %s\n", name)) { D3D11_RESOURCE_DIMENSION resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN; dxresource->GetType(&resDim); assert(resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D); ID3D11Texture2D* dxTex = NULL; hr = dxresource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&dxTex); dxresource->Release(); assert(dxTex); tex = new Texture(dxTex, dxTexView); auto insertResult = pImple->m_textures.insert(NameTexPair(name, tex)); assert(insertResult.second); } return tex; }
void TextureLib::InitInstance(ID3D11Device* device) { s_Inst = new TextureLib(); Imple* pImple = s_Inst->m_pImple; // color format ABGR pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080); pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true); pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080); pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF); pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000); pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00); pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF); pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF); typedef std::pair<std::wstring, Texture*> NameTexPair; uint32_t resCount = ResUtil::ResourceCount(); for(uint32_t i = 0; i < resCount; i++) { const wchar_t* resName = ResUtil::GetResourceName(i); const std::wstring ext = FileUtils::GetExtensionLower(resName); HRESULT hr = E_FAIL; Texture* tex = NULL; ID3D11Resource* dxresource = NULL; ID3D11ShaderResourceView* dxTexView = NULL; uint32_t resSize = 0; if(ext == L".dds") { uint8_t* data = ResUtil::LoadResource(resName,&resSize); if(resSize == 0) continue; hr = CreateDDSTextureFromMemory( device, data, resSize, &dxresource, &dxTexView); free(data); } else if(ext == L".png" || ext == L".bmp" || ext == L".jpeg") { uint8_t* data = ResUtil::LoadResource(resName,&resSize); if(resSize == 0) continue; hr = CreateWICTextureFromMemory( device, NULL, data, resSize, &dxresource, &dxTexView); free(data); } if (Logger::IsFailureLog(hr, L"Error loading %s\n", resName)) { continue; } D3D11_RESOURCE_DIMENSION resType = D3D11_RESOURCE_DIMENSION_UNKNOWN; dxresource->GetType( &resType ); assert( resType == D3D11_RESOURCE_DIMENSION_TEXTURE2D); ID3D11Texture2D* dxTex = NULL; hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex ); dxresource->Release(); assert(dxTex); tex = new Texture(dxTex,dxTexView); auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex)); assert(insertResult.second); } }
void TextureLib::InitInstance(ID3D11Device* device) { s_Inst = new TextureLib(); Imple* pImple = s_Inst->m_pImple; pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080, false,true); pImple->m_defaultTextures[TextureType::DIFFUSE]->SetTextureType(TextureType::DIFFUSE); pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true); pImple->m_defaultTextures[TextureType::Cubemap]->SetTextureType(TextureType::Cubemap); pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080); pImple->m_defaultTextures[TextureType::NORMAL]->SetTextureType(TextureType::NORMAL); pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF); pImple->m_defaultTextures[TextureType::LIGHT]->SetTextureType(TextureType::LIGHT); pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000); pImple->m_defaultTextures[TextureType::SPEC]->SetTextureType(TextureType::SPEC); pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00); pImple->m_defaultTextures[TextureType::BlankMask]->SetTextureType(TextureType::BlankMask); pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF); pImple->m_defaultTextures[TextureType::FullMask]->SetTextureType(TextureType::FullMask); pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF); typedef std::pair<std::wstring, Texture*> NameTexPair; const wchar_t* resName = L"Light.png"; const wchar_t* resType = L"Texture"; HRESULT hr = E_FAIL; Texture* tex = NULL; ID3D11Resource* dxresource = NULL; ID3D11ShaderResourceView* dxTexView = NULL; uint32_t resSize = 0; uint8_t* data = (uint8_t*)ResUtil::LoadResource(resType, resName,&resSize); hr = CreateWICTextureFromMemory( device, NULL, data, resSize, &dxresource, &dxTexView); free(data); if (!Logger::IsFailureLog(hr, L"Error loading %s\n", resName)) { D3D11_RESOURCE_DIMENSION resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN; dxresource->GetType( &resDim ); assert( resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D); ID3D11Texture2D* dxTex = NULL; hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex ); dxresource->Release(); assert(dxTex); tex = new Texture(dxTex,dxTexView); auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex)); assert(insertResult.second); } }
bool CScreenshotSurface::capture() { #if defined(TARGET_RASPBERRY_PI) g_RBP.GetDisplaySize(m_width, m_height); m_buffer = g_RBP.CaptureDisplay(m_width, m_height, &m_stride, true, false); if (!m_buffer) return false; #elif defined(HAS_DX) g_graphicsContext.Lock(); if (g_application.m_pPlayer->IsPlayingVideo()) { #ifdef HAS_VIDEO_PLAYBACK g_renderManager.SetupScreenshot(); #endif } g_application.RenderNoPresent(); g_Windowing.FinishCommandList(); ID3D11DeviceContext* pImdContext = g_Windowing.GetImmediateContext(); ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); ID3D11Device* pDevice = g_Windowing.Get3D11Device(); ID3D11RenderTargetView* pRTView = nullptr; pContext->OMGetRenderTargets(1, &pRTView, nullptr); if (pRTView == nullptr) return false; ID3D11Resource *pRTResource = nullptr; pRTView->GetResource(&pRTResource); SAFE_RELEASE(pRTView); ID3D11Texture2D* pCopyTexture = nullptr; ID3D11Texture2D* pRTTexture = nullptr; HRESULT hr = pRTResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pRTTexture)); SAFE_RELEASE(pRTResource); if (FAILED(hr)) return false; D3D11_TEXTURE2D_DESC desc; pRTTexture->GetDesc(&desc); desc.Usage = D3D11_USAGE_STAGING; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.BindFlags = 0; if (SUCCEEDED(pDevice->CreateTexture2D(&desc, nullptr, &pCopyTexture))) { // take copy pImdContext->CopyResource(pCopyTexture, pRTTexture); D3D11_MAPPED_SUBRESOURCE res; if (SUCCEEDED(pImdContext->Map(pCopyTexture, 0, D3D11_MAP_READ, 0, &res))) { m_width = desc.Width; m_height = desc.Height; m_stride = res.RowPitch; m_buffer = new unsigned char[m_height * m_stride]; memcpy(m_buffer, res.pData, m_height * m_stride); pImdContext->Unmap(pCopyTexture, 0); } else CLog::Log(LOGERROR, "%s: MAP_READ failed.", __FUNCTION__); SAFE_RELEASE(pCopyTexture); } SAFE_RELEASE(pRTTexture); g_graphicsContext.Unlock(); #elif defined(HAS_GL) || defined(HAS_GLES) g_graphicsContext.BeginPaint(); if (g_application.m_pPlayer->IsPlayingVideo()) { #ifdef HAS_VIDEO_PLAYBACK g_renderManager.SetupScreenshot(); #endif } g_application.RenderNoPresent(); #ifndef HAS_GLES glReadBuffer(GL_BACK); #endif //get current viewport GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); m_width = viewport[2] - viewport[0]; m_height = viewport[3] - viewport[1]; m_stride = m_width * 4; unsigned char* surface = new unsigned char[m_stride * m_height]; //read pixels from the backbuffer #if HAS_GLES == 2 glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)surface); #else glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)surface); #endif g_graphicsContext.EndPaint(); //make a new buffer and copy the read image to it with the Y axis inverted m_buffer = new unsigned char[m_stride * m_height]; for (int y = 0; y < m_height; y++) { #ifdef HAS_GLES // we need to save in BGRA order so XOR Swap RGBA -> BGRA unsigned char* swap_pixels = surface + (m_height - y - 1) * m_stride; for (int x = 0; x < m_width; x++, swap_pixels+=4) { std::swap(swap_pixels[0], swap_pixels[2]); } #endif memcpy(m_buffer + y * m_stride, surface + (m_height - y - 1) *m_stride, m_stride); } delete [] surface; #if defined(HAS_LIBAMCODEC) // Captures the current visible videobuffer and blend it into m_buffer (captured overlay) CScreenshotAML::CaptureVideoFrame(m_buffer, m_width, m_height); #endif #ifdef HAS_IMXVPU // Captures the current visible framebuffer page and blends it into the // captured GL overlay g_IMXContext.CaptureDisplay(m_buffer, m_width, m_height); #endif #else //nothing to take a screenshot from return false; #endif return true; }
/** * Callback to draw things in world space. ***/ void OSVR_DirectMode::DrawWorld(void* userData, osvr::renderkit::GraphicsLibrary cLibrary, osvr::renderkit::RenderBuffer cBuffers, osvr::renderkit::OSVR_ViewportDescription sViewport, OSVR_PoseState pose, osvr::renderkit::OSVR_ProjectionMatrix sProjection, OSVR_TimeValue deadline) { static int nEye = 0; static float fAspect = 1.0f; // Make sure our pointers are filled in correctly. The config file selects // the graphics library to use, and may not match our needs. if (cLibrary.D3D11 == nullptr) { std::cerr << "SetupDisplay: No D3D11 GraphicsLibrary" << std::endl; return; } if (cBuffers.D3D11 == nullptr) { std::cerr << "SetupDisplay: No D3D11 RenderBuffer" << std::endl; return; } // auto pcContext = cLibrary.D3D11->context; auto pcDevice = cLibrary.D3D11->device; auto pcContext = cLibrary.D3D11->context; // create all bool bool bAllCreated = true; // create vertex shader if (!m_pcVertexShader11) { if (FAILED(Create2DVertexShader(pcDevice, &m_pcVertexShader11, &m_pcVertexLayout11))) { OutputDebugString(L"FAILED"); bAllCreated = false; } } // create pixel shader... if (!m_pcPixelShader11) { if (FAILED(CreatePixelShaderEffect(pcDevice, &m_pcPixelShader11, PixelShaderTechnique::FullscreenGammaCorrection))) bAllCreated = false; } // Create vertex buffer if (!m_pcVertexBuffer11) { if (FAILED(CreateFullScreenVertexBuffer(pcDevice, &m_pcVertexBuffer11))) bAllCreated = false; } // create constant buffer if (!m_pcConstantBufferDirect11) { if (FAILED(CreateMatrixConstantBuffer(pcDevice, &m_pcConstantBufferDirect11))) bAllCreated = false; } // sampler ? if (!m_pcSamplerState) { bAllCreated = false; } if ((bAllCreated) && (m_sStereoTextureViews.m_ppcTexView11[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); // get orthographic matrix from projection and normalize it by its width (since we use a fullscreen shader here) float afProjectionD3D[16]; osvr::renderkit::OSVR_Projection_to_D3D(afProjectionD3D, sProjection); D3DXMATRIX sProj(afProjectionD3D); // due to the aspect ratio (90° horizontal, 90° vertical) of the HDK we adjust the screen by // the height, not by the width... in this case we need to set a higher FOV by following formular: // V = 2 * arctan( tan(H / 2) * aspectratio ) - so we get V 90° and H 121° sProj.m[0][0] = sProj.m[0][0] * fAspect; // < incorporate game screen aspect ratio; sProj.m[0][1] = 0.0f; sProj.m[0][3] = sProj.m[0][2]; sProj.m[0][2] = 0.0f; sProj.m[1][0] = 0.0f; sProj.m[1][1] = sProj.m[1][1]; sProj.m[1][3] = sProj.m[1][2]; sProj.m[1][2] = 0.0f; sProj.m[2][0] = 0.0f; sProj.m[2][1] = 0.0f; sProj.m[2][2] = 1.0f; // 1.0f here... fullscreen shader ! sProj.m[2][3] = 0.0f; sProj.m[3][0] = 0.0f; sProj.m[3][1] = 0.0f; sProj.m[3][2] = 0.0f; sProj.m[3][3] = 1.0f; // zoom out ? if (m_pbZoomOut) { if (*m_pbZoomOut) { sProj.m[0][0] /= 2.0f; sProj.m[1][1] /= 2.0f; } } // Set constant buffer, first update it... scale and translate the left and right image 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); // texture connected ? if ((m_sStereoTextureViews.m_ppcTexView11[nEye]) && (*m_sStereoTextureViews.m_ppcTexView11[nEye])) { if (m_eMethod == OSVR_DirectModeMethods::OSVR_D3D11_use_Game_Device) { // set texture, sampler state pcContext->PSSetShaderResources(0, 1, m_sStereoTextureViews.m_ppcTexView11[nEye]); pcContext->PSSetSamplers(0, 1, &m_pcSamplerState); } else { ID3D11Resource* pcResource = nullptr; (*m_sStereoTextureViews.m_ppcTexView11[nEye])->GetResource(&pcResource); if (!m_sStereoTextureCopies.m_pcTex11Copy[nEye]) { // get the description and create the copy texture D3D11_TEXTURE2D_DESC sDesc; ((ID3D11Texture2D*)pcResource)->GetDesc(&sDesc); sDesc.MiscFlags |= D3D11_RESOURCE_MISC_SHARED; sDesc.BindFlags |= D3D11_BIND_SHADER_RESOURCE; if (FAILED(((ID3D11Device*)m_pcGameDevice)->CreateTexture2D(&sDesc, NULL, (ID3D11Texture2D**)&m_sStereoTextureCopies.m_pcTex11Copy[nEye]))) { OutputDebugString(L"StereoSplitterDX10 : Failed to create twin texture !"); return; } // aspect ratio fAspect = (float)sDesc.Width / (float)sDesc.Height; // TODO !! DX9 // DX10 !! // get shared handle IDXGIResource* pcDXGIResource(NULL); m_sStereoTextureCopies.m_pcTex11Copy[nEye]->QueryInterface(__uuidof(IDXGIResource), (void**)&pcDXGIResource); HANDLE sharedHandle; if (pcDXGIResource) { pcDXGIResource->GetSharedHandle(&sharedHandle); pcDXGIResource->Release(); } else OutputDebugString(L"Failed to query IDXGIResource."); // open the shared handle with the temporary device ID3D11Resource* pcResourceShared; pcDevice->OpenSharedResource(sharedHandle, __uuidof(ID3D11Resource), (void**)(&pcResourceShared)); if (pcResourceShared) { pcResourceShared->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&m_sStereoFrameTextures.m_pcFrameTexture[nEye])); pcResourceShared->Release(); } else OutputDebugString(L"Could not open shared resource."); // create shader resource view if (m_sStereoFrameTextures.m_pcFrameTexture[nEye]) { D3D11_SHADER_RESOURCE_VIEW_DESC sDescSRV; ZeroMemory(&sDescSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC)); sDescSRV.Format = sDesc.Format; sDescSRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; sDescSRV.Texture2D.MostDetailedMip = 0; sDescSRV.Texture2D.MipLevels = 1; if (FAILED(pcDevice->CreateShaderResourceView(m_sStereoFrameTextures.m_pcFrameTexture[nEye], &sDescSRV, &m_sSteroFrameTextureSRViews.m_pcFrameTextureSRView[nEye]))) OutputDebugString(L"Failed to create shader resource view."); } else OutputDebugString(L"No Texture available."); } else { // copy the frame tex to shared texture m_pcGameDeviceContext->CopyResource(m_sStereoTextureCopies.m_pcTex11Copy[nEye], pcResource); if (pcResource) pcResource->Release(); // set texture, sampler state pcContext->PSSetShaderResources(0, 1, &m_sSteroFrameTextureSRViews.m_pcFrameTextureSRView[nEye]); pcContext->PSSetSamplers(0, 1, &m_pcSamplerState); } } } // set shaders pcContext->VSSetShader(m_pcVertexShader11, 0, 0); pcContext->PSSetShader(m_pcPixelShader11, 0, 0); // Render a triangle pcContext->Draw(6, 0); // switch eye for next call nEye = !nEye; } }
void IDXGISwapChainNew::preUpdateBB(UINT *width, UINT *height) { dbg("dxgi_sc: preUpdateBB"); int rrx = config.main.renderResolution.x; int rry = config.main.renderResolution.y; if(*width == rrx && *height == rry) { dbg("dxgi_sc: Multihead swapchain mode detected"); HEAD *h = config.getPrimaryHead(); *width = h->screenMode.x; *height = h->screenMode.y; // Set mouse hook on application focus window ihGlobal.setHWND(win); SoftTHActive++; h->hwnd = win; // Create new backbuffer dbg("dxgi_sc: Creating new backbuffer"); // TODO: format #if defined(SOFTTHMAIN) || defined(D3D11) if(dev11) { // Create the full backbuffer render texture dbg("dxgi_sc: Creating FULL backbuffer for D3D11 Device"); //CD3D10_TEXTURE2D_DESC d(DXGI_FORMAT_R8G8B8A8_UNORM, rrx, rry, 1, 1, D3D10_BIND_RENDER_TARGET, D3D10_USAGE_DEFAULT, NULL); CD3D11_TEXTURE2D_DESC d(DXGI_FORMAT_R8G8B8A8_UNORM, rrx, rry, 1, 1, D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT, NULL); newbbDesc11 = d; if(dev11->CreateTexture2D(&newbbDesc11, NULL, &newbb11) != S_OK) dbg("dxgi_sc: CreateTexture2D failed :("), exit(0); // Initialize outputs numDevs = config.getNumAdditionalHeads(); dbg("dxgi_sc: Initializing %d outputs", numDevs); int logoStopTime = GetTickCount() + 4000; bool fpuPreserve = true; // TODO: does this exist in d3d10? outDevs11 = new OUTDEVICE11[numDevs]; stagingOuts11 = new STAGINGOUT11[numDevs]; for(int i=0;i<numDevs;i++) { OUTDEVICE11 *o = &outDevs11[i]; STAGINGOUT11 *so = &stagingOuts11[i]; so->headID = i+1; so->devID = h->devID; so->stagingSurf = NULL; // Create the output device HEAD *h = config.getHead(i); dbg("dxgi_sc: Initializing Head %d (DevID: %d)",i+1,h->devID); o->output = new outDirect3D11(h->devID, h->screenMode.x, h->screenMode.y, h->transportRes.x, h->transportRes.y, win); o->cfg = h; bool local = h->transportMethod==OUTMETHOD_LOCAL; if (!local) has_nonlocal = true; dbg("dxgi_sc: Head %d is %s", i+1, local?"local":"non-local"); // Create a main staging buffer sized for this head if non-local if (!local) { dbg("dxgi_sc: Creating a main non-local staging buffer for Head %d (DevID %d)", i + 1, h->devID); CD3D11_TEXTURE2D_DESC dss(DXGI_FORMAT_R8G8B8A8_UNORM, h->transportRes.x, h->transportRes.y, 1, 1, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE, 1, 0, 0); /*DWORD32 *fillbuf = new DWORD32[h->transportRes.x*h->transportRes.y]; for (int ii = 0; ii < h->transportRes.y; ii++) for (int jj = 0; jj < h->transportRes.x; jj++) { if ((ii&32)==(jj&32)) fillbuf[ii*h->transportRes.x + jj] = (DWORD32) 0x0000ff00; else fillbuf[ii*h->transportRes.x + jj] = (DWORD32) 0xffffffff; } D3D11_SUBRESOURCE_DATA fillsr; ZeroMemory(&fillsr, sizeof(fillsr)); fillsr.pSysMem = (void *)fillbuf; fillsr.SysMemPitch = h->transportRes.x * 4; fillsr.SysMemSlicePitch = h->transportRes.x * h->transportRes.y * 4; if (dev11->CreateTexture2D(&dss, &fillsr, &so->stagingSurf) != S_OK) {*/ if (dev11->CreateTexture2D(&dss, NULL, &so->stagingSurf) != S_OK) { dbg("dxgi_sc: CreateTexture2D staged for Head %d (DevID %d) failed :(",i+1,h->devID), exit(0); } } // Create shared surfaces HANDLE sha = o->output->GetShareHandle(); if(sha) { o->localSurf = NULL; { // Open surfA share handle ID3D11Resource *tr; if (o->cfg->transportMethod == OUTMETHOD_LOCAL) { // Local output if (dev11->OpenSharedResource(sha, __uuidof(ID3D11Resource), (void**)(&tr)) != S_OK) dbg("dxgi_sc: Local OpenSharedResource A failed!"), exit(0); } else { // Non-local output if (o->output->dev->OpenSharedResource(sha, __uuidof(ID3D11Resource), (void**)(&tr)) != S_OK) dbg("dxgi_sc: Non-local OpenSharedResource A failed!"), exit(0); } if(tr->QueryInterface(__uuidof(ID3D11Texture2D), (void**)(&o->localSurf)) != S_OK) dbg("dxgi_sc: Shared surface QueryInterface failed!"), exit(0); tr->Release(); } dbg("dxgi_sc: Opened share handles"); } else dbg("dxgi_sc: ERROR: Head %d: No share handle!", i+1), exit(0); } // Create the full backbuffer staged texture if we have non-local head /*if (has_nonlocal) { CD3D11_TEXTURE2D_DESC ds(DXGI_FORMAT_R8G8B8A8_UNORM, rrx, rry, 1, 1, NULL, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ, 1, 0, D3D11_RESOURCE_MISC_SHARED); newbbDesc11staged = ds; if(dev11->CreateTexture2D(&newbbDesc11staged, NULL, &newbb11staged) != S_OK) dbg("dxgi_sc: CreateTexture2D staged failed :("), exit(0); }*/ } #endif #ifdef SOFTTHMAIN else #endif #if defined(SOFTTHMAIN) || defined(D3D10_1) if(dev10_1) { dbg("dxgi_sc: Creating backbuffer for D3D10.1 Device"); CD3D10_TEXTURE2D_DESC d(DXGI_FORMAT_R8G8B8A8_UNORM, rrx, rry, 1, 1, D3D10_BIND_RENDER_TARGET, D3D10_USAGE_DEFAULT, NULL); newbbDesc10 = d; if(dev10_1->CreateTexture2D(&newbbDesc10, NULL, &newbb10) != S_OK) dbg("dxgi_sc: CreateTexture2D failed :("), exit(0); // Initialize outputs numDevs = config.getNumAdditionalHeads(); dbg("dxgi_sc: Initializing %d outputs", numDevs); int logoStopTime = GetTickCount() + 4000; bool fpuPreserve = true; // TODO: does this exist in d3d10? outDevs10 = new OUTDEVICE10[numDevs]; for(int i=0;i<numDevs;i++) { OUTDEVICE10 *o = &outDevs10[i]; // Create the output device HEAD *h = config.getHead(i); bool local = h->transportMethod==OUTMETHOD_LOCAL; dbg("dxgi_sc: Initializing head %d (DevID: %d, %s)...", i+1, h->devID, local?"local":"non-local"); o->output = new outDirect3D10(h->devID, h->screenMode.x, h->screenMode.y, h->transportRes.x, h->transportRes.y, win); o->cfg = h; // Create shared surfaces HANDLE sha = o->output->GetShareHandle(); if(sha) { o->localSurf = NULL; { // Open surfA share handle ID3D10Resource *tr; if(dev10_1->OpenSharedResource(sha, __uuidof(ID3D10Resource), (void**)(&tr)) != S_OK) dbg("dxgi_sc: OpenSharedResource A failed!"), exit(0); if(tr->QueryInterface(__uuidof(ID3D10Texture2D), (void**)(&o->localSurf)) != S_OK) dbg("dxgi_sc: Shared surface QueryInterface failed!"), exit(0); tr->Release(); } dbg("dxgi_sc: Opened share handles"); } else dbg("dxgi_sc: ERROR: Head %d: No share handle!", i+1), exit(0); } } #endif #ifdef SOFTTHMAIN else #endif #if defined(SOFTTHMAIN) || defined(D3D10) if(dev10) { dbg("dxgi_sc: Creating backbuffer for D3D10 Device"); CD3D10_TEXTURE2D_DESC d(DXGI_FORMAT_R8G8B8A8_UNORM, rrx, rry, 1, 1, D3D10_BIND_RENDER_TARGET, D3D10_USAGE_DEFAULT, NULL); newbbDesc10 = d; if(dev10->CreateTexture2D(&newbbDesc10, NULL, &newbb10) != S_OK) dbg("dxgi_sc: CreateTexture2D failed :("), exit(0); // Initialize outputs numDevs = config.getNumAdditionalHeads(); dbg("dxgi_sc: Initializing %d outputs", numDevs); int logoStopTime = GetTickCount() + 4000; bool fpuPreserve = true; // TODO: does this exist in d3d10? outDevs10 = new OUTDEVICE10[numDevs]; for(int i=0;i<numDevs;i++) { OUTDEVICE10 *o = &outDevs10[i]; // Create the output device HEAD *h = config.getHead(i); bool local = h->transportMethod==OUTMETHOD_LOCAL; dbg("dxgi_sc: Initializing head %d (DevID: %d, %s)...", i+1, h->devID, local?"local":"non-local"); o->output = new outDirect3D10(h->devID, h->screenMode.x, h->screenMode.y, h->transportRes.x, h->transportRes.y, win); o->cfg = h; // Create shared surfaces HANDLE sha = o->output->GetShareHandle(); if(sha) { o->localSurf = NULL; { // Open surfA share handle ID3D10Resource *tr; if(dev10->OpenSharedResource(sha, __uuidof(ID3D10Resource), (void**)(&tr)) != S_OK) dbg("dxgi_sc: OpenSharedResource A failed!"), exit(0); if(tr->QueryInterface(__uuidof(ID3D10Texture2D), (void**)(&o->localSurf)) != S_OK) dbg("dxgi_sc: Shared surface QueryInterface failed!"), exit(0); tr->Release(); } dbg("dxgi_sc: Opened share handles"); } else dbg("dxgi_sc: ERROR: Head %d: No share handle!", i+1), exit(0); } } #endif } else { dbg("dxgi_sc: Singlehead swapchain mode"); SoftTHActive--; if(dev11) { if(newbb11) SAFE_RELEASE_LAST(newbb11); newbb11 = NULL; } else if(dev10 || dev10_1) { if(newbb10) SAFE_RELEASE_LAST(newbb10); newbb10 = NULL; } } }