了解着色器编程 [英] Understanding Shader Programming

查看:135
本文介绍了了解着色器编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解着色器编程,但是在这一点上,文档将无法进一步帮助我。

I am trying to understand shader programming, but at this point, documentation wont help me further.

1] 是否数据类型&缓冲区的大小必须匹配吗?

1] Does the data type & size of the buffers have to match?

在DX SDK的DX教程4中,它们具有一个结构:

In the DX tutorial 4 from the DX SDK, they have a struct:

struct SimpleVertex{
    XMFLOAT3 Pos;
    XMFLOAT4 Color;
};

在着色器文件中,他们定义:

While in their shader file, they define:

struct VS_OUTPUT{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR0;
};

他们将Pos定义为一个文件中3的向量,而另一个文件中4的向量。这如何正确?我认为数据大小必须匹配。

They define Pos as a vector of 3 in one file, while it is 4 in another. How is this correct? I thought the size of the data have to match.

2] 要创建另一个常量缓冲区,这是我需要做的步骤吗?

2] To create another constant buffer, is this the steps I need to do?

// Make in shader file
cbuffer LightBuffer : register(b0){
    float3 lDir;
    float4 lColor;
}

// Make in C++ file
struct LightBuffer{
    XMFLOAT3 Direction;
    XMFLOAT4 Color;
};

    ...

LightBuffer lb;
lb.Direction=XMFLOAT3(-1.0f, -10.0f, 4.0f); // Make an instance of it
lb.Color=XMFLOAT4(0.35f, 0.5f, 1.0f, 1.0f);

    ...

ID3D11Buffer* lightBuffer=NULL; // Declare in global scope

D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage=D3D11_USAGE_DEFAULT;
bd.ByteWidth=sizeof(LightBuffer);
bd.BindFlags=D3D11_BIND_CONSTANT_BUFFER;

hr=graphics->deviceInterface->CreateBuffer(&bd, NULL, &lightBuffer);

graphics->deviceContext->UpdateSubresource(lightBuffer, 0, NULL, &lb, 0, 0);

graphics->deviceContext->PSSetConstantBuffers(0, 1, &lightBuffer);

这些是我执行的步骤,类似于本教程中的常量缓冲区。

These are the steps I did, which was similar to the constant buffer in the tutorial. It ends up producing nothing.

我偶然发现,如果将 LightBuffer :: XMFLOAT3 Direction 的类型更改为XMFLOAT4,则可以正常工作。我不明白什么?为什么我不能拥有自己想要的类型?

I found out by accident that if I change the type of LightBuffer::XMFLOAT3 Direction to XMFLOAT4, it works. What am I not understanding? Why cant I have the type I wish?

感谢阅读。

推荐答案

1-从结构名称中可以看到,它不是顶点着色器的输入,而是输出。顶点着色器应将位置变量输出为4个浮点数(同质坐标)。因此,在着色器文件中的某处,应该有一个将向量扩展为float4变量的操作(类似 float4(inputPos,1.0);之类的东西)。

1-As you can see from the struct's name, it is not the input but the output of the vertex shader. Vertex shader should output position variable as 4 floats(homogeneus coordinates). So somewhere in the shader file, there should be an operation which expands the vector to a float4 variable(something like "float4(inputPos, 1.0);").

2 -这可能是对齐问题。 GPU旨在与4D向量配合使用。使用常量缓冲区时,请尝试先创建矩阵,然后再创建4D变量,然后再创建3D变量,依此类推。或者,您可以像说的那样添加额外的未使用的填充字节。如果非4D向量过多,则可以使用 packoffset关键字将它们打包在一个插槽中,以免浪费GPU寄存器。详细说明在这里:
http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb509581(v = vs.85).aspx

2-It's probably an alignment issue. GPU's are designed to work with 4D vectors. While using constant buffers, try to create your structures with matrices first, 4D variables second, 3D variables third, and so on. Or you can add extra unused padding bytes like you said you did. If you have too much non-4D vectors, you can pack them in one slot with 'packoffset' keyword for not wasting GPU registers. Detailed explanation is here: http://msdn.microsoft.com/en-us/library/windows/desktop/bb509581(v=vs.85).aspx

这篇关于了解着色器编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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