我目前正在学习GLSL.看起来,一旦你学习了一种着色器语言,学习其中一种语言并不会太困难.是否类似于学习像wxWidgets这样的小部件工具集然后切换到Qt?一旦你了解了一个小部件工具集中发生的事情,另一个工具集会做类似的事情,因为它们最终在某些时候做了几乎相同的事情?从一种着色器语言到另一种着色器语言有什么经验?
嗨大家这应该是一个简单的任务,但由于某种原因,我没有得到它...
我只想使用着色器获取场景深度的可视化:
float4x4 matViewProjection;
float4x4 matWorld;
float4 eyePos;
struct VS_OUTPUT
{
float4 position : POSITION0;
float depth : TEXCOORD0;
};
VS_OUTPUT vs_main( VS_INPUT input )
{
VS_OUTPUT output;
output.position = mul( input.position, matViewProjection );
float3 posWorld = mul(input.position, matWorld);
output.depth = distance(posWorld, eyePos);
return( output );
}
Run Code Online (Sandbox Code Playgroud)
为了获得深度值(或者我认为),我计算了世界空间中的位置与视图位置之间的距离.
和相应的像素着色器
float4 ps_main(VS_OUTPUT input) : COLOR0
{
float depth = input.depth;
return float4(depth, depth, depth, 1.0f);
}
Run Code Online (Sandbox Code Playgroud)
除了白色之外什么也没有结果
所以我开始尝试将错误值乘以深度:
float depth = input.depth * 0.005f;
Run Code Online (Sandbox Code Playgroud)
根据到物体的距离给出令人满意的结果.因此,如果我靠近对象,我将不得不再次调整该值.
所以这是非常错误的......
谢谢阅读!
我一直在努力寻找任何资源来帮助我使用高级着色器语言和DirectX 9托管库编写简单的辉光/模糊着色器.
我需要做的就是将一系列CustomVertex.TransformedColored顶点绘制成简单的线条,然后通过HLSL效果模糊/发光.
我已经在互联网上搜索了大约三天的一些结果,但我找不到一个非常好的教程或示例.我对HLSL有一个基本的了解,但我不太了解如何编写这个着色器(我已经阅读了3本DirectX书籍中的HLSL章节).
这是一些(删节)代码:
CustomVertex.TransformedColored[] glowVertices = new CustomVertex.TransformedColored[4];
glowVertices[0] = new CustomVertex.TransformedColored(random.Next(this.render.Width), random.Next(this.render.Height), 1, 1, Color.Cyan.ToArgb());
glowVertices[1] = new CustomVertex.TransformedColored(random.Next(this.render.Width), random.Next(this.render.Height), 1, 1, Color.Blue.ToArgb());
glowVertices[2] = new CustomVertex.TransformedColored(random.Next(this.render.Width), random.Next(this.render.Height), 1, 1, Color.Cyan.ToArgb());
glowVertices[3] = new CustomVertex.TransformedColored(random.Next(this.render.Width), random.Next(this.render.Height), 1, 1, Color.Blue.ToArgb());
this.device.BeginScene();
int passes = this.glowEffect.Begin(0);
for (int i = 0; i < passes; i++)
{
this.glowEffect.BeginPass(i);
this.device.DrawUserPrimitives(PrimitiveType.LineStrip, glowVertices.Length - 1, glowVertices);
this.glowEffect.EndPass();
}
this.glowEffect.End();
this.device.EndScene();
Run Code Online (Sandbox Code Playgroud)
我想我不是在寻找HLSL特定部分的帮助,考虑到我要发布的问题和代码数量,我真的只是寻找一些帮助寻找资源!
晚上好,
我正在尝试将XMFLOAT3X3发送到常量缓冲区(请参阅下面的代码).
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof(XMFLOAT3X3);
constDesc.Usage = D3D11_USAGE_DEFAULT;
result = m_pDevice->CreateBuffer(&constDesc,0,&m_pTexTransformCB);
if (FAILED(result)) {
MessageBoxA(NULL,"Error creating constant buffer m_pTexTransformCB", "Error", MB_OK);
return false;
}
Run Code Online (Sandbox Code Playgroud)
但编译器告诉我XMFLOAT3X3是常量缓冲区字节宽度的无效维度:
D3D11: ERROR: ID3D11Device::CreateBuffer: The Dimensions are invalid. For ConstantBuffers, marked with the D3D11_BIND_CONSTANT_BUFFER BindFlag, the ByteWidth (value = 36) must be a multiple of 16 and be less than or equal to 65536. [ STATE_CREATION ERROR #66: CREATEBUFFER_INVALIDDIMENSIONS ]
Run Code Online (Sandbox Code Playgroud)
但是,我对HLSL并不熟悉,所以我不确定是否将字节宽度设置为48,着色器的cbuffer中的float3x3将正确注册.我该怎样处理这个最好的?
如果您需要更多信息,请发表评论,我将编辑问题.我希望它足够清楚.
我在我的 Effect 文件中创建了一个雾功能..在像素着色器中,我计算了相机位置和输入位置的距离,如下所示:
float x = distance(_in.pos3d, CameraPosition);
float fd;
if(Fog)
{
if(x > FogDistance)
fd = ((x-FogDistance) * FogIntensity > 100) ? 100 : ((x-FogDistance) * FogIntensity);
//_in.color = ???;
}
Run Code Online (Sandbox Code Playgroud)
其中:x 是相机位置和顶点位置的距离,fd 是雾颜色的百分比,FogDistance 是物体不受雾影响的距离,FogIntensity 是雾的强度。
我想要的是一种包含 (fd %) 雾色的颜色。例如,如果雾颜色为橙色,输入为白色,fd 为 25%,则生成的颜色将为白色 + 25% 橙色。
编辑:顺便说一下,对于无法维护的设置代码,我深表歉意。
编辑 2:我注意到有两个透明层有一个透明的结果,所以我稍微清理了这个问题。顶点输入没有百分比。
有没有办法将屏幕纹理存储在HLSL纹理变量中,而不将其从C#代码中传递出来?
例如,可以通过使用来访问当前的drowing纹理
sampler TextureSampler : register(s0);
Run Code Online (Sandbox Code Playgroud)
有什么方法可以对目前的scrren做同样的事情吗?
MSDN表示您可以将.hlsl文件编译为头文件中定义的字节数组.这是他们给出的代码.
#include "PixelShader.h"
ComPtr<ID3D11PixelShader> m_pPixelShader;
hr = pDevice->CreatePixelShader(g_psshader,
sizeof(g_psshader), nullptr, &m_pPixelShader);
Run Code Online (Sandbox Code Playgroud)
g_psshader字节数组也是如此.但是如何定义g_psshader呢?我无处可寻找谈论这个问题.我尝试了一些方法,但都失败了.MSDN提供了媒体扩展示例.但是该样本中没有PixelShader.h.
如果你错过了这个问题:如何在上面的代码中定义g_psshader(我的意思是在标题PixelShader.h中).
我刚开始学习DirectX编程,使用F#和SharpDX作为.NET包装器.作为测试用例,我渲染了Mandelbrot集.使用2个计算着色器完成计算.
第一个着色器计算每个像素的深度(函数"CalcMandel"),结果存储在RWStructuredBuffer中.这种计算需要大量的单次或双次乘法,但它在我的GPU(AMD 7790)上的速度非常快."CalcMandel"具有该属性
[numthreads(16, 16, 1)]
Run Code Online (Sandbox Code Playgroud)
并通过发送
context.Dispatch (imageWidth / 16, imageHeight / 16, 1)
Run Code Online (Sandbox Code Playgroud)
这里没有问题 - "核心"Mandelbrot集的1000 x 800像素图像以超过1000 fps的速度运行(在GPU上使用单精度).
第二个着色器几乎没有做任何事情:它计算先前计算的最小值,最大值和平均值(函数"CalcMinMax")."CalcMinMax"具有该属性
[numthreads(1, 1, 1)]
Run Code Online (Sandbox Code Playgroud)
并被称为通过
context.Dispatch (1,1,1)
Run Code Online (Sandbox Code Playgroud)
对于当时给定的图像大小,单个GPU线程必须遍历超过800.000个整数的缓冲区以计算最小值,最大值和平均值.我使用单个线程,因为我不知道如何以并行方式实现此计算.
问题:"CalcMinMax"非常慢:帧速率从1000 fps下降到5 fps!
我的问题:这里有什么问题?我使用了错误的设置/参数(numthreads)吗?如何加快min-max计算?
我的想法:我的第一个假设是访问RWBuffer可能会很慢 - 事实并非如此.当我用常量替换缓冲区访问时,帧速率没有增加.
我的GPU有appr.900个着色器核心并使用数千个线程来计算Mandelbrot集合,而"CalcMinMax"仅使用一个线程.然而,我仍然不明白为什么事情变得如此缓慢.
我很感激任何建议!
================================================
// HLSL CONTENT(省略Mandelbrot集计算):
cbuffer cbCSMandel : register( b0 )
{
double a0, b0, da, db;
double ja0, jb0;
int max_iterations;
bool julia; int cycle;
int width; int height;
double colorFactor;
int algoIndex;
int step;
};
struct statistics
{
int minDepth;
int …Run Code Online (Sandbox Code Playgroud) 我有一个HLSL像素着色器:
struct PixelShaderInput
{
float4 pos : SV_POSITION;
float2 texCoord : TEXCOORD0;
};
Texture2D s_texture : register(t0);
cbuffer ColorConstantBuffer : register(b1)
{
float4 m_color;
};
SamplerState s_sampleParams
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
float4 main(PixelShaderInput input) : SV_TARGET
{
float4 t = s_texture.Sample(s_sampleParams, input.texCoord);
return t * m_color;
}
Run Code Online (Sandbox Code Playgroud)
它在视觉上工作正常,但垃圾邮件输出带有警告:
D3D11警告:ID3D11DeviceContext :: Draw:像素着色器单元期望采样器设置在插槽0,但没有绑定.这是完全有效的,因为NULL Sampler映射到默认的Sampler状态.但是,开发人员可能不希望依赖默认值.[执行警告#352:DEVICE_DRAW_SAMPLER_NOT_SET]
正如你所看到的,我已经设定了s_sampleParams所以有人可以解释什么是错的吗?
我找不到在Visual Studio 2017中启动HLSL调试器的选项.
Microsoft文档指示从Graphics Pipeline Stages窗口或Graphics Pixel History启动它
https://msdn.microsoft.com/en-us/library/hh873197.aspx
但是,我不知道它们是什么或如何到达它们
当我直接在visual studio IDE中的hlsl代码中设置断点时,它只会The Breakpoint will not currently be hit在空的红色圆圈上显示" ".我假设我需要在hlsl调试器中打开hlsl代码,以便它在行中断开.
我正在DirectX 11 App(Universal Windows)x64调试模式下运行一个新的项目模板,并在顶点和像素着色器中设置断点.
谢谢,