void CGUIShaderDX::ApplyChanges(void) { ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); D3D11_MAPPED_SUBRESOURCE res; if (m_bIsWVPDirty) { if (SUCCEEDED(pContext->Map(m_pWVPBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &res))) { XMMATRIX worldView = XMMatrixMultiply(m_cbWorldViewProj.world, m_cbWorldViewProj.view); XMMATRIX worldViewProj = XMMatrixMultiplyTranspose(worldView, m_cbWorldViewProj.projection); cbWorld* buffer = (cbWorld*)res.pData; buffer->wvp = worldViewProj; buffer->blackLevel = (g_Windowing.UseLimitedColor() ? 16.f / 255.f : 0.f); buffer->colorRange = (g_Windowing.UseLimitedColor() ? (235.f - 16.f) / 255.f : 1.0f); pContext->Unmap(m_pWVPBuffer, 0); m_bIsWVPDirty = false; } } // update view port buffer if (m_bIsVPDirty) { if (SUCCEEDED(pContext->Map(m_pVPBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &res))) { *(cbViewPort*)res.pData = m_cbViewPort; pContext->Unmap(m_pVPBuffer, 0); m_bIsVPDirty = false; } } }
void TerrainController::init(ID3D11Device* device) { width = Config::getValue<int>(ConfigKeys::terrainWidth); length = Config::getValue<int>(ConfigKeys::terrainLength); position = Vector3(width / -2.0f, 0, length / -2.0f); bumpiness = Config::getValue<float>(ConfigKeys::terrainBumpiness); quality = Config::getValue<float>(ConfigKeys::terrainQuality); D3DX11_IMAGE_LOAD_INFO loadInfo; ZeroMemory( &loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO) ); loadInfo.Usage = D3D11_USAGE_STAGING; loadInfo.Format = DXGI_FORMAT_FROM_FILE; loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ; ID3D11Texture2D *heightmapTexture, *normalmapTexture; D3DX11CreateTextureFromFile( device, L"Content/Textures/Terrain/Heightmap.png", &loadInfo, NULL, (ID3D11Resource**)(&heightmapTexture), NULL ); D3DX11CreateTextureFromFile( device, L"Content/Textures/Terrain/Normalmap.png", &loadInfo, NULL, (ID3D11Resource**)(&normalmapTexture), NULL ); heightmapTexture->GetDesc(&heightmapDesc); normalmapTexture->GetDesc(&normalmapDesc); std::vector<HeightmapFormat> hData = std::vector<HeightmapFormat>(heightmapDesc.Width * heightmapDesc.Height); std::vector<NormalmapFormat> nData = std::vector<NormalmapFormat>(normalmapDesc.Width * normalmapDesc.Height); ID3D11DeviceContext* deviceContext; device->GetImmediateContext(&deviceContext); D3D11_MAPPED_SUBRESOURCE heightmapData; deviceContext->Map(heightmapTexture, 0, D3D11_MAP_READ, 0, &heightmapData); memcpy((void*)&hData[0], heightmapData.pData, heightmapDesc.Width * heightmapDesc.Height * sizeof(HeightmapFormat)); deviceContext->Unmap(heightmapTexture, 0); D3D11_MAPPED_SUBRESOURCE normalmapData; deviceContext->Map(normalmapTexture, 0, D3D11_MAP_READ, 0, &normalmapData); memcpy((void*)&nData[0], normalmapData.pData, normalmapDesc.Width * normalmapDesc.Height * sizeof(NormalmapFormat)); deviceContext->Unmap(normalmapTexture, 0); heightData = HeightData(heightmapDesc.Width * heightmapDesc.Height); normalData = NormalData(normalmapDesc.Width * normalmapDesc.Height); std::transform(hData.begin(), hData.end(), heightData.begin(), &TerrainController::convertHeight); std::transform(nData.begin(), nData.end(), normalData.begin(), &TerrainController::convertNormal); heightmapTexture->Release(); normalmapTexture->Release(); deviceContext->Release(); }
gl::Error Image11::map(const gl::Context *context, D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map) { // We must recover from the TextureStorage if necessary, even for D3D11_MAP_WRITE. ANGLE_TRY(recoverFromAssociatedStorage(context)); const TextureHelper11 *stagingTexture = nullptr; unsigned int subresourceIndex = 0; ANGLE_TRY(getStagingTexture(&stagingTexture, &subresourceIndex)); ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); ASSERT(stagingTexture && stagingTexture->valid()); HRESULT result = deviceContext->Map(stagingTexture->get(), subresourceIndex, mapType, 0, map); if (FAILED(result)) { // this can fail if the device is removed (from TDR) if (d3d11::isDeviceLostError(result)) { mRenderer->notifyDeviceLost(); } return gl::OutOfMemory() << "Failed to map staging texture, " << gl::FmtHR(result); } mDirty = true; return gl::NoError(); }
static bool grabFrameD3D11(IDXGISwapChain *swap) { ID3D11Device *device = 0; ID3D11DeviceContext *context = 0; ID3D11Texture2D *tex = 0, *captureTex = 0; if (FAILED(swap->GetBuffer(0, IID_ID3D11Texture2D, (void**)&tex))) return false; D3D11_TEXTURE2D_DESC desc; tex->GetDevice(&device); tex->GetDesc(&desc); // re-creating the capture staging texture each frame is definitely not the most efficient // way to handle things, but it frees me of all kind of resource management trouble, so // here goes... desc.MipLevels = 1; desc.ArraySize = 1; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D11_USAGE_STAGING; desc.BindFlags = 0; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.MiscFlags = 0; if(FAILED(device->CreateTexture2D(&desc,0,&captureTex))) printLog("video/d3d11: couldn't create staging texture for gpu->cpu download!\n"); else setCaptureResolution(desc.Width,desc.Height); device->GetImmediateContext(&context); context->CopySubresourceRegion(captureTex,0,0,0,0,tex,0,0); D3D11_MAPPED_SUBRESOURCE mapped; bool grabOk = false; if(captureTex && SUCCEEDED(context->Map(captureTex,0,D3D11_MAP_READ,0,&mapped))) { switch(desc.Format) { case DXGI_FORMAT_R8G8B8A8_UNORM: blitAndFlipRGBAToCaptureData((unsigned char *) mapped.pData,mapped.RowPitch); grabOk = true; break; default: printLog("video/d3d11: unsupported backbuffer format, can't grab pixels!\n"); break; } context->Unmap(captureTex,0); } tex->Release(); if(captureTex) captureTex->Release(); context->Release(); device->Release(); return grabOk; }
HRESULT DeferredPipeline::Lighting::set_ps_spot_light_buffer(ID3D11DeviceContext & device_context, Lighting_PS_Spot_Light_Buffer & ps_light_buffer) { HRESULT hr; D3D11_MAPPED_SUBRESOURCE resource; Lighting_PS_Spot_Light_Buffer* mappedBuffer; hr = device_context.Map(_ps_spot_light_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); if (FAILED(hr)) { return hr; } mappedBuffer = (Lighting_PS_Spot_Light_Buffer*)resource.pData; mappedBuffer->ambient = ps_light_buffer.ambient; mappedBuffer->diffuse = ps_light_buffer.diffuse; mappedBuffer->specular = ps_light_buffer.specular; mappedBuffer->specular_power = ps_light_buffer.specular_power; mappedBuffer->light_direction = ps_light_buffer.light_direction; mappedBuffer->attenuation = ps_light_buffer.attenuation; mappedBuffer->cutoff = ps_light_buffer.cutoff; mappedBuffer->position = ps_light_buffer.position; device_context.Unmap(_ps_spot_light_buffer, 0); device_context.PSSetConstantBuffers(1, 1, &_ps_spot_light_buffer); return S_OK; }
bool CSlideShowPic::UpdateVertexBuffer(Vertex* vericies) { if (!m_vb) // create new { CD3D11_BUFFER_DESC desc(sizeof(Vertex) * 5, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); D3D11_SUBRESOURCE_DATA initData = {}; initData.pSysMem = vericies; initData.SysMemPitch = sizeof(Vertex) * 5; if (SUCCEEDED(g_Windowing.Get3D11Device()->CreateBuffer(&desc, &initData, &m_vb))) return true; } else // update { ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); D3D11_MAPPED_SUBRESOURCE res; if (SUCCEEDED(pContext->Map(m_vb, 0, D3D11_MAP_WRITE_DISCARD, 0, &res))) { memcpy(res.pData, vericies, sizeof(Vertex) * 5); pContext->Unmap(m_vb, 0); return true; } } return false; }
gl::Error IndexBuffer11::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) { if (!mBuffer) { return gl::Error(GL_OUT_OF_MEMORY, "Internal index buffer is not initialized."); } // Check for integer overflows and out-out-bounds map requests if (offset + size < offset || offset + size > mBufferSize) { return gl::Error(GL_OUT_OF_MEMORY, "Index buffer map range is not inside the buffer."); } ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { return gl::Error(GL_OUT_OF_MEMORY, "Failed to map internal index buffer, HRESULT: 0x%08x.", result); } *outMappedMemory = reinterpret_cast<char*>(mappedResource.pData) + offset; return gl::Error(GL_NO_ERROR); }
bool IndexBuffer11::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) { if (mBuffer) { // Check for integer overflows and out-out-bounds map requests if (offset + size < offset || offset + size > mBufferSize) { ERR("Index buffer map range is not inside the buffer."); return false; } ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { ERR("Index buffer map failed with error 0x%08x", result); return false; } *outMappedMemory = reinterpret_cast<char*>(mappedResource.pData) + offset; return true; } else { ERR("Index buffer not initialized."); return false; } }
bool VertexBuffer11::storeRawData(const void* data, unsigned int size, unsigned int offset) { if (mBuffer) { ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { ERR("Vertex buffer map failed with error 0x%08x", result); return false; } char* bufferData = static_cast<char*>(mappedResource.pData); memcpy(bufferData + offset, data, size); dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Vertex buffer not initialized."); return false; } }
void IndexManager::addMesh(Mesh * mesh) { if (mesh->indices().size() < spaceLeft()) { UINT sizeOfUint = sizeof(UINT); UINT offset = mIndexCount * sizeOfUint; mesh->setIndexOffset(offset); mesh->setIndexBuffer(buffer); ID3D11DeviceContext* context = mGame->deviceContext(); D3D11_MAPPED_SUBRESOURCE resource; HRESULT hr = context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); UINT * indices = (UINT*)resource.pData; //for (size_t i = 0; i < mIndexCount; i++) //{ // indices[i] = mIndices[i]; //} for (size_t i = 0; i < mesh->indices().size(); i++) { //mIndices[mIndexCount] = mesh->indices()[i]; indices[mIndexCount] = mesh->indices()[i]; mIndexCount++; } context->Unmap(buffer, 0); } }
bool CSlideShowPic::UpdateVertexBuffer(Vertex* vertices) { if (!m_vb) // create new { CD3D11_BUFFER_DESC desc(sizeof(Vertex) * 5, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); D3D11_SUBRESOURCE_DATA initData = {}; initData.pSysMem = vertices; initData.SysMemPitch = sizeof(Vertex) * 5; if (SUCCEEDED(DX::DeviceResources::Get()->GetD3DDevice()->CreateBuffer(&desc, &initData, m_vb.ReleaseAndGetAddressOf()))) return true; } else // update { ID3D11DeviceContext* pContext = DX::DeviceResources::Get()->GetD3DContext(); D3D11_MAPPED_SUBRESOURCE res; if (SUCCEEDED(pContext->Map(m_vb.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &res))) { memcpy(res.pData, vertices, sizeof(Vertex) * 5); pContext->Unmap(m_vb.Get(), 0); return true; } } return false; }
void CGUIWindowTestPatternDX::UpdateVertexBuffer(Vertex *vertices, unsigned count) { unsigned width = sizeof(Vertex) * count; if (!m_vb || width > m_bufferWidth) // create new { SAFE_RELEASE(m_vb); CD3D11_BUFFER_DESC desc(width, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); D3D11_SUBRESOURCE_DATA initData = {}; initData.pSysMem = vertices; initData.SysMemPitch = width; if (SUCCEEDED(g_Windowing.Get3D11Device()->CreateBuffer(&desc, &initData, &m_vb))) { m_bufferWidth = width; } return; } else // update { ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); D3D11_MAPPED_SUBRESOURCE res; if (SUCCEEDED(pContext->Map(m_vb, 0, D3D11_MAP_WRITE_DISCARD, 0, &res))) { memcpy(res.pData, vertices, sizeof(Vertex) * count); pContext->Unmap(m_vb, 0); } } }
MappedTexture D3D11Texture::map() { D3D11_MAP mtype = D3D11_MAP_WRITE_DISCARD; if (mReadable && mWriteable) mtype = D3D11_MAP_READ_WRITE; else if (mReadable) mtype = D3D11_MAP_READ; else mtype = D3D11_MAP_WRITE_DISCARD; ID3D11DeviceContext *ctx = static_cast<D3D11RenderSystem*>(D3D11RenderSystem::getPtr())->getContext(); D3D11_MAPPED_SUBRESOURCE msr; msr.pData = NULL; switch (mDimension) { case D3D11_SRV_DIMENSION_TEXTURE2D: ID3D11Texture2D *tex; mTexture->GetResource((ID3D11Resource**)&tex); ctx->Map(tex, D3D11CalcSubresource(0,0,1), D3D11_MAP_WRITE_DISCARD, 0, &msr); SAFE_RELEASE(tex); return MappedTexture(msr.pData, msr.RowPitch); } return MappedTexture(0, 0); }
void RasterizingShadowsComputeShader::setParameters( ID3D11DeviceContext& deviceContext, const float3& cameraPos, const Light& light, const Texture2DSpecBind< TexBind::ShaderResource, float4 >& rayOriginTexture, const Texture2DSpecBind< TexBind::ShaderResource, float4 >& surfaceNormalTexture, const int outputTextureWidth, const int outputTextureHeight ) { if ( !m_compiled ) throw std::exception( "RasterizingShadowsComputeShader::setParameters - Shader hasn't been compiled yet." ); std::shared_ptr< Texture2DSpecBind< TexBind::ShaderResource, float > > shadowMap; if ( light.getType() == Light::Type::SpotLight ) shadowMap = static_cast<const SpotLight&>( light ).getShadowMap(); { // Set input buffers and textures. const unsigned int resourceCount = 3; ID3D11ShaderResourceView* resources[ resourceCount ] = { rayOriginTexture.getShaderResourceView(), surfaceNormalTexture.getShaderResourceView(), shadowMap ? shadowMap->getShaderResourceView() : nullptr, }; deviceContext.CSSetShaderResources( 0, resourceCount, resources ); } { // Set constant buffer. D3D11_MAPPED_SUBRESOURCE mappedResource; ConstantBuffer* dataPtr; HRESULT result = deviceContext.Map( m_constantInputBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource ); if ( result < 0 ) throw std::exception( "RasterizingShadowsComputeShader::setParameters - mapping constant buffer to CPU memory failed." ); dataPtr = (ConstantBuffer*)mappedResource.pData; dataPtr->outputTextureSize = float2( (float)outputTextureWidth, (float)outputTextureHeight ); dataPtr->lightPosition = light.getPosition(); dataPtr->lightEmitterRadius = light.getEmitterRadius(); const SpotLight& spotLight = static_cast<const SpotLight&>( light ); dataPtr->shadowMapViewMatrix = spotLight.getShadowMapViewMatrix().getTranspose(); dataPtr->shadowMapProjectionMatrix = spotLight.getShadowMapProjectionMatrix().getTranspose(); dataPtr->lightConeMinDot = cos( spotLight.getConeAngle() ); dataPtr->lightDirection = spotLight.getDirection(); dataPtr->cameraPosition = cameraPos; deviceContext.Unmap( m_constantInputBuffer.Get(), 0 ); deviceContext.CSSetConstantBuffers( 0, 1, m_constantInputBuffer.GetAddressOf() ); } { // Set texture samplers. ID3D11SamplerState* samplerStates[] = { m_pointSamplerState.Get(), m_linearSamplerState.Get() }; deviceContext.CSSetSamplers( 0, 2, samplerStates ); } }
void TgcDX11Effect::setConstantBuffer(string name, const void* bufferData) { ID3D11DeviceContext* deviceContext = ((TgcDX11Renderer*)GuiController::Instance->renderer)->deviceContext; TgcEffectValues::ConstantBuffer cBuffer = this->constantBuffers[name]; ID3D11Buffer* dxCbuffer = this->dxConstantBuffers[name]; //Map D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = deviceContext->Map(dxCbuffer, cBuffer.slot, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); //Copy values memcpy(mappedResource.pData, bufferData, cBuffer.size); //Unmap deviceContext->Unmap(dxCbuffer, cBuffer.slot); //Set buffer in VS o PS if(cBuffer.shaderType == TgcEffectValues::VS) { deviceContext->VSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); } else if(cBuffer.shaderType == TgcEffectValues::PS) { deviceContext->PSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); } else if(cBuffer.shaderType == TgcEffectValues::VS_AND_PS) { deviceContext->VSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); deviceContext->PSSetConstantBuffers(cBuffer.slot, 1, &dxCbuffer); } }
void DX11ScratchStructuredArray::Refresh(ID3D11DeviceContext& context) { // Read back the result into staging memory context.CopyResource(readback_buffer_.Get(), buffer_.Get()); // Copy the staging memory buffer into system memory buffer D3D11_MAPPED_SUBRESOURCE subresource; context.Map(readback_buffer_.Get(), 0, // Map everything D3D11_MAP_READ, 0, &subresource); auto size = element_size_ * element_count_; memcpy_s(raw_buffer_, size, subresource.pData, size); context.Unmap(readback_buffer_.Get(), 0); }
void CPlanBuffer::SetUV(const D3DXVECTOR2& _vUV) { D3D11_MAPPED_SUBRESOURCE tSubreResource; ID3D11DeviceContext* pDeviceContext = m_pDevice->GetDeviceContext(); pDeviceContext->Map(m_pVtxBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &tSubreResource); VertexTexture* pVertex = (VertexTexture*)tSubreResource.pData; //pVertex[1].vTextureUV = D3DXVECTOR2(_vUV.x, 0); //pVertex[2].vTextureUV = D3DXVECTOR2(_vUV.x, _vUV.y); //pVertex[3].vTextureUV = D3DXVECTOR2(0, _vUV.y); VertexTexture pVertexForChange[] = { { D3DXVECTOR3(-1.f, -0.5f, 1.f) , D3DXVECTOR3(0.f, 1.f, 0.f) , D3DXVECTOR2(0.f, 0.f) }, { D3DXVECTOR3(1.f, -0.5f, 1.f) , D3DXVECTOR3(0.f, 1.f, 0.f) , D3DXVECTOR2(_vUV.x, 0.f) }, { D3DXVECTOR3(1.f, -0.5f, -1.f) , D3DXVECTOR3(0.f, 1.f, 0.f) , D3DXVECTOR2(_vUV.x, _vUV.y) }, { D3DXVECTOR3(-1.f, -0.5f, -1.f) , D3DXVECTOR3(0.f, 1.f, 0.f) , D3DXVECTOR2(0.f, _vUV.y) }, }; memcpy(pVertex, pVertexForChange, (sizeof(VertexTexture)* m_nVtxNum)); pDeviceContext->Unmap(m_pVtxBuffer, 0); }
void CRenderCaptureDX::SurfaceToBuffer() { ID3D11DeviceContext* pContext = g_Windowing.GetImmediateContext(); D3D11_MAPPED_SUBRESOURCE lockedRect; if (pContext->Map(m_copySurface, 0, D3D11_MAP_READ, 0, &lockedRect) == S_OK) { //if pitch is same, do a direct copy, otherwise copy one line at a time if (lockedRect.RowPitch == m_width * 4) { memcpy(m_pixels, lockedRect.pData, m_width * m_height * 4); } else { for (unsigned int y = 0; y < m_height; y++) memcpy(m_pixels + y * m_width * 4, (uint8_t*)lockedRect.pData + y * lockedRect.RowPitch, m_width * 4); } pContext->Unmap(m_copySurface, 0); SetState(CAPTURESTATE_DONE); } else { CLog::Log(LOGERROR, "CRenderCaptureDX::SurfaceToBuffer: locking m_copySurface failed"); SetState(CAPTURESTATE_FAILED); } }
CRenderPicture *CProcessorHD::Convert(DVDVideoPicture* picture) { // RENDER_FMT_YUV420P -> DXGI_FORMAT_NV12 // RENDER_FMT_YUV420P10 -> DXGI_FORMAT_P010 // RENDER_FMT_YUV420P16 -> DXGI_FORMAT_P016 if ( picture->format != RENDER_FMT_YUV420P && picture->format != RENDER_FMT_YUV420P10 && picture->format != RENDER_FMT_YUV420P16) { CLog::Log(LOGERROR, "%s - colorspace not supported by processor, skipping frame.", __FUNCTION__); return nullptr; } ID3D11View* pView = m_context->GetFree(nullptr); if (!pView) { CLog::Log(LOGERROR, "%s - no free video surface", __FUNCTION__); return nullptr; } ID3D11VideoProcessorInputView* view = reinterpret_cast<ID3D11VideoProcessorInputView*>(pView); ID3D11Resource* pResource = nullptr; view->GetResource(&pResource); D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC vpivd; view->GetDesc(&vpivd); UINT subresource = D3D11CalcSubresource(0, vpivd.Texture2D.ArraySlice, 1); D3D11_MAPPED_SUBRESOURCE rectangle; ID3D11DeviceContext* pContext = g_Windowing.GetImmediateContext(); if (FAILED(pContext->Map(pResource, subresource, D3D11_MAP_WRITE_DISCARD, 0, &rectangle))) { CLog::Log(LOGERROR, "%s - could not lock rect", __FUNCTION__); m_context->ClearReference(view); return nullptr; } uint8_t* pData = static_cast<uint8_t*>(rectangle.pData); uint8_t* dst[] = { pData, pData + m_texDesc.Height * rectangle.RowPitch }; int dstStride[] = { rectangle.RowPitch, rectangle.RowPitch }; if (picture->format == RENDER_FMT_YUV420P) { convert_yuv420_nv12(picture->data, picture->iLineSize, picture->iHeight, picture->iWidth, dst, dstStride); } else { convert_yuv420_p01x(picture->data, picture->iLineSize, picture->iHeight, picture->iWidth, dst, dstStride , picture->format == RENDER_FMT_YUV420P10 ? 10 : 16); } pContext->Unmap(pResource, subresource); SAFE_RELEASE(pResource); m_context->ClearReference(view); m_context->MarkRender(view); CRenderPicture *pic = new CRenderPicture(m_context); pic->view = view; return pic; }
void CopyPXCImageToTexture(PXCImage *srcImage, SCodeTextureWrap *dstTexture) { if (srcImage) { ID3D11DeviceContext *context = CPUT_DX11::GetContext(); D3D11_MAPPED_SUBRESOURCE mapData; PXCImage::ImageData colorImageData; srcImage->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PixelFormat::PIXEL_FORMAT_RGB24, &colorImageData); PXCImage::ImageInfo colorInfo = srcImage->QueryInfo(); TextureWrap_AdjustSize(dstTexture, colorInfo.width, colorInfo.height); context->Map(dstTexture->Texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapData); uint32 *data = (uint32*)mapData.pData; for (int y = 0; y < colorInfo.height; y++) { byte *srcData = ((byte*)colorImageData.planes[0]) + colorImageData.pitches[0] * y; byte *dstData = ((byte*)mapData.pData) + mapData.RowPitch * (colorInfo.height - y - 1); for (int x = 0; x < colorInfo.width; x++) { dstData[0] = srcData[2]; dstData[1] = srcData[1]; dstData[2] = srcData[0]; dstData[3] = 0xff; srcData += 3; dstData += 4; } } srcImage->ReleaseAccess(&colorImageData); context->Unmap(dstTexture->Texture, 0); } }
void EdgeDistanceComputeShader::setParameters( ID3D11DeviceContext& deviceContext, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& distToEdgeTexture, const unsigned char passIndex ) { if ( !m_compiled ) throw std::exception( "EdgeDistanceComputeShader::setParameters - Shader hasn't been compiled yet." ); { // Set input textures. const unsigned int resourceCount = 1; ID3D11ShaderResourceView* resources[ resourceCount ] = { distToEdgeTexture.getShaderResourceView() }; deviceContext.CSSetShaderResources( 0, resourceCount, resources ); } D3D11_MAPPED_SUBRESOURCE mappedResource; ConstantBuffer* dataPtr; HRESULT result = deviceContext.Map( m_constantInputBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource ); if ( result < 0 ) throw std::exception( "EdgeDistanceComputeShader::setParameters - mapping constant buffer to CPU memory failed." ); dataPtr = (ConstantBuffer*)mappedResource.pData; dataPtr->passIndex = passIndex; dataPtr->pad1 = float3( 0.0f, 0.0f, 0.0f ); // Padding. deviceContext.Unmap( m_constantInputBuffer.Get(), 0 ); deviceContext.CSSetConstantBuffers( 0, 1, m_constantInputBuffer.GetAddressOf() ); }
gl::Error VertexBuffer11::storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData ¤tValue, GLint start, GLsizei count, GLsizei instances, unsigned int offset) { if (!mBuffer) { return gl::Error(GL_OUT_OF_MEMORY, "Internal vertex buffer is not initialized."); } gl::Buffer *buffer = attrib.buffer.get(); int inputStride = ComputeVertexAttributeStride(attrib); ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { return gl::Error(GL_OUT_OF_MEMORY, "Failed to map internal vertex buffer, HRESULT: 0x%08x.", result); } uint8_t *output = reinterpret_cast<uint8_t*>(mappedResource.pData) + offset; const uint8_t *input = NULL; if (attrib.enabled) { if (buffer) { BufferD3D *storage = BufferD3D::makeFromBuffer(buffer); gl::Error error = storage->getData(&input); if (error.isError()) { return error; } input += static_cast<int>(attrib.offset); } else { input = static_cast<const uint8_t*>(attrib.pointer); } } else { input = reinterpret_cast<const uint8_t*>(currentValue.FloatValues); } if (instances == 0 || attrib.divisor == 0) { input += inputStride * start; } gl::VertexFormat vertexFormat(attrib, currentValue.Type); const d3d11::VertexFormat &vertexFormatInfo = d3d11::GetVertexFormatInfo(vertexFormat); ASSERT(vertexFormatInfo.copyFunction != NULL); vertexFormatInfo.copyFunction(input, inputStride, count, output); dxContext->Unmap(mBuffer, 0); return gl::Error(GL_NO_ERROR); }
//---------------------------------------------------------------------------------------------------- bool EEProgressbar::Update() { if (!EEObject::Update()) return false; UpdateObjectState(); if (m_isPositionDirty) { m_isPositionDirty = false; } if (m_isScaleDirty || m_isLocalZOrderDirty || m_isProgressDirty) { Rect_Float rect( -m_quadWidth / 2, -m_quadHeight / 2, m_quadWidth / 2, m_quadHeight / 2 ); //the value of the z depends on the progress (the scaled end - the scaled width * (1.0f - the progress) rect.z -= (rect.z - rect.x) * (1.0f - m_progress); EEQuad2DVertex vertices[4]; vertices[0].pos = FLOAT3(rect.x, rect.y, m_localZOrder * 0.0001f); vertices[0].tex = FLOAT2(0, 0); vertices[1].pos = FLOAT3(rect.z, rect.y, m_localZOrder * 0.0001f); vertices[1].tex = FLOAT2(m_progress, 0); vertices[2].pos = FLOAT3(rect.x, rect.w, m_localZOrder * 0.0001f); vertices[2].tex = FLOAT2(0, 1); vertices[3].pos = FLOAT3(rect.z, rect.w, m_localZOrder * 0.0001f); vertices[3].tex = FLOAT2(m_progress, 1); ID3D11DeviceContext *deviceContext = EECore::s_EECore->GetDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE)); deviceContext->Map(m_quadVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); memcpy(mappedResource.pData, vertices, sizeof(vertices)); deviceContext->Unmap(m_quadVB, 0); if (m_isProgressDirty) { if (m_callbackFunc) { m_callbackFunc(m_progress); } } m_isScaleDirty = false; m_isLocalZOrderDirty = false; m_isProgressDirty = false; } m_progressFrame.Update(); return true; }
CRenderPicture *CProcessorHD::Convert(DVDVideoPicture &picture) { if ( picture.format != RENDER_FMT_YUV420P && picture.format != RENDER_FMT_YUV420P10 && picture.format != RENDER_FMT_YUV420P16 && picture.format != RENDER_FMT_DXVA) { CLog::Log(LOGERROR, "%s - colorspace not supported by processor, skipping frame.", __FUNCTION__); return nullptr; } if (picture.format == RENDER_FMT_DXVA) return picture.dxva->Acquire(); ID3D11View *pView = m_context->GetFree(nullptr); if (!pView) { CLog::Log(LOGERROR, "%s - no free video surface", __FUNCTION__); return nullptr; } ID3D11Resource* pResource = nullptr; pView->GetResource(&pResource); D3D11_MAPPED_SUBRESOURCE rectangle; ID3D11DeviceContext* pContext = g_Windowing.GetImmediateContext(); if (FAILED(pContext->Map(pResource, 0, D3D11_MAP_WRITE_DISCARD, 0, &rectangle))) { CLog::Log(LOGERROR, "%s - could not lock rect", __FUNCTION__); m_context->ClearReference(pView); return nullptr; } uint8_t* pData = static_cast<uint8_t*>(rectangle.pData); uint8_t* dst[] = { pData, pData + m_texDesc.Height * rectangle.RowPitch }; int dstStride[] = { rectangle.RowPitch, rectangle.RowPitch }; if (picture.format == RENDER_FMT_YUV420P) { convert_yuv420_nv12(picture.data, picture.iLineSize, picture.iHeight, picture.iWidth, dst, dstStride); } else if(picture.format == RENDER_FMT_YUV420P10 || picture.format == RENDER_FMT_YUV420P16) { convert_yuv420_p01x(picture.data, picture.iLineSize, picture.iHeight, picture.iWidth, dst, dstStride , picture.format == RENDER_FMT_YUV420P10 ? 10 : 16); } pContext->Unmap(pResource, 0); SAFE_RELEASE(pResource); m_context->ClearReference(pView); m_context->MarkRender(pView); CRenderPicture *pic = new CRenderPicture(m_context); pic->view = pView; return pic; }
void checkPixelsUsingD3D(bool usingPresentPathFast) { ASSERT_NE(nullptr, mOffscreenSurfaceD3D11Texture); D3D11_TEXTURE2D_DESC textureDesc = {0}; ID3D11Device *device; ID3D11DeviceContext *context; mOffscreenSurfaceD3D11Texture->GetDesc(&textureDesc); mOffscreenSurfaceD3D11Texture->GetDevice(&device); device->GetImmediateContext(&context); ASSERT_NE(nullptr, device); ASSERT_NE(nullptr, context); textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; textureDesc.Usage = D3D11_USAGE_STAGING; textureDesc.BindFlags = 0; textureDesc.MiscFlags = 0; ID3D11Texture2D *cpuTexture = nullptr; ASSERT_TRUE(SUCCEEDED(device->CreateTexture2D(&textureDesc, nullptr, &cpuTexture))); context->CopyResource(cpuTexture, mOffscreenSurfaceD3D11Texture); D3D11_MAPPED_SUBRESOURCE mappedSubresource; context->Map(cpuTexture, 0, D3D11_MAP_READ, 0, &mappedSubresource); ASSERT_EQ(static_cast<UINT>(mWindowWidth * 4), mappedSubresource.RowPitch); ASSERT_EQ(static_cast<UINT>(mWindowWidth * mWindowWidth * 4), mappedSubresource.DepthPitch); angle::GLColor *byteData = reinterpret_cast<angle::GLColor *>(mappedSubresource.pData); // Note that the texture is in BGRA format, although the GLColor struct is RGBA GLColor expectedTopLeftPixel = GLColor(0, 0, 255, 255); // Red GLColor expectedTopRightPixel = GLColor(0, 255, 0, 255); // Green GLColor expectedBottomLeftPixel = GLColor(255, 0, 0, 255); // Blue GLColor expectedBottomRightPixel = GLColor(0, 255, 255, 255); // Red + Green if (usingPresentPathFast) { // Invert the expected values GLColor tempTopLeft = expectedTopLeftPixel; GLColor tempTopRight = expectedTopRightPixel; expectedTopLeftPixel = expectedBottomLeftPixel; expectedTopRightPixel = expectedBottomRightPixel; expectedBottomLeftPixel = tempTopLeft; expectedBottomRightPixel = tempTopRight; } EXPECT_EQ(expectedTopLeftPixel, byteData[0]); EXPECT_EQ(expectedTopRightPixel, byteData[(mWindowWidth - 1)]); EXPECT_EQ(expectedBottomLeftPixel, byteData[(mWindowWidth - 1) * mWindowWidth]); EXPECT_EQ(expectedBottomRightPixel, byteData[(mWindowWidth - 1) * mWindowWidth + (mWindowWidth - 1)]); context->Unmap(cpuTexture, 0); SafeRelease(cpuTexture); SafeRelease(device); SafeRelease(context); }
void* DXIndexBuffer::lockIndexPointer(){ ID3D11DeviceContext* ctx = static_cast< DXRenderer* >(Engine::instance()->getRenderer())->getContext(); void* inds; D3D11_MAPPED_SUBRESOURCE mr; ctx->Map(mIb, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mr); inds = mr.pData; mInds = (unsigned char*)inds; return inds; }
bool VertexBuffer11::storeVertexAttributes(const gl::VertexAttribute &attrib, const gl::VertexAttribCurrentValueData ¤tValue, GLint start, GLsizei count, GLsizei instances, unsigned int offset) { if (mBuffer) { gl::Buffer *buffer = attrib.buffer.get(); int inputStride = ComputeVertexAttributeStride(attrib); ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { ERR("Vertex buffer map failed with error 0x%08x", result); return false; } uint8_t* output = reinterpret_cast<uint8_t*>(mappedResource.pData) + offset; const uint8_t *input = NULL; if (attrib.enabled) { if (buffer) { Buffer11 *storage = Buffer11::makeBuffer11(buffer->getImplementation()); input = static_cast<const uint8_t*>(storage->getData()) + static_cast<int>(attrib.offset); } else { input = static_cast<const uint8_t*>(attrib.pointer); } } else { input = reinterpret_cast<const uint8_t*>(currentValue.FloatValues); } if (instances == 0 || attrib.divisor == 0) { input += inputStride * start; } gl::VertexFormat vertexFormat(attrib, currentValue.Type); const d3d11::VertexFormat &vertexFormatInfo = d3d11::GetVertexFormatInfo(vertexFormat); ASSERT(vertexFormatInfo.copyFunction != NULL); vertexFormatInfo.copyFunction(input, inputStride, count, output); dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Vertex buffer not initialized."); return false; } }
void GenerateFirstRefractedRaysComputeShader::setParameters( ID3D11DeviceContext& deviceContext, const float3 cameraPos, const float3 viewportCenter, const float3 viewportUp, const float3 viewportRight, const float2 viewportSize, const Texture2DSpecBind< TexBind::ShaderResource, float4 >& positionTexture, const Texture2DSpecBind< TexBind::ShaderResource, float4 >& normalTexture, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& roughnessTexture, const Texture2DSpecBind< TexBind::ShaderResource, unsigned char >& refractiveIndexTexture, const Texture2DSpecBind< TexBind::ShaderResource, uchar4 >& contributionTermTexture, const int outputTextureWidth, const int outputTextureHeight ) { if ( !m_compiled ) throw std::exception( "GenerateFirstRefractedRaysComputeShader::setParameters - Shader hasn't been compiled yet." ); { // Set input buffers and textures. m_resourceCount = 5 /*+ (int)refractiveIndexTextures.size()*/; std::vector< ID3D11ShaderResourceView* > resources; resources.reserve( m_resourceCount ); resources.push_back( positionTexture.getShaderResourceView() ); resources.push_back( normalTexture.getShaderResourceView() ); resources.push_back( roughnessTexture.getShaderResourceView() ); resources.push_back( refractiveIndexTexture.getShaderResourceView() ); resources.push_back( contributionTermTexture.getShaderResourceView() ); /*for ( int i = 0; i < refractiveIndexTextures .size(); ++i ) resources.push_back( refractiveIndexTextures[ i ]->getShaderResourceView() );*/ deviceContext.CSSetShaderResources( 0, m_resourceCount, resources.data() ); } D3D11_MAPPED_SUBRESOURCE mappedResource; ConstantBuffer* dataPtr; HRESULT result = deviceContext.Map( m_constantInputBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource ); if ( result < 0 ) throw std::exception( "GenerateFirstRefractedRaysComputeShader::setParameters - mapping constant buffer to CPU memory failed." ); dataPtr = (ConstantBuffer*)mappedResource.pData; dataPtr->cameraPos = cameraPos; dataPtr->viewportCenter = viewportCenter; dataPtr->viewportUp = viewportUp; dataPtr->viewportRight = viewportRight; dataPtr->viewportSizeHalf = viewportSize / 2.0f; dataPtr->outputTextureSize = float2( (float)outputTextureWidth, (float)outputTextureHeight ); // Padding. dataPtr->pad1 = 0.0f; dataPtr->pad2 = 0.0f; dataPtr->pad3 = 0.0f; dataPtr->pad4 = 0.0f; dataPtr->pad5 = float2( 0.0f, 0.0f ); deviceContext.Unmap( m_constantInputBuffer.Get(), 0 ); deviceContext.CSSetConstantBuffers( 0, 1, m_constantInputBuffer.GetAddressOf() ); ID3D11SamplerState* samplerStates[] = { m_samplerStateLinearFilter.Get() }; deviceContext.CSSetSamplers( 0, 1, samplerStates ); }
// // Read the pixel content of a sprite. // uint8_t * SpriteUtils::ReadPixels( ID3D11DeviceContext2 * context, ID3D11Device2 * device, ID3D11Texture2D * texture, int * dimensions) { HBITMAP hBitmapTexture = NULL; ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)texture; D3D11_TEXTURE2D_DESC desc; d3dtex->GetDesc(&desc); ID3D11Texture2D* pNewTexture = NULL; D3D11_TEXTURE2D_DESC description; d3dtex->GetDesc(&description); description.BindFlags = 0; description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; description.Usage = D3D11_USAGE_STAGING; HRESULT hr = device->CreateTexture2D(&description, NULL, &pNewTexture); ID3D11DeviceContext* ctx = NULL; device->GetImmediateContext(&ctx); ctx->CopyResource(pNewTexture, d3dtex); D3D11_MAPPED_SUBRESOURCE resource; UINT subresource = D3D11CalcSubresource(0, 0, 0); ctx->Map(pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource); // COPY from texture to bitmap buffer uint8_t* sptr = reinterpret_cast<uint8_t*>(resource.pData); uint8_t* dptr = new uint8_t[desc.Width * desc.Height * 4]; for (size_t h = 0; h < desc.Height; ++h) { size_t msize = std::min<size_t>(desc.Width * 4, resource.RowPitch); memcpy_s(dptr, desc.Width * 4, sptr, msize); sptr += resource.RowPitch; dptr += desc.Width * 4; } dptr -= desc.Width*desc.Height * 4; ctx->Unmap(pNewTexture, subresource); // SWAP BGR to RGB bitmap // Notice the capital "P". dptr vs dPtr. uint32_t * dPtr = reinterpret_cast<uint32_t*>(dptr); *(dimensions + WIDTH_INDEX) = desc.Width; *(dimensions + HEIGHT_INDEX) = desc.Height; return dptr; }
void D3D11App::RenderBillboards(const float3 *position, const int count, const float size, const float4 &color) { // Make sure we have enough room in the tool vertex buffer SetToolsVBSize(count * 6 * sizeof(float3)); float3 dx, dy; m_camera.GetBaseVectors(&dx, &dy, NULL); // Fill vertex buffer float3 *dest; ID3D11DeviceContext* context = m_context->GetDeviceContext(); D3D11_MAPPED_SUBRESOURCE resource; context->Map( m_toolsVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); dest = reinterpret_cast<float3*> ( resource.pData ); for (int i = 0; i < count; i++) { dest[6 * i + 0] = position[i] + size * (-dx + dy); dest[6 * i + 1] = position[i] + size * ( dx + dy); dest[6 * i + 2] = position[i] + size * (-dx - dy); dest[6 * i + 3] = position[i] + size * (-dx - dy); dest[6 * i + 4] = position[i] + size * ( dx + dy); dest[6 * i + 5] = position[i] + size * ( dx - dy); } context->Unmap(m_toolsVB, 0 ); ID3D11DeviceContext *dev = m_context->GetDeviceContext(); // Set constants float4x4 *mvp; dev->Map(m_toolsVsCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); mvp = reinterpret_cast<float4x4*> ( resource.pData ); *mvp = m_camera.GetModelViewProjection(); dev->Unmap(m_toolsVsCB, 0); float4 *col; dev->Map( m_toolsPsCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); col = reinterpret_cast<float4*> ( resource.pData ); *col = color; dev->Unmap( m_toolsPsCB, 0 ); // Setup effect m_context->SetEffect(m_toolsEffect); m_context->Apply(1, 0); dev->IASetInputLayout(m_pos3Layout); UINT stride = sizeof(float3); UINT offset = 0; dev->IASetVertexBuffers(0, 1, &m_toolsVB, &stride, &offset); // Render the quads dev->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); dev->Draw(6 * count, 0); }