【发布时间】:2018-07-01 19:28:11
【问题描述】:
我正在尝试使用 SharpDX 学习 Direct3d 编程,并且正在研究 Internet 上的各种示例以及 Frank Luna 的使用 DirectX 11 进行 3D 游戏编程的介绍。目前我正在尝试传递多个光源(定向、点和 Spotlight)在我的像素着色器中使用数组。现在,据我了解 HLSL 中打包的工作原理,一切都应该正常工作,但当然不是,我似乎无法找到问题所在。
在我的 HLSL 着色器代码中,我的照明结构具有以下结构定义:
#define NUM_LIGHTS 1 //Max number of lights in scene
struct DirectionalLight {
float4 Ambient;
float4 Diffuse;
float4 Specular;
float3 Direction;
float pad;
};
struct PointLight {
float4 Ambient;
float4 Diffuse;
float4 Specular;
float3 Position;
float Range;
float3 Attentuation;
float pad;
};
struct SpotLight {
float4 Ambient;
float4 Diffuse;
float4 Specular;
float3 Position;
float Range;
float3 Direction;
float Spot;
float3 Attentuation;
float pad;
};
正如您所见,填充变量都在那里,以确保所有内容都整齐地打包到 float4 中并遵守 16 字节边界。
然后在我的 C# 代码中,我有以下相应的结构:
[StructLayout(LayoutKind.Sequential, Size=64)]
public struct DirectionalLight
{
public Color4 Ambient;
public Color4 Diffuse;
public Color4 Specular;
public Vector3 Direction;
public float pad;
}
[StructLayout(LayoutKind.Sequential, Size=96)]
public struct Spotlight
{
public Color4 Ambient;
public Color4 Diffuse;
public Color4 Specular;
public Vector3 Position;
public float Range;
public Vector3 Direction;
public float Spot;
public Vector3 Attentuation;
public float pad;
}
[StructLayout(LayoutKind.Sequential, Size=80)]
public struct PointLight
{
public Color4 Ambient;
public Color4 Diffuse;
public Color4 Specular;
public Vector3 Position;
public float Range;
public Vector3 Attentuation;
public float pad;
}
在 HLSL 中,我的常量缓冲区设置如下:
cbuffer cbPerFrame : register(b1) //register b0 used for cbuffer cbPerObject //(world, view, projection matrices)
{
DirectionalLight gDirLight[NUM_LIGHTS];
SpotLight gSpotLight[NUM_LIGHTS];
PointLight gPointLight[NUM_LIGHTS];
float3 cameraPosition;
float fogStart;
float fogEnd;
float3 pad;
};
又是在 C# 中:
[StructLayout(LayoutKind.Sequential, Size=272)]
public struct ConstantBufferPerFrame
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public DirectionalLight[] DirectionalLight;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public Spotlight[] SpotLight;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public PointLight[] PointLight;
public Vector3 CameraPosition;
public float FogStart;
public float FogEnd;
public Vector3 pad;
}
请注意,现在我试图让它尽可能简单,每种类型只传递 1 个光(这就是 SizeConst=1 的原因)
然后,我在 C# 中创建我的常量缓冲区,如下所示:
...
int size = Marshal.SizeOf(typeof(ConstantBufferPerFrame));
_constantBufferPerFrame = new SharpDX.Direct3D11.Buffer(device, size,
ResourceUsage.Dynamic, BindFlags.ConstantBuffer,
CpuAccessFlags.Write, ResourceOptionFlags.None, 0);
...
_context.PixelShader.SetConstantBuffer(1, _constantBufferPerFrame);
...
我按如下方式写入缓冲区:
...
DataStream mappedResource;
context.MapSubresource(_constantBufferPerFrame, 0, MapMode.WriteDiscard, MapFlags.None, out mappedResource);
mappedResource.Write(_perFrameBuffer);
context.UnmapSubresource(_constantBufferPerFrame, 0);
context.PixelShader.SetShaderResource(0, texture);
context.DrawIndexed(indexCount, 0, 0);
...
现在这一切看起来应该可以工作了,但是当我运行我的程序时,常量缓冲区完全错误。
例如,如果 C# ConstantBuffer 对象包含以下值:
ConstantBuffer
{
DirectionalLight:
{
Ambient: {Alpha:1, Red:100, Green:100, Blue:100},
Diffuse: {Alpha:1, Red:0, Green:0, Blue:0},
Specular: {Alpha:1, Red:0, Green:0, Blue:0},
Direction: {X:0, Y:0, Z:0}
},
PointLight:
{
Ambient: {Alpha:1, Red:10.7, Green:0.7, Blue:0.7},
Diffuse: {Alpha:1, Red:0, Green:0, Blue:0},
Specular: {Alpha:1, Red:0, Green:0, Blue:0},
Position: {X:201.0751 Y:6.075078 Z:2145},
Range: 100
Attentuation: {X:0 Y:1 Z:0}
Pad: 0
},
Spotlight:
{
Ambient: {Alpha:1, Red:10.7, Green:0.7, Blue:0.7},
Diffuse: {Alpha:1, Red:0, Green:0, Blue:0},
Specular: {Alpha:1, Red:0, Green:0, Blue:0},
Direction: {X:0, Y:0, Z:0}
Position: {X:0, Y:0, Z:0}
Range: 0,
Spot: 0,
Attentuation: {x:0, Y:0, Z:0},
Pad: 0
},
CameraPosition: {X:195, Y:16, Z:2145},
FogStart: 50,
FogEnd: 1000,
Pad: {X:0, Y:0, Z:0}
}
在图形分析器中,我得到以下编译器输出:
// cbuffer cbPerFrame
// {
//
// struct DirectionalLight
// {
//
// float4 Ambient; // Offset: 0
// float4 Diffuse; // Offset: 16
// float4 Specular; // Offset: 32
// float3 Direction; // Offset: 48
// float pad; // Offset: 60
//
// } gDirLight; // Offset: 0 Size: 64
//
// struct SpotLight
// {
//
// float4 Ambient; // Offset: 64
// float4 Diffuse; // Offset: 80
// float4 Specular; // Offset: 96
// float3 Position; // Offset: 112
// float Range; // Offset: 124
// float3 Direction; // Offset: 128
// float Spot; // Offset: 140
// float3 Attentuation; // Offset: 144
// float pad; // Offset: 156
//
// } gSpotLight; // Offset: 64 Size: 96
//
// struct PointLight
// {
//
// float4 Ambient; // Offset: 160
// float4 Diffuse; // Offset: 176
// float4 Specular; // Offset: 192
// float3 Position; // Offset: 208
// float Range; // Offset: 220
// float3 Attentuation; // Offset: 224
// float pad; // Offset: 236
//
// } gPointLight; // Offset: 160 Size: 80
// float3 cameraPosition; // Offset: 240 Size: 12 [unused]
// float fogStart; // Offset: 252 Size: 4 [unused]
// float fogEnd; // Offset: 256 Size: 4 [unused]
// float3 pad; // Offset: 260 Size: 12 [unused]
//
// }
但是,常量缓冲区具有以下值:
#,float
"0","-8.0022389e-09"
"1","+9.9492191e-43"
"2","-8.0024094e-09"
"3","+9.9492191e-43"
"4","-8.002317e-09"
"5","+9.9492191e-43"
"6","+50"
"7","+1000"
"8","+195"
"9","+16"
"10","+2145"
"11","+0"
"12","+0"
"13","+0"
"14","+0"
"15","+0"
"16","+0"
"17","+0"
"18","+0"
"19","+0"
"20","+0"
"21","+0"
"22","+0"
"23","+0"
"24","+0"
"25","+0"
"26","+0"
"27","+0"
"28","+0"
"29","+0"
"30","+0"
"31","+0"
"32","+0"
"33","+0"
"34","+0"
"35","+0"
"36","+0"
"37","+0"
"38","+0"
"39","+0"
"40","+0"
"41","+0"
"42","+0"
"43","+0"
"44","+0"
"45","+0"
"46","+0"
"47","+0"
"48","+0"
"49","+0"
"50","+0"
"51","+0"
"52","+0"
"53","+0"
"54","+0"
"55","+0"
"56","+0"
"57","+0"
"58","+0"
"59","+0"
"60","+0"
"61","+0"
"62","+0"
"63","+0"
"64","+0"
"65","+0"
"66","+0"
"67","+0"
如您所见,CameraPosition 和 Fog End/Start 值已正确编组(或者是这样吗?当我再次查看它时,我发现位置已被交换,我预计浮点数 6,7 中的 CameraPosition 变量和 8 和 2 雾漂浮在位置 9 和 10),只是阵列灯结构似乎有点不稳定。
我一定是在什么地方遗漏了什么,谁能帮帮我?
【问题讨论】:
标签: c# directx-11 hlsl sharpdx packing