DirectX 11 渲染到纹理问题

Posted

技术标签:

【中文标题】DirectX 11 渲染到纹理问题【英文标题】:DirectX 11 Render to Texture Issue 【发布时间】:2018-03-18 07:34:36 【问题描述】:

在我的代码中,我正在制作 2 个矩形,

矩形 1:在纹理上渲染。

Rectangle2:在后台缓冲区渲染。

我正在尝试进行可编程混合,因此需要在像素着色器中访问目标像素。

在我的代码中,

我正在创建如下纹理:

d3d11Device->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);

在此之后,我正在创建纹理的渲染目标视图。

  d3d11Device->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetViewMap);

在此之后,我声明了顶点和像素着色器。

然后在我的draw call中,

我正在执行以下操作:

float bgColor[4] = 0.0f, 0.0f,0.0f, 1.0f ;
d3d11DevCon->ClearRenderTargetView(renderTargetViewMap, bgColor);

float bgColor2[4] =  0.0f, 1.0f, 0.0f, 1.0f ;

////////////////////////////////////////////////Buffer 1///////////////////////////////////////////////////////////////
//Set the vertex buffer
UINT stride = sizeof(Vertex);
UINT offset = 0;

///////////////////////////////////////////////////////////Buffer 2//////////////////////////////////////////////////////////////////

d3d11DevCon->IASetIndexBuffer(d2dIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
d3d11DevCon->IASetVertexBuffers(0, 1, &triangleVertBuffer, &stride, &offset);
////Draw the triangle
d3d11DevCon->DrawIndexed(6, 0, 0);

我假设由于我已将渲染目标视图设置为 renderTargetViewMap,所以我的绘图调用将仅渲染到纹理。

现在我正在渲染到我的后备缓冲区:

/////////////////////////////////////

d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, NULL);
d3d11DevCon->PSSetShaderResources(0, 1, &shaderResourceViewMap);
//d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor2);

d3d11DevCon->IASetIndexBuffer(d2dIndexBuffer2, DXGI_FORMAT_R32_UINT, 0);
d3d11DevCon->IASetVertexBuffers(0, 1, &triangleVertBuffer2, &stride, &offset);

////Draw the triangle
d3d11DevCon->DrawIndexed(6, 0, 0);

//Present the backbuffer to the screen
SwapChain->Present(0, 0);

所以,我的渲染就是这样进行的。

问题面孔:

在我的像素着色器中,

VS_OUTPUT VS(float4 inPos : POSITION, float4 inColor : COLOR)

    VS_OUTPUT output;

    output.Pos = inPos;
    output.Color = inColor;

    return output;


float4 PS(VS_OUTPUT input) : SV_TARGET

    float2 temp;
    temp = input.Pos;
    float4 diffuse = ObjTexture.Sample(ObjSamplerState,0.5+0.5*temp);

    return input.Color + diffuse;

这里的漫反射等于我在渲染到纹理时设置的 bgcolor

float bgColor[4] = 0.0f, 0.0f,0.0f, 1.0f ;
d3d11DevCon->ClearRenderTargetView(renderTargetViewMap, bgColor);

我还在上面画了一个矩形,但是我无法访问那些像素。

如何访问我在渲染到纹理时绘制的矩形像素。

这是问题图片

想要的结果

着色器文件:Effect.fx

struct VS_OUTPUT

    float4 Pos : SV_POSITION;
    float4 Color : COLOR;
;

Texture2D ObjTexture;
SamplerState ObjSamplerState;


VS_OUTPUT VS(float4 inPos : POSITION, float4 inColor : COLOR)

    VS_OUTPUT output;

    output.Pos = inPos;
    output.Color = inColor;

    return output;


float4 PS(VS_OUTPUT input) : SV_TARGET

    float2 temp;
    temp = input.Pos;
    float4 diffuse = ObjTexture.Sample(ObjSamplerState,0.5+0.5*temp);

    return input.Color + diffuse;

编辑 - 1:

通过我对代码的最新更改,我能够将矩形 2 与矩形 1 混合,但我面临的一个问题是,当我混合时,矩形 2 的主要部分变为黄色(红色 + 绿色)只有边缘我才能看到实际的绿色。

修改代码:

main.cpp

//Include and link appropriate libraries and headers//
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")

#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include <xnamath.h>




//Global Declarations - Interfaces//
IDXGISwapChain* SwapChain;
ID3D11Device* d3d11Device;
ID3D11DeviceContext* d3d11DevCon;
ID3D11RenderTargetView* renderTargetView;

ID3D11Buffer* triangleVertBuffer;
ID3D11Buffer* triangleVertBuffer2;
ID3D11VertexShader* VS;
ID3D11PixelShader* PS;
ID3D10Blob* VS_Buffer;
ID3D10Blob* PS_Buffer;
ID3D11InputLayout* vertLayout;


XMMATRIX mapView;
XMMATRIX mapProjection;

XMVECTOR DefaultForward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);


//Global Declarations - Others//
LPCTSTR WndClassName = L"firstwindow";
HWND hwnd = NULL;
HRESULT hr;

const int Width = 800;
const int Height = 600;

bool InitializeDirect3d11App(HINSTANCE hInstance)

    //Describe our Buffer
    DXGI_MODE_DESC bufferDesc;

    ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));

    bufferDesc.Width = Width;
    bufferDesc.Height = Height;
    bufferDesc.RefreshRate.Numerator = 60;
    bufferDesc.RefreshRate.Denominator = 1;
    bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    //Describe our SwapChain
    DXGI_SWAP_CHAIN_DESC swapChainDesc;

    ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

    swapChainDesc.BufferDesc = bufferDesc;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.BufferCount = 1;
    swapChainDesc.OutputWindow = hwnd;
    swapChainDesc.Windowed = TRUE;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;


    //Create our SwapChain
    hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
        D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);

    //Create our BackBuffer
    ID3D11Texture2D* BackBuffer;
    hr = SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);

    //Create our Render Target
    hr = d3d11Device->CreateRenderTargetView(BackBuffer, NULL, &renderTargetView);
    BackBuffer->Release();
    ////////////////////////////////////////////////////////////////////////EXPERIMENT AREA//////////////////////////////////////////////////////////////////////////////////////
    ZeroMemory(&textureDesc, sizeof(textureDesc));

    // Setup the texture description.
    // We will have our map be a square
    // We will need to have this texture bound as a render target AND a shader resource
    textureDesc.Width = Width/ 3.9729999999999999999999999999999;
    textureDesc.Height = Height/3.9729999999999999999999999999999;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.Usage = D3D11_USAGE_DEFAULT;
    textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = 0;
    textureDesc.MiscFlags = 0;

    d3d11Device->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);

    // Setup the description of the render target view.
    renderTargetViewDesc.Format = textureDesc.Format;
    renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
    renderTargetViewDesc.Texture2D.MipSlice = 0;

    // Create the render target view.
    d3d11Device->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetViewMap);



    /////////////////////// Map's Shader Resource View
    // Setup the description of the shader resource view.
    shaderResourceViewDesc.Format = textureDesc.Format;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;

    // Create the shader resource view.
    d3d11Device->CreateShaderResourceView(renderTargetTextureMap, &shaderResourceViewDesc, &shaderResourceViewMap);


    d3d11DevCon->OMSetRenderTargets(1, &renderTargetViewMap, NULL);
    d3d11DevCon->PSSetShaderResources(0, 1, &shaderResourceViewMap);

    return true;


void CleanUp()

    //Release the COM Objects we created
    SwapChain->Release();
    d3d11Device->Release();
    d3d11DevCon->Release();
    renderTargetView->Release();
    triangleVertBuffer->Release();
    VS->Release();
    PS->Release();
    VS_Buffer->Release();
    PS_Buffer->Release();
    vertLayout->Release();


bool InitScene()

    //Compile Shaders from shader file
    hr = D3DX11CompileFromFile(L"Effect.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS_Buffer, 0, 0);
    hr = D3DX11CompileFromFile(L"Effect.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS_Buffer, 0, 0);

    //Create the Shader Objects
    hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
    hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

    //Set Vertex and Pixel Shaders
    d3d11DevCon->VSSetShader(VS, 0, 0);
    d3d11DevCon->PSSetShader(PS, 0, 0);


    //Create the Input Layout
    hr = d3d11Device->CreateInputLayout(layout, numElements, VS_Buffer->GetBufferPointer(),
        VS_Buffer->GetBufferSize(), &vertLayout);

    //Set the Input Layout
    d3d11DevCon->IASetInputLayout(vertLayout);

    //Set Primitive Topology
    d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    //Create the Viewport
    D3D11_VIEWPORT viewport;
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = 800;
    viewport.Height = 600;

    //Set the Viewport
    d3d11DevCon->RSSetViewports(1, &viewport);



    ////////////////***********************First  Texture Vertex Buffer *******************************/////////////////////////////
    //Create the vertex buffer

    Vertex v[] =
    
        Vertex(-0.5f, -0.5f, 0.0f, 1.0f,0.0f,0.0f, 1.0f),
        Vertex(-0.5f,  0.5f, 0.0f, 1.0f,0.0f,0.0f, 1.0f),
        Vertex(0.5f,  0.5f, 0.0f,   1.0f, 0.0f,0.0f, 1.0f),
        Vertex(0.5f, -0.5f, 0.0f,   1.0f,0.0f, 0.0f, 1.0f),
    ;

    DWORD indices[] = 
        // Front Face
        0,  1,  3,
        1,  2,  3,
    ;


    D3D11_BUFFER_DESC indexBufferDesc;
    ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));

    indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    indexBufferDesc.ByteWidth = sizeof(DWORD) * 2 * 3;
    indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexBufferDesc.CPUAccessFlags = 0;
    indexBufferDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA iinitData;

    iinitData.pSysMem = indices;
    d3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &d2dIndexBuffer);


    D3D11_BUFFER_DESC vertexBufferDesc;
    ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));

    vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(Vertex) * 4;
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = 0;
    vertexBufferDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA vertexBufferData;

    ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
    vertexBufferData.pSysMem = v;
    hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &triangleVertBuffer);




    ////////////////////////////////////////////////////// Second Vertex.


     Vertex v2[] = 
        // positions          // colors           // texture coords
        Vertex(1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f,1.0f),   // top right
        Vertex(1.0f,0.0f, 0.0f,0.0f,1.0f,0.0f,1.0f),   // bottom right
        Vertex(0.0f,0.0f, 0.0f,0.0f,1.0f,0.0f,1.0f),   // bottom left
        Vertex(0.0f, 1.0, 0.0f,0.0f, 1.0f,0.0f,1.0f)    // top left 
    ;

     DWORD indices2[] = 
         // Front Face
         0,  1,  2,
        0,  2,  3,
     ;



     D3D11_BUFFER_DESC indexBufferDesc2;
     ZeroMemory(&indexBufferDesc2, sizeof(indexBufferDesc2));

     indexBufferDesc2.Usage = D3D11_USAGE_DEFAULT;
     indexBufferDesc2.ByteWidth = sizeof(DWORD) * 2 * 3;
     indexBufferDesc2.BindFlags = D3D11_BIND_INDEX_BUFFER;
     indexBufferDesc2.CPUAccessFlags = 0;
     indexBufferDesc2.MiscFlags = 0;

     D3D11_SUBRESOURCE_DATA iinitData2;

     iinitData2.pSysMem = indices2;
     d3d11Device->CreateBuffer(&indexBufferDesc2, &iinitData2, &d2dIndexBuffer2);


    D3D11_BUFFER_DESC vertexBufferDesc2;
    ZeroMemory(&vertexBufferDesc2, sizeof(vertexBufferDesc2));

    vertexBufferDesc2.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc2.ByteWidth = sizeof(Vertex) * 4;
    vertexBufferDesc2.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc2.CPUAccessFlags = 0;
    vertexBufferDesc2.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA vertexBufferData2;

    ZeroMemory(&vertexBufferData2, sizeof(vertexBufferData2));
    vertexBufferData2.pSysMem = v2;
    hr = d3d11Device->CreateBuffer(&vertexBufferDesc2, &vertexBufferData2, &triangleVertBuffer2);






    UINT stride = sizeof(Vertex);
    UINT offset = 0;



    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    return true;

着色器文件:

struct VS_OUTPUT

    float4 Pos : SV_POSITION;
    float4 Color : COLOR;
;

Texture2D ObjTexture;
SamplerState ObjSamplerState;


VS_OUTPUT VS(float4 inPos : POSITION, float4 inColor : COLOR)

    VS_OUTPUT output;

    output.Pos = inPos;
    output.Color = inColor;

    return output;


float4 PS(VS_OUTPUT input) : SV_TARGET

    float2 temp;
    temp = input.Pos;
    float4 diffuse = ObjTexture.Sample(ObjSamplerState,0.5*temp);

    return input.Color + diffuse ;


        return DefWindowProc(hwnd,
            msg,
            wParam,
            lParam);
    

编辑 3:

我的顶点结构:

struct Vertex    //Overloaded Vertex Structure

    Vertex() 
    Vertex(float x, float y, float z,
        float cr, float cg, float cb, float ca)
        : pos(x, y, z), color(cr, cg, cb, ca) 

    XMFLOAT3 pos;
    XMFLOAT4 color;
;

输入说明:

D3D11_INPUT_ELEMENT_DESC layout[] =

     "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 ,
     "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 ,
;

我认为这样的问题是当我传递我的纹理并在像素着色器中以漫反射方式读取它时,我的纹理在后台缓冲区上的映射是不一样的,因此我只能从第二条扫描线找到红色,因此结果为零是生产..?

编辑 3:

我的初始化代码:

struct Vertex    //Overloaded Vertex Structure

    Vertex() 
    Vertex(float x, float y, float z,
        float cr, float cg, float cb, float ca)
        : pos(x, y, z), color(cr, cg, cb, ca) 

    XMFLOAT3 pos;
    XMFLOAT4 color;
;

ID3D11Texture2D* renderTargetTextureMap;
ID3D11RenderTargetView* renderTargetViewMap;
ID3D11ShaderResourceView* shaderResourceViewMap;
ID3D11SamplerState* CubesTexSamplerState;


ID3D11Buffer *d2dIndexBuffer;
ID3D11Buffer *d2dIndexBuffer2;

D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;




D3D11_INPUT_ELEMENT_DESC layout[] =

     "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 ,
     "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 ,
;
UINT numElements = ARRAYSIZE(layout);

bool InitializeDirect3d11App(HINSTANCE hInstance)

    //Describe our Buffer
    DXGI_MODE_DESC bufferDesc;

    ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));

    bufferDesc.Width = Width;
    bufferDesc.Height = Height;
    bufferDesc.RefreshRate.Numerator = 60;
    bufferDesc.RefreshRate.Denominator = 1;
    bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    //Describe our SwapChain
    DXGI_SWAP_CHAIN_DESC swapChainDesc;

    ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

    swapChainDesc.BufferDesc = bufferDesc;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.BufferCount = 1;
    swapChainDesc.OutputWindow = hwnd;
    swapChainDesc.Windowed = TRUE;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;


    //Create our SwapChain
    hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
        D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);

    //Create our BackBuffer
    ID3D11Texture2D* BackBuffer;
    hr = SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);

    //Create our Render Target
    hr = d3d11Device->CreateRenderTargetView(BackBuffer, NULL, &renderTargetView);
    BackBuffer->Release();
    ////////////////////////////////////////////////////////////////////////EXPERIMENT AREA//////////////////////////////////////////////////////////////////////////////////////
    ZeroMemory(&textureDesc, sizeof(textureDesc));

    // Setup the texture description.
    // We will have our map be a square
    // We will need to have this texture bound as a render target AND a shader resource
    textureDesc.Width = Width/2;
    textureDesc.Height = Height/2;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.Usage = D3D11_USAGE_DEFAULT;
    textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = 0;
    textureDesc.MiscFlags = 0;

    d3d11Device->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);

    // Setup the description of the render target view.
    renderTargetViewDesc.Format = textureDesc.Format;
    renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
    renderTargetViewDesc.Texture2D.MipSlice = 0;

    // Create the render target view.
    d3d11Device->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetView);



    /////////////////////// Map's Shader Resource View
    // Setup the description of the shader resource view.
    shaderResourceViewDesc.Format = textureDesc.Format;
    shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
    shaderResourceViewDesc.Texture2D.MipLevels = 1;

    // Create the shader resource view.
    d3d11Device->CreateShaderResourceView(renderTargetTextureMap, &shaderResourceViewDesc, &shaderResourceViewMap);


    //d3d11DevCon->OMSetRenderTargets(1, &renderTargetViewMap, NULL);
    //d3d11DevCon->PSSetShaderResources(0, 1, &shaderResourceViewMap);

    return true;


void CleanUp()

    //Release the COM Objects we created
    SwapChain->Release();
    d3d11Device->Release();
    d3d11DevCon->Release();
    renderTargetView->Release();
    triangleVertBuffer->Release();
    VS->Release();
    PS->Release();
    VS_Buffer->Release();
    PS_Buffer->Release();
    vertLayout->Release();


bool InitScene()

    //Compile Shaders from shader file
    hr = D3DX11CompileFromFile(L"Effect.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS_Buffer, 0, 0);
    hr = D3DX11CompileFromFile(L"Effect.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS_Buffer, 0, 0);

    //Create the Shader Objects
    hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
    hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

    //Set Vertex and Pixel Shaders
    d3d11DevCon->VSSetShader(VS, 0, 0);
    d3d11DevCon->PSSetShader(PS, 0, 0);


    //Create the Input Layout
    hr = d3d11Device->CreateInputLayout(layout, numElements, VS_Buffer->GetBufferPointer(),
        VS_Buffer->GetBufferSize(), &vertLayout);

    //Set the Input Layout
    d3d11DevCon->IASetInputLayout(vertLayout);

    //Set Primitive Topology
    d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    //Create the Viewport
    D3D11_VIEWPORT viewport;
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = 800;
    viewport.Height = 600;

    //Set the Viewport
    d3d11DevCon->RSSetViewports(1, &viewport);



    ////////////////***********************First  Texture Vertex Buffer *******************************/////////////////////////////
    //Create the vertex buffer

    Vertex v[] =
    
        Vertex(-0.35f, -0.35f, 0.0f, 1.0f,0.0f,0.0f, 1.0f),
        Vertex(-0.35f,  0.35f, 0.0f, 1.0f,0.0f,0.0f, 1.0f),
        Vertex(0.35f,  0.35f, 0.0f, 1.0f, 0.0f,0.0f, 1.0f),
        Vertex(0.35f, -0.35f, 0.0f, 1.0f,0.0f, 0.0f, 1.0f),
    ;

    DWORD indices[] = 
        // Front Face
        0,  1,  3,
        1,  2,  3,
    ;


    D3D11_BUFFER_DESC indexBufferDesc;
    ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));

    indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    indexBufferDesc.ByteWidth = sizeof(DWORD) * 2 * 3;
    indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexBufferDesc.CPUAccessFlags = 0;
    indexBufferDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA iinitData;

    iinitData.pSysMem = indices;
    d3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &d2dIndexBuffer);


    D3D11_BUFFER_DESC vertexBufferDesc;
    ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));

    vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(Vertex) * 4;
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = 0;
    vertexBufferDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA vertexBufferData;

    ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
    vertexBufferData.pSysMem = v;
    hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &triangleVertBuffer);




    ////////////////////////////////////////////////////// Second Vertex.


     Vertex v2[] = 
        // positions          // colors           // texture coords
        Vertex(1.0f, 1.0f, 0.0f,0.0f,1.0f,0.0f,1.0f),   // top right
        Vertex(1.0f,0.0f, 0.0f,0.0f,1.0f,0.0f,1.0f),   // bottom right
        Vertex(0.0f,0.0f, 0.0f,0.0f,1.0f,0.0f,1.0f),   // bottom left
        Vertex(0.0f, 1.0, 0.0f,0.0f, 1.0f,0.0f,1.0f)    // top left 
    ;

     DWORD indices2[] = 
         // Front Face
         0,  1,  2,
        0,  2,  3,
     ;



     D3D11_BUFFER_DESC indexBufferDesc2;
     ZeroMemory(&indexBufferDesc2, sizeof(indexBufferDesc2));

     indexBufferDesc2.Usage = D3D11_USAGE_DEFAULT;
     indexBufferDesc2.ByteWidth = sizeof(DWORD) * 2 * 3;
     indexBufferDesc2.BindFlags = D3D11_BIND_INDEX_BUFFER;
     indexBufferDesc2.CPUAccessFlags = 0;
     indexBufferDesc2.MiscFlags = 0;

     D3D11_SUBRESOURCE_DATA iinitData2;

     iinitData2.pSysMem = indices2;
     d3d11Device->CreateBuffer(&indexBufferDesc2, &iinitData2, &d2dIndexBuffer2);


    D3D11_BUFFER_DESC vertexBufferDesc2;
    ZeroMemory(&vertexBufferDesc2, sizeof(vertexBufferDesc2));

    vertexBufferDesc2.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc2.ByteWidth = sizeof(Vertex) * 4;
    vertexBufferDesc2.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc2.CPUAccessFlags = 0;
    vertexBufferDesc2.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA vertexBufferData2;

    ZeroMemory(&vertexBufferData2, sizeof(vertexBufferData2));
    vertexBufferData2.pSysMem = v2;
    hr = d3d11Device->CreateBuffer(&vertexBufferDesc2, &vertexBufferData2, &triangleVertBuffer2);

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    return true;

编辑 4:

添加绘制场景代码:

void DrawScene()


    //Set the vertex buffer
    UINT stride = sizeof(Vertex);
    UINT offset = 0;

    ///////////////////////////////////////////////////////////Buffer 2//////////////////////////////////////////////////////////////////

    d3d11DevCon->IASetIndexBuffer(d2dIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
    d3d11DevCon->IASetVertexBuffers(0, 1, &triangleVertBuffer, &stride, &offset);
    ////Draw the triangle
    d3d11DevCon->DrawIndexed(6, 0, 0);

    d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, NULL);
    ////////////////////////////////////////////
    d3d11DevCon->PSSetShaderResources(0, 1, &shaderResourceViewMap);



    d3d11DevCon->IASetIndexBuffer(d2dIndexBuffer2, DXGI_FORMAT_R32_UINT, 0);
    d3d11DevCon->IASetVertexBuffers(0, 1, &triangleVertBuffer2, &stride, &offset);

    ////Draw the triangle
    d3d11DevCon->DrawIndexed(6, 0, 0);

    //Present the backbuffer to the screen
    SwapChain->Present(0, 0);

【问题讨论】:

您不应该在顶点中使用纹理坐标,因为您没有在布局中指定它。此外,您不必在着色器中对纹理进行采样。对于第二个矩形,您可能还需要两个不同的着色器。只需使用 input.color.a *0.5。 你设置了纹理吗?因为我在代码中没有看到。 好的,然后在处理您的着色器视图的着色器中尝试直接设置颜色以进行测试。比如 return half4(1,0,0,0.5f); 正如我所说,您的顶点中没有定义纹理坐标。这就是为什么在 PS 中对纹理进行采样时没有任何反应的原因。第一次渲染到纹理通道和第二次渲染需要不同的顶点。粘贴完整的初始化代码,以便我可以尝试在此处进行修改。 他正在尝试使用第二次绘制的 x,y 坐标作为纹理坐标 【参考方案1】:

为了从顶点位置计算出正确的纹理坐标,您需要: 1.使用宽度和高度(不是一半)创建渲染目标纹理:

textureDesc.Width = Width; // /2; // Do not use half width
textureDesc.Height = Height; // /2;  // Do not use half height

2.在像素着色器中将位置划分为 float2(1 / Width, 1 / Height),如下所示:

float2 tex = input.Pos * float2(1.0f / 800.0f, 1.0f / 600.0f);
float4 diffuse = ObjTexture.Sample(ObjSamplerState, tex);
return input.Color + diffuse;

【讨论】:

这是什么原因:input.Pos * float2(1.0f / 800.0f, 1.0f / 600.0f); 将位置从 800x600 转换为 0-1 范围内。

以上是关于DirectX 11 渲染到纹理问题的主要内容,如果未能解决你的问题,请参考以下文章

DirectX 11 渲染到特定区域

使用所有 3 个参数调用时 DirectX Device::CreateTexture2D() 崩溃

DirectX11--深入理解与使用2D纹理资源

DirectX11--深入理解与使用2D纹理资源

DirectX11第六篇 纹理映射

DirectX11第六篇 纹理映射