首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写非常简单的HLSL像素着色器时出错

编写非常简单的HLSL像素着色器时出错
EN

Stack Overflow用户
提问于 2012-10-04 22:08:05
回答 1查看 3.1K关注 0票数 0

我正在编写我的SlimDX应用程序来测试人类视觉系统的颜色对比敏感度阈值。该主题有一个大字母,与背景形成了较低的对比,并要求识别该信件。然而,我需要的能力,以显示更多的颜色,然后是一个简单的8位每通道的颜色深度。(即rgb值0-255)。我计划这样做的方式是通过一个简单的“抖动”算法实现为HLSL像素着色器。基本上,如果请求slimDX将文本呈现到一个颜色为Color4( 0.55f、0.55f、0.55f )的表面,该颜色对应于rgb值( 140.25、140.25、140.25 ),我希望每个像素的每个颜色通道都有25%的机会被设置为141,75%的几率被设置为140。这应该会(在许多像素的限制下)导致一个字母显示为灰色的1/4,介于140到141之间。

然而,当我试图用fxc编译时,我的原型着色器代码出现了错误。当我编译时,我得到了一个非法的字符代码,我不知道为什么。另外,如果您是HLSL专家,请查看我的代码并发表任何令您印象深刻的评论。注意,我从这个问题的答案中得到了HLSL随机函数。Can I generate a random number inside a pixel shader?

下面是我的着色代码。如果有许多错误,请原谅,因为这是我的第一个HLSL代码:

代码语言:javascript
复制
float4 DitherByChance(float2 coords : TEXCOORD0) : COLOR0
{
    float4 newColor;    // The pixel color to return
    float4 oldColor = tex2D(s0, coords);

    // Note I know that as the code stands now rCutOff = gCutOff = bCutOff I will sort a fix for this out later
    float rCutOff = random(coords);     // A random float that determines if the red channel will be rounded up or down
    float gCutOff = random(coords);     // A random float that determines if the green channel will be rounded up or down
    float bCutOff = random(coords);     // A random float that determines if the blue channel will be rounded up or down

    float rPercentChance = frac(oldColor.r * 255);  //Chance to round red channel up
    float gPercentChance = frac(oldColor.g * 255);  //Chance to round green channel up
    float bPercentChance = frac(oldColor.b * 255);  //Chance to round blue channel up

    //In the code below (1/255) is the floating point represntaion of an incress of one on the 0-255 RGB scale

    if (rCutOff <= rPercentChance) newColor.r = oldColor.r + ((1 - rPercentChance) / 255);  //Bump up one r value
    else newColor.r = oldColor.r - rPercentChance * (1 / 255);                  //Bump down to ensure r is not rounded up

    if (gCutOff <= gPercentChance) newColor.g = oldColor.g + ((1 - gPercentChance) / 255);  //Bump up one g value
    else newColor.b = oldColor.b - bPercentChance * (1 / 255);                  //Bump down to ensure g is not rounded up

    if (bCutOff <= bPercentChance) newColor.b = oldColor.b + ((1 - bPercentChance) / 255);  //Bump up one b value
    else newColor.b = oldColor.b - bPercentChance * (1 / 255);                  //Bump down to ensure b is not rounded up

    return newColor;
}

// Input: It uses texture coords as the random number seed.
// Output: Random number: [0,1), that is between 0.0 and 0.999999... inclusive.
// Author: Michael Pohoreski
// Copyright: Copyleft 2012 :-)
float random( vec2 p )
{
  // We need irrationals for pseudo randomness.
  // Most (all?) known transcendental numbers will (generally) work.
  const vec2 r = vec2(
    23.1406926327792690,  // e^pi (Gelfond's constant)
     2.6651441426902251); // 2^sqrt(2) (Gelfond–Schneider constant)
  return fract( cos( mod( 123456789., 1e-7 + 256. * dot(p,r) ) ) );  
}

编辑:在下面的答案的帮助下,我已经接近了一个解决方案,但仍然存在一些问题。我接受了catflier的评论,我借用的随机函数是用GLSL编写的,而不是HLSL。我不得不将vec2改为float2,并将随机函数放在顶部。此外,我还必须编写一个mod函数,因为GLSL mod() != HLSL fmod。而GLSL ()被更改为HLSL frac()。我还遇到了一个问题,我的文本文件是用UTF-8编码的,我猜这会导致fxc出现一些问题。在重新编码到ANSI之后,我得到了一些更好的错误消息。不过,我仍有一些问题。

我尝试使用fxc /Zi /E DitherByChance DitherByChance.fxfxc /Zi /E DitherByChance /LD DitherByChance.fx进行编译,因为我的目标是DX9,但两次都出现了以下错误:

undeclared indentifier 's0' error从这一行float4 oldColor = tex2D(s0, coords);

我不知道s0的论点到底是什么,我刚才在另一个着色器例子中看到了它。我看到了另一个示例,其中有人声明并使用了全局sampler2D变量,而不是s0,因此我尝试了以下操作:

代码语言:javascript
复制
sampler2D Tex0;
...
.... 
.....
float4 oldColor = tex2D(Tex0, coords); 

然而,现在当我运行fxc /Zi /E DitherByChance /LD DitherByChance.fx时,我会得到以下错误:

vs_2_0 target does not support texture lookup

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-04 22:59:47

首先,您需要在随机函数中用vec2替换float2,因为vec2是glsl语法。

然后你的随机函数应该放在你的PixelShader (c-风格)之上。

您的着色器现在应该编译,如果没有,请张贴错误信息。

从最新比特编辑:

代码语言:javascript
复制
sampler2D Tex0;

是一个采样器单元,您需要将它绑定到您的纹理,因为采样器将告诉它如何过滤它(线性/点),并寻址它(包装/夹子.)

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509644(v=vs.85).aspx

由于您编译像素着色器,您需要使用fxc /T ps_3_0,因为您编译为像素着色器,而不是顶点着色器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12736750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档