C++ 如何使用UpdateSubresource在Direct3d中更新纹理

C++ 如何使用UpdateSubresource在Direct3d中更新纹理,c++,directx-11,C++,Directx 11,在我的CreateDeviceResources方法中有以下代码:(该方法在初始化时被调用一次) 我需要做什么来创建一个方法来更改纹理? void SetTexture(…纹理输入…) 每次需要更改纹理时,我是否需要运行下面的代码?或者我可以改变内存中的一些数据吗 我发现我想使用ID3D11DeviceContext::UpdateSubresource,但尚未找到如何使用它的示例 auto textureData = reader->ReadData("SIn.Win8\\textu

在我的CreateDeviceResources方法中有以下代码:(该方法在初始化时被调用一次)

我需要做什么来创建一个方法来更改纹理? void SetTexture(…纹理输入…)

每次需要更改纹理时,我是否需要运行下面的代码?或者我可以改变内存中的一些数据吗

我发现我想使用ID3D11DeviceContext::UpdateSubresource,但尚未找到如何使用它的示例

  auto textureData = reader->ReadData("SIn.Win8\\texturedata.bin");
        D3D11_SUBRESOURCE_DATA textureSubresourceData = {0};
        textureSubresourceData.pSysMem = textureData->Data;

        // Specify the size of a row in bytes, known a priori about the texture data.
        textureSubresourceData.SysMemPitch = 1024;

        // As this is not a texture array or 3D texture, this parameter is ignored.
        textureSubresourceData.SysMemSlicePitch = 0;

        // Create a texture description from information known a priori about the data.
        // Generalized texture loading code can be found in the Resource Loading sample.
        D3D11_TEXTURE2D_DESC textureDesc = {0};
        textureDesc.Width = 256;
        textureDesc.Height = 256;
        textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        textureDesc.Usage = D3D11_USAGE_DEFAULT;
        textureDesc.CPUAccessFlags = 0;
        textureDesc.MiscFlags = 0;

        // Most textures contain more than one MIP level.  For simplicity, this sample uses only one.
        textureDesc.MipLevels = 1;

        // As this will not be a texture array, this parameter is ignored.
        textureDesc.ArraySize = 1;

        // Don't use multi-sampling.
        textureDesc.SampleDesc.Count = 1;
        textureDesc.SampleDesc.Quality = 0;

        // Allow the texture to be bound as a shader resource.
        textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

        ComPtr<ID3D11Texture2D> texture;
        DX::ThrowIfFailed(
            m_d3dDevice->CreateTexture2D(
                &textureDesc,
                &textureSubresourceData,
                &texture
                )
            );

        // Once the texture is created, we must create a shader resource view of it
        // so that shaders may use it.  In general, the view description will match
        // the texture description.
        D3D11_SHADER_RESOURCE_VIEW_DESC textureViewDesc;
        ZeroMemory(&textureViewDesc, sizeof(textureViewDesc));
        textureViewDesc.Format = textureDesc.Format;
        textureViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
        textureViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
        textureViewDesc.Texture2D.MostDetailedMip = 0;

        ComPtr<ID3D11ShaderResourceView> textureView;
        DX::ThrowIfFailed(
            m_d3dDevice->CreateShaderResourceView(
                texture.Get(),
                &textureViewDesc,
                &textureView
                )
            );

        // Once the texture view is created, create a sampler.  This defines how the color
        // for a particular texture coordinate is determined using the relevant texture data.
        D3D11_SAMPLER_DESC samplerDesc;
        ZeroMemory(&samplerDesc, sizeof(samplerDesc));

        samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;

        // The sampler does not use anisotropic filtering, so this parameter is ignored.
        samplerDesc.MaxAnisotropy = 0;

        // Specify how texture coordinates outside of the range 0..1 are resolved.
        samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;

        // Use no special MIP clamping or bias.
        samplerDesc.MipLODBias = 0.0f;
        samplerDesc.MinLOD = 0;
        samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

        // Don't use a comparison function.
        samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;

        // Border address mode is not used, so this parameter is ignored.
        samplerDesc.BorderColor[0] = 0.0f;
        samplerDesc.BorderColor[1] = 0.0f;
        samplerDesc.BorderColor[2] = 0.0f;
        samplerDesc.BorderColor[3] = 0.0f;

        ComPtr<ID3D11SamplerState> sampler;
        DX::ThrowIfFailed(
            m_d3dDevice->CreateSamplerState(
                &samplerDesc,
                &sampler
                )
            );
auto-textureData=reader->ReadData(“SIn.Win8\\textureData.bin”);
D3D11_子资源_数据结构SubResourceData={0};
textureSubresourceData.psysem=textureData->Data;
//以字节为单位指定行的大小,这是纹理数据的先验知识。
textureSubresourceData.SysMemPitch=1024;
//由于这不是纹理阵列或三维纹理,因此将忽略此参数。
textureSubresourceData.SysMemSlicePitch=0;
//根据数据的先验信息创建纹理描述。
//广义纹理加载代码可以在资源加载示例中找到。
D3D11_TEXTURE2D_DESC textureDesc={0};
纹理离散宽度=256;
纹理离散高度=256;
textureDesc.Format=DXGI_格式_R8G8B8A8_UNORM;
textureDesc.Usage=D3D11\u Usage\u默认值;
textureDesc.CPUAccessFlags=0;
textureDesc.miscsflags=0;
//大多数纹理包含多个MIP级别。为简单起见,此示例仅使用一个。
textureDesc.MipLevels=1;
//由于这不是纹理阵列,因此忽略此参数。
textureDesc.ArraySize=1;
//不要使用多重采样。
textureDesc.SampleDesc.Count=1;
textureDesc.SampleDesc.Quality=0;
//允许将纹理绑定为着色器资源。
textureDesc.BindFlags=D3D11\u BIND\u SHADER\u资源;
ComPtr结构;
DX::ThrowIfFailed(
m_d3dDevice->CreateTexture2D(
&结构主义者,
&纹理SubResourceData,
&质地
)
);
//创建纹理后,我们必须为其创建着色器资源视图
//以便着色器可以使用它。通常,视图描述将匹配
//纹理描述。
D3D11_着色器_资源_视图_描述TextureViewer;
零内存(&textureViewDesc,sizeof(textureViewDesc));
textureViewDesc.Format=textureDesc.Format;
TextureViewer.ViewDimension=D3D11\u SRV\u DIMENSION\u TEXTURE2D;
textureViewDesc.Texture2D.MipLevels=textureDesc.MipLevels;
textureViewDesc.Texture2D.MostDetailedMip=0;
ComPtr-textureView;
DX::ThrowIfFailed(
m_d3dDevice->CreateShaderResourceView(
texture.Get(),
&TextureViewer,
&纹理视图
)
);
//创建纹理视图后,创建采样器。这定义了颜色是如何显示的
//对于特定纹理,使用相关纹理数据确定坐标。
D3D11_取样器_DESC取样器DESC;
零内存(&sampledesc,sizeof(sampledesc));
samplerDesc.Filter=D3D11_Filter_MIN_MAG_MIP_LINEAR;
//采样器不使用各向异性过滤,因此忽略此参数。
sampledesc.max各向异性=0;
//指定如何解析范围0..1之外的纹理坐标。
sampledesc.AddressU=D3D11\u纹理\u地址\u包裹;
sampledesc.AddressV=D3D11\u纹理\u地址\u包裹;
samplerDesc.AddressW=D3D11\u纹理\u地址\u包裹;
//不要使用特殊的MIP夹紧或偏置。
sampledesc.MipLODBias=0.0f;
samplerDesc.MinLOD=0;
samplerDesc.MaxLOD=D3D11_FLOAT32_MAX;
//不要使用比较函数。
samplerDesc.ComparisonFunc=D3D11\u比较\u从不;
//未使用边界地址模式,因此忽略此参数。
采样器描述边界颜色[0]=0.0f;
采样器描述边界颜色[1]=0.0f;
采样器描述边界颜色[2]=0.0f;
采样器描述边界颜色[3]=0.0f;
ComPtr取样器;
DX::ThrowIfFailed(
m_d3dDevice->CreateSamplerState(
&采样器描述,
&采样器
)
);

如果要在运行时更新相同的纹理,需要使用

映射类型需要为D3D11\u映射\u写入\u放弃

此外,您的纹理需要使用动态标志(而不是默认标志)创建,cpu访问标志需要设置为D3D11_cpu_access_WRITE

如果允许您访问,您可以使用pData设置新数据

根据具体情况,只需重新创建纹理会更好,这是基于具体情况的(如果纹理经常更改,则动态效果更好)