【发布时间】:2020-03-06 09:41:05
【问题描述】:
我编写了一个片段着色器,它为每个 voronoi 单元采样不同的纹理。现在我循环遍历每个像素的所有位置,这是非常低效的。
关于如何优化它的任何提示?我需要使用 1000 个“单元”运行 2 x 1080p - 这是我的 box2d 的 cpu 最大值。
也许在顶点着色器中绘制单元格然后对其进行采样?我对此很陌生,任何提示表示赞赏!
奇怪的尺寸(*20 等),因为我的 box2d 世界很大,用于测试。
干杯,A
Texture2DArray texArray <string uiname="Texture Array";>;
Texture2D tex <string uiname="Texture";>;
int id;
int scale= 20;
float4x4 tWVP: WORLDVIEWPROJECTION;
SamplerState linearSampler : IMMUTABLE
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};
cbuffer cbPerDraw : register( b0 )
{
float4x4 tVP : LAYERVIEWPROJECTION;
};
cbuffer cbPerObj : register( b1 )
{
float4x4 tW : WORLD;
};
StructuredBuffer<float2> posBuffer;
StructuredBuffer<int> idBuffer;
struct vsInput
{
float4 PosO : POSITION;
float4 TexCd : TEXCOORD0;
};
struct psInput
{
float4 PosWVP: SV_Position;
float4 TexCd: TEXCOORD0;
};
psInput VS(vsInput In)
{
return In;
}
float4 PS(psInput In): SV_Target
{
uint count, stride;
posBuffer.GetDimensions(count, stride);
float minDist = 100;
float2 uvRaw = In.TexCd.xy;
float2 uv = ( uvRaw -.5) * 20;
float4 col = 1;
uint id;
for (uint i=0; i<count; i++)
{
id = idBuffer[i];
float2 p = posBuffer[i]*1;
float d = length(uv-p) * .2;
if (d < minDist)
{
minDist = d;
col = texArray.SampleLevel(linearSampler, float3(uvRaw - p *0.05, i), 0);
}
}
return col;
}
technique10 Constant
{
pass P0
{
SetVertexShader( CompileShader( vs_5_0, VS() ) );
SetPixelShader( CompileShader( ps_5_0, PS() ) );
}
}
[1]: https://ibb.co/82WMNCt
【问题讨论】:
标签: arrays shader textures fragment-shader voronoi