Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Directx 像素着色器仅处理从顶点着色器传递的部分数据_Directx - Fatal编程技术网

Directx 像素着色器仅处理从顶点着色器传递的部分数据

Directx 像素着色器仅处理从顶点着色器传递的部分数据,directx,Directx,我使用DirectX11绘制场景。但我只有黑色的形状。然后,我使用MicrosoftVisualStudio的图形调试器查看数据过程。所有恒定缓冲区、输入缓冲区数据均已纠正,并从主存传输至GPU内存。但在像素着色器中,调试器仅处理环境光。另一个灯光和NoramlW、Eyeposition向量显示为“不在范围内”或“未使用”。指令指针跳过所有这些“不在范围内”变量程序行。结果是几乎没有环境光颜色(0.1+f),因此基本为黑色。当我将初始环境光增加到1.0f时,形状会有一些颜色。但是,整个像素着色器

我使用DirectX11绘制场景。但我只有黑色的形状。然后,我使用MicrosoftVisualStudio的图形调试器查看数据过程。所有恒定缓冲区、输入缓冲区数据均已纠正,并从主存传输至GPU内存。但在像素着色器中,调试器仅处理环境光。另一个灯光和NoramlW、Eyeposition向量显示为“不在范围内”或“未使用”。指令指针跳过所有这些“不在范围内”变量程序行。结果是几乎没有环境光颜色(0.1+f),因此基本为黑色。当我将初始环境光增加到1.0f时,形状会有一些颜色。但是,整个像素着色器的工作原理与我的预期不同

我检查了“不在范围内”的变量,没有一个拼写错误。 我不知道为什么。以下顶点着色器和像素着色器函数仅是旧版本中简单的基本修复函数渲染和骨骼动画。 我检查了GPU中的所有数据:常量缓冲区,调试器中的输入缓冲区数据是否正确。但它在逐步执行中显示“不在范围内”

cbuffer cbPerFrame : register(b0) {
float4x4 gViewProj;
DirectionalLight gDirLight[3];
float3 gEyePosW;
float pad;
float4 gFogColor;
float gFogStart;
float gFogRange;
};
cbuffer cbPerObject : register(b1) {
float4x4 gWorld;
float4x4 gWorldInvTranspose;
float4x4 gTexTransform;
Material gMaterial;
};

cbuffer cbAnimationBones : register(b2) {
float4x4 gBoneTransforms[58];
};
struct VertexIn {
float3 PosL         :POSITION;
float3 NormalL      :NORMAL;
float2 Tex          :TEXCOORD;
float4 TangentL     :TANGENT;
float3 Weights      :WEIGHTS;
uint4 BoneIndices   :BONEINDICES;
};

struct VertexOut {
float3 PosW             :   POSITION;
float3 NormalW          :   NORMAL;
float2 Tex              :   TEXCOORD;
float4 PosH             :   SV_POSITION;
};

VertexOut main( VertexIn vin) {
VertexOut vout;
float weights[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
float3 posL = float3(0.0f, 0.0f, 0.0f);
float3 normalL = float3(0.0f, 0.0f, 0.0f);
weights[0] = vin.Weights.x;
weights[1] = vin.Weights.y;
weights[2] = vin.Weights.z;
weights[3] = 1.0f - weights[0] - weights[1] - weights[2];

//blend skin vertex
[unroll]
for (int i = 0; i < 4; ++i) {
    posL += weights[i] * mul(float4(vin.PosL, 1.0f), gBoneTransforms[vin.BoneIndices[i]]).xyz;
    normalL += weights[i] * mul(vin.NormalL, (float3x3)gBoneTransforms[vin.BoneIndices[i]]);
}
//coordinates transformation
vout.PosW = mul(float4(posL, 1.0f), gWorld).xyz;
vout.NormalW = mul(normalL, (float3x3)gWorldInvTranspose);
vout.PosH = mul(float4(vout.PosW, 1.0f), gViewProj);

//transform texture
vout.Tex = mul(float4(vin.Tex, 0.0f, 1.0f), gTexTransform).xy;
return vout;
 }

struct PixelIn {
float3 PosW     :   POSITION;
float3 NormalW          :   NORMAL;
float2 Tex      :   TEXCOORD;
};
Texture2D gDiffuseMap : register(t0);
SamplerState samState : register(s0);

float4 main(PixelIn pin) : SV_TARGET
{

//the debugger will skip these vectors calculation
//since pin.NormalW, pin.PosW are not in scope.
// only pin.Tex has two values which means Vertex Shader has passed variable  here
pin.NormalW = normalize(pin.NormalW);
float3 toEye = gEyePosW - pin.PosW;
float distanceToEye = length(toEye);
toEye /= distanceToEye;

//the debugger start from the following line:
float4 texColor = float4(1, 1, 1, 1);
texColor = gDiffuseMap.Sample(samState, pin.Tex);

float4 lightColor = texColor;

float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 spec = float4(0.0f, 0.0f, 0.0f, 0.0f);

[unroll]
for (uint i = 0; i < 3; ++i) {
        float4 A, D, S;
    //the debugger will skip A assignment since it says A is not in scope
    A = float4(0.0f, 0.0f, 0.0f, 0.0f);
    D = float4(0.0f, 0.0f, 0.0f, 0.0f);
    S = float4(0.0f, 0.0f, 0.0f, 0.0f);
    ComputeDirectionalLight(gMaterial, gDirLight[i], pin.NormalW, toEye, A, D, S);

// the gMaterial only gMateria.ambient has value, the rest of part "not in scope, but I can see the whole values passed correctly in the GPU object.
// The computeDirectinalLight function only calculate the ambient light since the Normal, eye, vectors "not in scope"

    ambient += A;
    diffuse += D;
    spec    += S;
}
lightColor = texColor * (ambient + diffuse) + spec;
lightColor.a = gMaterial.Diffuse.a * texColor.a;
return lightColor;
}

struct Material {
float4 Ambient;
float4 Diffuse;
float4 Specular;
float4 Reflect;
};
struct DirectionalLight {
float4 Ambient;
float4 Diffuse;
float4 Specular;
float3 Direction;
float pad;
};

void ComputeDirectionalLight(Material mat, DirectionalLight L, float3 normal, float3 toEye, out float4 ambient, float4 diffuse, float4 spec) {

// the debugger skip the following line since L.Direction vector "not in scope"
float3 lightVec = -L.Direction;

//the debugger only execute the following one line
ambient = mat.Ambient * L.Ambient;

 //the debugger return here and skip all the following lines:
float diffuseFactor = dot(lightVec, normal);
[flatten]
if (diffuseFactor > 0.0f) {
    float3 v = reflect(-lightVec, normal);
    float specFactor = pow(max(dot(v, toEye), 0.0f), mat.Specular.w);

    diffuse = diffuseFactor * mat.Diffuse * L.Diffuse;
    spec = specFactor * mat.Specular * L.Specular;
}
}
cbuffer cbPerFrame:寄存器(b0){
浮动4x4 gViewProj;
定向光gDirLight[3];
3.gEyePosW;
浮垫;
漂浮4 gFogColor;
浮动启动;
浮动范围;
};
cbuffer cbPerObject:寄存器(b1){
浮动4x4gworld;
浮动4x4转座子;
浮动4x4 gTexTransform;
材料材料;
};
cbuffer cbAnimationBones:寄存器(b2){
浮动4x4 gBoneTransforms[58];
};
结构蛋白{
浮动3位:位置;
float3 NORMAL:正常;
浮动2 Tex:TEXCOORD;
浮动4切线:切线;
浮动3个权重:权重;
uint4骨指数:骨指数;
};
结构顶点输出{
浮动3位:位置;
float3 NormalW:正常;
浮动2 Tex:TEXCOORD;
浮动4豪华:SV_位置;
};
VertexOut主(VertexIn vin){
垂直输出;
浮动权重[4]={0.0f,0.0f,0.0f,0.0f};
浮动3位置=浮动3(0.0f,0.0f,0.0f);
浮动3法线=浮动3(0.0f,0.0f,0.0f);
权重[0]=车辆识别码权重.x;
权重[1]=车辆识别号权重.y;
权重[2]=车辆识别号权重.z;
权重[3]=1.0f-权重[0]-权重[1]-权重[2];
//混合蒙皮顶点
[展开]
对于(int i=0;i<4;++i){
posL+=权重[i]*mul(float4(vin.posL,1.0f),gBoneTransforms[vin.BoneIndices[i]]).xyz;
normalL+=权重[i]*mul(vin.normalL,(float3x3)gBoneTransforms[vin.BoneIndicates[i]];
}
//坐标变换
vout.PosW=mul(float4(posL,1.0f),gWorld).xyz;
vout.NormalW=mul(normalL,(float3x3)gworldinvse);
vout.PosH=mul(浮动4(vout.PosW,1.0f),gViewProj);
//变换纹理
vout.Tex=mul(浮动4(vin.Tex,0.0f,1.0f),gTexTransform).xy;
退票;
}
结构像素{
浮动3位:位置;
float3 NormalW:正常;
浮动2 Tex:TEXCOORD;
};
Texture2D gDiffuseMap:寄存器(t0);
采样器状态samState:寄存器(s0);
float4 main(像素引脚):SV_目标
{
//调试器将跳过这些向量的计算
//由于pin.NormalW,pin.PosW不在范围内。
//只有pin.Tex有两个值,这意味着顶点着色器在此处传递了变量
pin.NormalW=规格化(pin.NormalW);
float3-toEye=gEyePosW-pin.PosW;
浮动距离toEye=长度(toEye);
toEye/=距离toEye;
//调试器从以下行开始:
float4 texColor=float4(1,1,1,1);
texColor=gDiffuseMap.Sample(samState,pin.Tex);
float4 lightColor=texColor;
浮动4环境=浮动4(0.0f,0.0f,0.0f,0.0f);
float4漫反射=float4(0.0f,0.0f,0.0f,0.0f);
浮动4规格=浮动4(0.0f、0.0f、0.0f、0.0f);
[展开]
对于(uint i=0;i<3;++i){
漂浮物4 A、D、S;
//调试器将跳过赋值,因为它说A不在范围内
A=浮动4(0.0f,0.0f,0.0f,0.0f);
D=浮动4(0.0f,0.0f,0.0f,0.0f);
S=浮动4(0.0f,0.0f,0.0f,0.0f);
计算方向光(gMaterial,gDirLight[i],pin.NormalW,toEye,A,D,S);
//gMaterial only gMateria.ambient有值,其余部分“不在范围内,但我可以看到GPU对象中正确传递的全部值。
//ComputedDirectionalLight函数仅计算环境光,因为法线、眼睛、向量“不在范围内”
环境+=A;
漫反射+=D;
spec+=S;
}
lightColor=texColor*(环境光+漫反射)+规格;
lightColor.a=材质.Diffuse.a*texColor.a;
返回浅色;
}
结构材料{
浮动4环境;
4弥漫性;
4镜面反射;
浮动4反映;
};
结构定向光{
浮动4环境;
4弥漫性;
4镜面反射;
浮动方向3;
浮垫;
};
void计算方向光(材质垫、方向光L、float3法线、float3 toEye、out float4环境光、float4漫反射、float4规范){
//调试器跳过以下行,因为L.方向向量“不在范围内”
float3 lightVec=-L.方向;
//调试器只执行以下一行
环境=材料环境*L.环境;
//调试器将返回此处并跳过以下所有行:
浮动扩散因子=点(lightVec,正常);
[变平]
如果(扩散系数>0.0f){
浮动3 v=反射(-lightVec,正常);
浮动镜面反射系数=功率(最大值(点(v,toEye),0.0f),材料镜面反射w);
漫反射=漫反射系数*材料漫反射*L.漫反射;
规格=镜面系数*材料镜面反射*L.镜面反射;
}
}
我自己买的: 在函数“ComputerDirectionalLight”函数中,我忘记在漫反射之前添加“out”关键字

void ComputeDirectionalLight(Material mat, DirectionalLight L, float3 normal, float3 toEye, out float4 ambient, float4 diffuse, float4 spec) 
应该是:

void ComputeDirectionalLight(Material mat, DirectionalLight L, float3 normal, float3 toEye, out float4 ambient, out float4 diffuse, out float4 spec)