简体   繁体   English

D3D11 Multiple Vertex Buffer 2D Depth sorting with depth buffer 深度缓冲

[英]D3D11 Multiple Vertex Buffer 2D Depth sorting with depth buffer

I am trying to create some 2D UI drawing in D3D11.我正在尝试在 D3D11 中创建一些 2D UI 绘图。 I am trying to get the draw calls down as much as possible to improve performance.我正在尝试尽可能减少绘制调用以提高性能。 Previously, I batched up as many textures as possible to send to one draw call, they were accessed with an else if.以前,我将尽可能多的纹理打包发送到一个绘制调用,它们是通过 else if 访问的。 Well it turned out that sending many textures, especially large ones, to a single shader destroyed performance in some scenarios which is exactly the opposite of what I was trying to achieve.事实证明,在某些情况下,将许多纹理(尤其是大纹理)发送到单个着色器会破坏性能,这与我试图实现的目标正好相反。

The goal is to render things back to front from 2 VBOs.目标是从 2 个 VBO 渲染事物。 So now, I am trying split some things up into a few vertex buffers.所以现在,我正在尝试将一些东西分成几个顶点缓冲区。 I have one vertex buffer that contains non textured vertices, and one that contains different textures.我有一个包含非纹理顶点的顶点缓冲区,以及一个包含不同纹理的顶点缓冲区。 This is because I send one texture to the shader at a time, and want to keep the amount of texture swapping between vertices being drawn at a minimum.这是因为我一次将一个纹理发送到着色器,并希望将绘制的顶点之间的纹理交换量保持在最低限度。 The problem I am encountering is that the depth sorting does not work as I want it to.我遇到的问题是深度排序没有按我的意愿工作。 I render the textured vertex buffer first, then the non textured one.我首先渲染纹理顶点缓冲区,然后渲染非纹理顶点缓冲区。 When I try to render textures over something rendered in the non textured vertex buffer, it simply doesn't work.当我尝试在非纹理顶点缓冲区中渲染的东西上渲染纹理时,它根本不起作用。 There may be some other strange effects that I cant quite explain.可能还有其他一些我无法解释的奇怪效果。

Maybe this is something with my alpha blending?也许这与我的 alpha 混合有关? It seems to me that the Z value I write has no effect on the output.在我看来,我写的 Z 值对输出没有影响。 Here is the relevant code:这是相关代码:

desc->AlphaToCoverageEnable = false;
desc->RenderTarget[0].BlendEnable = true;
desc->RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc->RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc->RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc->RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
desc->RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
desc->RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc->RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
d3d->device->CreateBlendState(desc.get(), &d3d->blend_state);

I also tried Depth -= 1.0f and keeping the depth between 0.0 and 1.0 here to no effect我还尝试了Depth -= 1.0f并在此处将深度保持在 0.0 和 1.0 之间没有效果

void put(float x, float y, float u, float v) {
    CurrentVBO->put({ {x,y, Depth }, current_color, {u,v}, Scale });
    Depth += 1.0f;
}

vertex shader顶点着色器

cbuffer vertex_buffer : register(b0) {
    float4x4 projection_matrix;
    float render_time;
};

struct VS_INPUT {
    float3 pos : POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD;
    float scale : SCALE;
};

struct PS_INPUT {
    float4 pos : SV_Position;
    float4 color : COLOR;
    float2 uv : TEXCOORD;
    uint tex_id : TEXID;
};

PS_INPUT main(VS_INPUT input) {
    PS_INPUT output;
    output.pos = mul(projection_matrix, float4(input.pos.xy * input.scale, input.pos.z, 1.0f));
    output.color = input.color;
    output.uv = input.uv;
    return output;
}
desc->DepthEnable = true;
desc->DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
desc->DepthFunc = D3D11_COMPARISON_LESS;

desc->StencilEnable = false;
desc->FrontFace.StencilFailOp = desc->FrontFace.StencilDepthFailOp = desc->FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
desc->FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

desc->BackFace = desc->FrontFace;

d3d->device->CreateDepthStencilState(desc.get(), &d3d->depth_stencil_2d);

After some more reading, I may need to do some more work to make this work properly.在阅读更多内容之后, 我可能需要做更多的工作才能使它正常工作。

Since the problems of blending and ordering transparent objects with depth.由于混合和排序具有深度的透明对象的问题。 I went with my original plan, but only allow a few textures to be passed to one shader.我按照最初的计划进行,但只允许将一些纹理传递给一个着色器。 I also removed the high resolution textures as I think they were causing bandwidth problems in general, as the GPU is already being heavily strained in my case.我还删除了高分辨率纹理,因为我认为它们通常会导致带宽问题,因为在我的情况下 GPU 已经非常紧张。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM