HLSL Schleifenvariablenproblem mit PS3_0, Help :)

MPi

Cadet 2nd Year
Registriert
Mai 2011
Beiträge
23
Hallo,

nachdem ich nun den ganzen Tag schon versuche eine Schleife dynamisch hinzubekommen in einem Pixelshader 3_0 und es einfach nicht so will, versuche ich bei Euch mal mein Glück.

Zum Problem:
Oben im "Globals" Bereich liegt die Variable:
int kernelSize;

Diese Variable wird durch
pEffect->SetValue("kernelSize", &kernelSize, sizeof(kernelSize));
auf den Wert 2 gesetzt.

Starte ich nun das Compilieren des .fx Files, semmelt die Kiste einfach ab.

Ändere ich die Zeile um in:
#define kernelSize 2
läuft alles einwandfrei und die durchschnittlich Helligkeit wird mit dem gauss'schen Operator um den jeweilgen Pixel herum von 3 Eingangsbildern erstellt.

Was mache ich falsch? Was habe ich übersehen? :rolleyes:

Gruss und Danke


Und hier das .fx File:
(bitte noch nicht auf Performance schauen, es geht nur um Funktion bis dato :)

//-----------------------------------------------------------------------------
// Globals

float4x4 worldMatrix;
float4x4 worldInverseTransposeMatrix;
float4x4 worldViewProjectionMatrix;

float3 cameraPos;

float pixelToTexelx;
float pixelToTexely;

int kernelSize;

//-----------------------------------------------------------------------------
// Textures.

sampler2D input1;
sampler2D input2;
sampler2D inputh1;
sampler2D inputh2;
sampler2D inputh3;

//-----------------------------------------------------------------------------
// Vertex Shaders.
//-----------------------------------------------------------------------------

struct VS_INPUT
{
float3 position : POSITION;
float2 texCoord : TEXCOORD0;
float3 normal : NORMAL;
};

struct VS_OUTPUT
{
float4 position : POSITION;
float3 worldPos : TEXCOORD0;
float2 texCoord : TEXCOORD1;
float3 viewDir : TEXCOORD2;
float3 normal : TEXCOORD3;
};

VS_OUTPUT VS_HDR(VS_INPUT IN)
{
VS_OUTPUT OUT;

OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix);
OUT.worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz;
OUT.texCoord = IN.texCoord;
OUT.viewDir = cameraPos - OUT.worldPos;
OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix);

return OUT;
}

//-----------------------------------------------------------------------------
// Pixel Shader

float3 LuminanceConv = { 0.2125f, 0.7154f, 0.0721f };

float4 PS_HDR(VS_OUTPUT IN) : COLOR
{
float4 color = float4(0.0f, 0.0f, 0.0f, 1.0f);
float4 colorh1 = float4(0.0f, 0.0f, 0.0f, 1.0f);
float4 colorh2 = float4(0.0f, 0.0f, 0.0f, 1.0f);
float4 colorh3 = float4(0.0f, 0.0f, 0.0f, 1.0f);

float luma=0;
float lumd=0;
float lum1=0;
float lum2=0;
float lum3=0;

float d=0;
float dx=0;
float dy=0;

int l1=0;
int l2=0;

float kernelsizesquared=0;

float2 samp = float2(0.0f, 0.0f);

colorh1.rgb = tex2D(inputh1, IN.texCoord);
colorh2.rgb = tex2D(inputh2, IN.texCoord);
colorh3.rgb = tex2D(inputh3, IN.texCoord);

l1=-kernelSize;
l2= kernelSize;
kernelsizesquared=(float)kernelSize*(float)kernelSize;
for (int j=l1; j<=l2; j++)
{
samp.y = IN.texCoord.y + j * pixelToTexely;
samp.y = clamp(samp.y,0.0005,0.9995);
for (int i=l1; i<=l2; i++)
{
dx=i;
dy=j;
d = 1/(3.1515926*kernelsizesquared) * exp ( -(dx*dx+dy*dy) / (kernelsizesquared) );

samp.x = IN.texCoord.x + i * pixelToTexelx;
samp.x = clamp(samp.x,0.0005,0.9995);

lum1+= (dot (tex2D(inputh1, samp.xy),LuminanceConv))*d;
lum2+= (dot (tex2D(inputh2, samp.xy),LuminanceConv))*d;
lum3+= (dot (tex2D(inputh3, samp.xy),LuminanceConv))*d;

lumd+=d;
}
}
lum1/=lumd;
lum2/=lumd;
lum3/=lumd;

luma = (lum1 + lum2 + lum3);

color.r = ( lum1 / luma );
color.g = ( lum2 / luma );
color.b = ( lum3 / luma );

return color;
}

technique PerPixelPointLighting
{
pass
{
VertexShader = compile vs_3_0 VS_HDR();
PixelShader = compile ps_3_0 PS_HDR();
}
}

PS: Entschuldigt die grausige Formatierung, kann man das hier irgendwie besser machen?
 
Zuletzt bearbeitet:
Code:
pEffect->SetValue("kernelSize", &kernelSize, sizeof(kernelSize));
ist C++ Code oder?? Wenn du das durch
Code:
#define kernelSize 2
im C++ Code ersetzt, dann hat das _keine_ Wirkung. #define ist eine Präprozessor-Anweisung, die einfach nur Konstanten oder Macros definiert. Wenn du die Konstante kernelSize einfach nur definierst ohne sie zu verwenden hat es 0 Effekt. Es ist als wenn die Zeile nicht da wäre.


P.S. PI ist 3.14159265 und nicht 3.1515926
 
Ups,

die falsche Ziffer bei Pi ist schon peinlich ;)

pEffect->SetValue("kernelSize", &kernelSize, sizeof(kernelSize)); ist C-Code und soll eine Variable im Shader setzen

Im Shader:
int kernelSize; anstatt #define kernelSize 2 führt zum direkten Absturz, das ist es was mir nicht in den Kopf geht.

Edit:
Wie es aussieht muss eine Loop bei 0 starten und sollte nicht mit <= verglichen werden wenn man eine Variable nutzt.
Ändere ich die Schleifen im Shader auf

for (int j=0; j<l2; j++) {
for (int i=0; i<l2; i++) {

läuft es plötzlich.
 
Zuletzt bearbeitet:
MPi schrieb:
PS: Entschuldigt die grausige Formatierung, kann man das hier irgendwie besser machen?
"Code/PHP-Code Einfügen" nutzen, oder so schreiben
HTML:
[code]...[/code] oder [php]...[/php]

Sieht dann so aus:
Code:
for (int j=0; j<l2; j++) {
  for (int i=0; i<l2; i++) {

PHP:
for (int j=0; j<l2; j++) {
  for (int i=0; i<l2; i++) {
 
Zurück
Oben