D3D11 CreateTexture2D的D3D12等效项是什么? [英] What is the D3D12 equivalent of D3D11 CreateTexture2D?

查看:442
本文介绍了D3D11 CreateTexture2D的D3D12等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Direct3d编程的新手,我一直在构建一个简单的Windows应用程序,用d3d12依次显示4张图像. 我了解D3D12架构的基本知识,到目前为止,我已经创建了四个命令分配器(backb缓冲区大小= 4),一个命令列表,一个命令队列,描述符堆和围栏.我还使用WIC从文件中加载了我的图像. 我知道我必须以某种方式将图像纹理数据作为资源.尽管该部分仍不清楚,但我想澄清一下如何在d3d12中从加载的图像中制作纹理.

I'm new to direct3d programming and I have been building a simple windows application to display 4 images one after other with d3d12. I know a basic understanding of the D3D12 architecture and so far I've created four command allocator (backb buffer size = 4) , a command list, a command queue, descriptor heap, and a fence. I also have loaded my images from file using WIC. I know somehow I have to make image texture data as a resource. While that part is still not clear, I'd like to clarify how I can make texture out of a loaded image in d3d12.

推荐答案

D3D12HelloTexture示例中,演示了Direct3D 12纹理上传的基础. -Graphics-Samples"rel =" nofollow> DirectX-Graphics-Samples GitHub repro.

The basics of Direct3D 12 texture upload is demonstrated in the D3D12HelloTexture sample on the DirectX-Graphics-Samples GitHub repro.

ComPtr<ID3D12Resource> textureUploadHeap;

// Create the texture.
{
    // Describe and create a Texture2D.
    D3D12_RESOURCE_DESC textureDesc = {};
    textureDesc.MipLevels = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.Width = TextureWidth;
    textureDesc.Height = TextureHeight;
    textureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
    textureDesc.DepthOrArraySize = 1;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;

    ThrowIfFailed(m_device->CreateCommittedResource(
        &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
        D3D12_HEAP_FLAG_NONE,
        &textureDesc,
        D3D12_RESOURCE_STATE_COPY_DEST,
        nullptr,
        IID_PPV_ARGS(&m_texture)));

    const UINT64 uploadBufferSize = GetRequiredIntermediateSize(m_texture.Get(), 0, 1);

    // Create the GPU upload buffer.
    ThrowIfFailed(m_device->CreateCommittedResource(
        &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
        D3D12_HEAP_FLAG_NONE,
        &CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize),
        D3D12_RESOURCE_STATE_GENERIC_READ,
        nullptr,
        IID_PPV_ARGS(&textureUploadHeap)));

    // Copy data to the intermediate upload heap and then schedule a copy 
    // from the upload heap to the Texture2D.
    std::vector<UINT8> texture = GenerateTextureData();

    D3D12_SUBRESOURCE_DATA textureData = {};
    textureData.pData = &texture[0];
    textureData.RowPitch = TextureWidth * TexturePixelSize;
    textureData.SlicePitch = textureData.RowPitch * TextureHeight;

    UpdateSubresources(m_commandList.Get(), m_texture.Get(), textureUploadHeap.Get(), 0, 0, 1, &textureData);
    m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_texture.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));

    // Describe and create a SRV for the texture.
    D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
    srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
    srvDesc.Format = textureDesc.Format;
    srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MipLevels = 1;
    m_device->CreateShaderResourceView(m_texture.Get(), &srvDesc, m_srvHeap->GetCPUDescriptorHandleForHeapStart());
}

它使用UpdateSubresources辅助函数

It uses the UpdateSubresources helper function the D3DX12.h C++ inline only utility header.

作为使用Direct3D 12而不是Direct3D 11的复杂性的一个示例,只有在为像素着色器加载纹理时,此代码才能正确运行.如果将纹理用于顶点着色器或几何着色器,则可能会崩溃.否则可能会起作用.否则有时可能会起作用,而其他人则不会.这也是在Direct3D 12中加载纹理的唯一方法,甚至不是最有效的方法.

As an example of the complications of using Direct3D 12 instead of Direct3D 11, this code only works correctly if you are loading a texture for a pixel shader. If you use the texture for a vertex shader or a geometry shader, then it might crash. Or it might work. Or it might work sometimes and not others. This is also only one way to load textures in Direct3D 12, and not even the particularly most efficient.

更新: DirectX12的DirectX工具套件现已可用.看看 DDSTextureLoader

UPDATE: DirectX Tool Kit for DirectX12 is now available. Take a look at DDSTextureLoader and WICTextureLoader.

这篇关于D3D11 CreateTexture2D的D3D12等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆