DirecTX像素仅显示为(0.0F,0.0F)

发布于 2025-02-13 11:09:09 字数 1521 浏览 1 评论 0原文

在一些YouTube教程之后,我正在学习DirectX11和Winapi。我试图在(0.0F,0.0F)的显示屏上显示一些像素,并且可以正常工作,但是如果我更改坐标(例如,为(-0.5F,0.0F)) - 不。当我尝试显示多个像素时,它仅显示(0.0F,0.0F)的像素。

这是渲染方法:

void Graphics::RenderFrame()
{
    float bgcolor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
    this->deviceContext->ClearRenderTargetView(this->renderTargetView.Get(), bgcolor);

    this->deviceContext->IASetInputLayout(this->vertexshader.GetInputLayout());
    this->deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);

    this->deviceContext->VSSetShader(vertexshader.GetShader(), NULL, 0);
    this->deviceContext->PSSetShader(pixelshader.GetShader(), NULL, 0);

    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    this->deviceContext->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset);

    this->deviceContext->Draw(2,0);

    this->swapchain->Present(1, NULL);
}

顶点结构:

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

顶点标头:

struct Vertex
{
    Vertex()
    {

    }

    Vertex(float x, float y)
        : pos(x, y)
    {
    }

    DirectX::XMFLOAT2 pos;
};

顶点着色器:

float4 main(float2 inPos : POSITION) : SV_POSITION 
{
    return float4(inPos, 0, 1);
}

像素着色器:

float4 main() : SV_TARGET
{
    return float4(1.0f, 1.0f, 1.0f, 1.0f);
}

I'm learning a DirectX11 and WinAPI following some Youtube tutorial. I trying to show some pixel on a display at (0.0f, 0.0f) and it works, but if I change coordinate (for example to (-0.5f, 0.0f)) - it doesn't. And when I try to show more than one pixel it shows me pixel at (0.0f, 0.0f) only.

Here's the render method:

void Graphics::RenderFrame()
{
    float bgcolor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
    this->deviceContext->ClearRenderTargetView(this->renderTargetView.Get(), bgcolor);

    this->deviceContext->IASetInputLayout(this->vertexshader.GetInputLayout());
    this->deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);

    this->deviceContext->VSSetShader(vertexshader.GetShader(), NULL, 0);
    this->deviceContext->PSSetShader(pixelshader.GetShader(), NULL, 0);

    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    this->deviceContext->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset);

    this->deviceContext->Draw(2,0);

    this->swapchain->Present(1, NULL);
}

Vertex structure:

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

Vertex header:

struct Vertex
{
    Vertex()
    {

    }

    Vertex(float x, float y)
        : pos(x, y)
    {
    }

    DirectX::XMFLOAT2 pos;
};

Vertex shader:

float4 main(float2 inPos : POSITION) : SV_POSITION 
{
    return float4(inPos, 0, 1);
}

Pixel shader:

float4 main() : SV_TARGET
{
    return float4(1.0f, 1.0f, 1.0f, 1.0f);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对不⑦ 2025-02-20 11:09:09

从测试过程中,我没有从您提供的代码中看到任何问题。我第一次测试数据与您相同,但是由于像素太小,我找不到特定位置,所以我将Pixels Point稍微近一点,我可以清楚地看到它们已成功地绘制了窗口。

我建议您的坐标更加接近,并且最好使用d3d11_primisity_topology_pointlist。这是我的测试图片。您可以看到输出的三个像素。

{  0.0f,  -0.0f  },
{  0.0f,  -0.1f  },
{  0.0f,  -0.2f  },

以下是我的测试代码,希望它对您有所帮助。
https://gist.github.com/msmshazan/dfd5362004be37ff5e016b6a42be5083

static void RenderFrame()
{

    if (!render_occluded)
    {
        if (render_frame_latency_wait)
        {
            WaitForSingleObjectEx(render_frame_latency_wait, INFINITE, TRUE);
        }

#if WINDOW_DEPTH || WINDOW_STENCIL
        ID3D11DeviceContext_OMSetRenderTargets(render_context, 1, &render_window_rtview, render_window_dpview);
        ID3D11DeviceContext_OMSetDepthStencilState(render_context, render_depthstencil_state, 0);
        ID3D11DeviceContext_ClearDepthStencilView(render_context, render_window_dpview, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.f, 0);
#else
        ID3D11DeviceContext_OMSetRenderTargets(render_context, 1, &render_window_rtview, NULL);
#endif
        
        // clear background
        FLOAT clear_color[] = { 100.f / 255.f, 149.f / 255.f, 237.f / 255.f, 1.f };
        ID3D11DeviceContext_ClearRenderTargetView(render_context, render_window_rtview, clear_color);

        // draw a triangle
        const UINT stride = sizeof(struct Vertex);
        const UINT offset = 0;
        ID3D11DeviceContext_IASetInputLayout(render_context, render_input_layout);
        ID3D11DeviceContext_IASetVertexBuffers(render_context, 0, 1, &render_vertex_buffer, &stride, &offset);
        ID3D11DeviceContext_IASetPrimitiveTopology(render_context, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
        ID3D11DeviceContext_VSSetShader(render_context, render_vertex_shader, NULL, 0);
        ID3D11DeviceContext_PSSetShader(render_context, render_pixel_shader, NULL, 0);
        ID3D11DeviceContext_RSSetState(render_context, render_raster_state);
        ID3D11DeviceContext_OMSetBlendState(render_context, render_blend_state, NULL, ~0U);
        ID3D11DeviceContext_Draw(render_context, _countof(vertices), 0);
        
    }
}

struct Vertex
{
    float x, y;
    float r, g, b;
};

static const struct Vertex vertices[] =
{
    {  0.0f,  -0.0f, 1.f, 0.f, 0.f },
    {  0.0f,  -0.1f, 1.f, 0.f, 0.f },
    {  0.0f,  -0.3f, 0.f, 1.f, 1.f },

};

// You can compile shader to bytecode at build time, this will increase startup
// performance and also will avoid dependency on d3dcompiler dll file.
// To do so, put the shader source in shader.hlsl file and run these two commands:

// fxc.exe /nologo /T vs_4_0_level_9_0 /E vs /O3 /WX /Zpc /Ges /Fh d3d11_vshader.h /Vn d3d11_vshader /Qstrip_reflect /Qstrip_debug /Qstrip_priv shader.hlsl
// fxc.exe /nologo /T ps_4_0_level_9_0 /E ps /O3 /WX /Zpc /Ges /Fh d3d11_pshader.h /Vn d3d11_pshader /Qstrip_reflect /Qstrip_debug /Qstrip_priv shader.hlsl

// then set next setting to 1
#define USE_PRECOMPILED_SHADERS 0

#if USE_PRECOMPILED_SHADERS
#include "d3d11_vshader.h"
#include "d3d11_pshader.h"
#else
#pragma comment (lib, "d3dcompiler.lib")
static const char d3d11_shader[] =
"struct VS_INPUT                                \n"
"{                                              \n"
"  float2 pos : POSITION;                       \n"
"  float3 col : COLOR0;                         \n"
"};                                             \n"
"                                               \n"
"struct PS_INPUT                                \n"
"{                                              \n"
"  float4 pos : SV_POSITION;                    \n"
"  float3 col : COLOR0;                         \n"
"};                                             \n"
"                                               \n"
"PS_INPUT vs(VS_INPUT input)                    \n"
"{                                              \n"
"  PS_INPUT output;                             \n"
"  output.pos = float4(input.pos.xy, 0.f, 1.f); \n"
"  output.col = input.col;                      \n"
"  return output;                               \n"
"}                                              \n"
"                                               \n"
"float4 ps(PS_INPUT input) : SV_Target          \n"
"{                                              \n"
"  return float4(input.col, 1.f);               \n"
"}                                              \n";
#endif

From the process of my test, I didn't see any problem from the code you provided. The first time I tested the data was the same as you, but because the pixels were too small, I couldn't find the specific location, so I put the pixels Point a little closer and I can clearly see that they are successfully drawn in the window.

I suggest your coordinates to be closer, and better to use D3D11_PRIMITIVE_TOPOLOGY_POINTLIST. here is my test pic. You can see three pixels of the output.

enter image description here

{  0.0f,  -0.0f  },
{  0.0f,  -0.1f  },
{  0.0f,  -0.2f  },

The following is my test code, hope it will help you.
https://gist.github.com/msmshazan/dfd5362004be37ff5e016b6a42be5083

static void RenderFrame()
{

    if (!render_occluded)
    {
        if (render_frame_latency_wait)
        {
            WaitForSingleObjectEx(render_frame_latency_wait, INFINITE, TRUE);
        }

#if WINDOW_DEPTH || WINDOW_STENCIL
        ID3D11DeviceContext_OMSetRenderTargets(render_context, 1, &render_window_rtview, render_window_dpview);
        ID3D11DeviceContext_OMSetDepthStencilState(render_context, render_depthstencil_state, 0);
        ID3D11DeviceContext_ClearDepthStencilView(render_context, render_window_dpview, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.f, 0);
#else
        ID3D11DeviceContext_OMSetRenderTargets(render_context, 1, &render_window_rtview, NULL);
#endif
        
        // clear background
        FLOAT clear_color[] = { 100.f / 255.f, 149.f / 255.f, 237.f / 255.f, 1.f };
        ID3D11DeviceContext_ClearRenderTargetView(render_context, render_window_rtview, clear_color);

        // draw a triangle
        const UINT stride = sizeof(struct Vertex);
        const UINT offset = 0;
        ID3D11DeviceContext_IASetInputLayout(render_context, render_input_layout);
        ID3D11DeviceContext_IASetVertexBuffers(render_context, 0, 1, &render_vertex_buffer, &stride, &offset);
        ID3D11DeviceContext_IASetPrimitiveTopology(render_context, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
        ID3D11DeviceContext_VSSetShader(render_context, render_vertex_shader, NULL, 0);
        ID3D11DeviceContext_PSSetShader(render_context, render_pixel_shader, NULL, 0);
        ID3D11DeviceContext_RSSetState(render_context, render_raster_state);
        ID3D11DeviceContext_OMSetBlendState(render_context, render_blend_state, NULL, ~0U);
        ID3D11DeviceContext_Draw(render_context, _countof(vertices), 0);
        
    }
}

struct Vertex
{
    float x, y;
    float r, g, b;
};

static const struct Vertex vertices[] =
{
    {  0.0f,  -0.0f, 1.f, 0.f, 0.f },
    {  0.0f,  -0.1f, 1.f, 0.f, 0.f },
    {  0.0f,  -0.3f, 0.f, 1.f, 1.f },

};

// You can compile shader to bytecode at build time, this will increase startup
// performance and also will avoid dependency on d3dcompiler dll file.
// To do so, put the shader source in shader.hlsl file and run these two commands:

// fxc.exe /nologo /T vs_4_0_level_9_0 /E vs /O3 /WX /Zpc /Ges /Fh d3d11_vshader.h /Vn d3d11_vshader /Qstrip_reflect /Qstrip_debug /Qstrip_priv shader.hlsl
// fxc.exe /nologo /T ps_4_0_level_9_0 /E ps /O3 /WX /Zpc /Ges /Fh d3d11_pshader.h /Vn d3d11_pshader /Qstrip_reflect /Qstrip_debug /Qstrip_priv shader.hlsl

// then set next setting to 1
#define USE_PRECOMPILED_SHADERS 0

#if USE_PRECOMPILED_SHADERS
#include "d3d11_vshader.h"
#include "d3d11_pshader.h"
#else
#pragma comment (lib, "d3dcompiler.lib")
static const char d3d11_shader[] =
"struct VS_INPUT                                \n"
"{                                              \n"
"  float2 pos : POSITION;                       \n"
"  float3 col : COLOR0;                         \n"
"};                                             \n"
"                                               \n"
"struct PS_INPUT                                \n"
"{                                              \n"
"  float4 pos : SV_POSITION;                    \n"
"  float3 col : COLOR0;                         \n"
"};                                             \n"
"                                               \n"
"PS_INPUT vs(VS_INPUT input)                    \n"
"{                                              \n"
"  PS_INPUT output;                             \n"
"  output.pos = float4(input.pos.xy, 0.f, 1.f); \n"
"  output.col = input.col;                      \n"
"  return output;                               \n"
"}                                              \n"
"                                               \n"
"float4 ps(PS_INPUT input) : SV_Target          \n"
"{                                              \n"
"  return float4(input.col, 1.f);               \n"
"}                                              \n";
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文