Skip to content

Instantly share code, notes, and snippets.

@RomiTT
Last active August 25, 2019 19:20
Show Gist options
  • Save RomiTT/048a0c0cd7f9d3abb39720fd6ee8044b to your computer and use it in GitHub Desktop.
Save RomiTT/048a0c0cd7f9d3abb39720fd6ee8044b to your computer and use it in GitHub Desktop.
Creating a texture for pixel shader and updating the texture in DirectX 11.
//------------------------------------------------
// Creating texture for rendering video
//------------------------------------------------
// ID3D11Device* Device
ID3D11Texture2D* texture = nullptr;
D3D11_TEXTURE2D_DESC td;
RtlZeroMemory(&td, sizeof(D3D11_TEXTURE2D_DESC));
td.Width = textureWidth;
td.Height = textureHeight;
td.MipLevels = 1;
td.ArraySize = 1;
td.Format = DXGI_FORMAT_R8_UNORM;
td.SampleDesc.Count = 1;
td.Usage = D3D11_USAGE_DYNAMIC;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = Device->CreateTexture2D(&td, nullptr, &texture);
//------------------------------------------------
// Updating texture
//------------------------------------------------
// ID3D11DeviceContext* DeviceContext
D3D11_MAPPED_SUBRESOURCE resource;
UINT subresource = D3D11CalcSubresource(0, 0, 0);
HRESULT hr = DeviceContext->Map(texture, subresource, D3D11_MAP_WRITE_DISCARD, 0, &resource);
// update using data pointer
BYTE* ptr = reinterpret_cast<BYTE*>(resource.pData);
DeviceContext->Unmap(texture, subresource);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment