I need to change the position of the texture in the window, I have it drawn in the upper left corner, and I want to place it in the center. In directx 11 we can do this
m_deviceResources->GetD2DDeviceContext()->SetTransform(...)
, but I haven't found any similar code for directx 12. I don't understand how i can change the texture position, maybe I need to change something in ConstantBuffer or change the code for the shader.
Texture Code
void CreateTexture(int width, int height, const void* pData)
{
D3D12_RESOURCE_DESC resourceDesc = {};
resourceDesc.MipLevels = 1;
resourceDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
resourceDesc.Width = width;
resourceDesc.Height = height;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.SampleDesc.Count = 1;
resourceDesc.SampleDesc.Quality = 0;
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
D3D12MA::ALLOCATION_DESC allocationDesc = {};
allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
D3D12MA::Allocation* alloc = nullptr;
allocator->CreateResource(
&allocationDesc,
&resourceDesc,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
nullptr,
&alloc,
IID_PPV_ARGS(&OffscreenTexture));
UpdateTextureResource(width, height, pData);
// Describe and create a SRV for the texture.
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = resourceDesc.Format;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
Device->CreateShaderResourceView(OffscreenTexture, &srvDesc, OffscreenSrvHeap->GetCPUDescriptorHandleForHeapStart());
alloc->Release();
}
void UpdateTextureResource(int width, int height, const void* pData)
{
if (pData == nullptr)
return;
if (upload_texture)
upload_texture->Release();
auto Barrier = CD3DX12_RESOURCE_BARRIER::Transition(OffscreenTexture, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_DEST);
CommandList->ResourceBarrier(1, &Barrier);
const UINT64 uploadBufferSize = GetRequiredIntermediateSize(OffscreenTexture, 0, 1);
D3D12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize);
D3D12MA::ALLOCATION_DESC allocationDesc = {};
allocationDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
allocator->CreateResource(
&allocationDesc,
&desc,
D3D12_RESOURCE_STATE_GENERIC_READ,
NULL,
&upload_texture,
__uuidof(ID3D12Resource),
nullptr);
if (upload_texture == nullptr)
return;
D3D12_SUBRESOURCE_DATA textureData = {};
textureData.pData = pData;
textureData.RowPitch = width * 4; // TextureWidth * TextureSize
textureData.SlicePitch = textureData.RowPitch * height;
UpdateSubresources(CommandList, OffscreenTexture, upload_texture->GetResource(), 0, 0, 1, &textureData);
auto barrier = CD3DX12_RESOURCE_BARRIER::Transition(OffscreenTexture, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
CommandList->ResourceBarrier(1, &barrier);
}
Shader Code
struct PSInput
{
float4 position : SV_POSITION;
float2 uv : TEXCOORD;
};
Texture2D g_texture : register(t0);
SamplerState g_sampler : register(s0);
PSInput VSMain(float4 position : POSITION, float4 uv : TEXCOORD)
{
PSInput result;
result.position = position;
result.uv = uv;
return result;
}
float4 PSMain(PSInput input) : SV_TARGET
{
return g_texture.Sample(g_sampler, input.uv);
}
the DirectXTK12 library helped me