Skip to main content
GameDev.net gamedev.net
🔒 Locked

Use an array in HLSL

Started by Nathan Drake Nov 17, 2015 at 3:38 PM 11 replies 36.7k views
Original Post
Nathan Drake
Nathan Drake

Hi guys,

I want to Implement Light propagation volumes with DX11 and for the first step i need to implement RSM and reflection from lighted pixels to the environment. for pixel light samples, i have to generate an array of random pixels and read that array in HLSL, how can I read an array in HLSL ?

Thanks,

Syntac_
Syntac_

Do you mean a way you can write VPLs generated from the RSM, if so you can use structured buffers to do that.

Nathan Drake
Nathan Drake

right now this is what i want to do:

i've made an array of random numbers :


struct pixelSamples
{
    float pixelSampleArray[32];
};

pixelSamples pxlSamples;

float linearRand(float Min, float Max)
{
    return float(std::rand()) / float(RAND_MAX) * (Max - Min) + Min;
}

void GenerateVPL()
{
	float r_max = 0.25f;

	const float pi = 3.14159265f;
	for (unsigned int i = 0; i<32; i += 2)
	{
		float rand1 = linearRand(0.f, 1.0f);
		float rand2 = linearRand(0.f, 1.0f);

		pxlSamples.pixelSampleArray[i] = r_max * rand1 * sin(2.0f*pi*rand2);
		pxlSamples.pixelSampleArray[i + 1] = r_max * rand1 * cos(2.0f*pi*rand2);
	}
}

and i want to read this array in HLSL. based on what i've found here, I've made a buffer in my HLSL file that contains:


cbuffer pixelSamples
{
	float4 pixelSamplesArray[2];
};

and for reading this array in my pixel shader, i'll use this 'for loop' inside my pixel shader:


	for (int i = 0; i < 32; i++)
		float2 data = float2(pixelSamplesArray[(i * 2) / 4][(i * 2) % 4], pixelSamplesArray[(i * 2 + 1) / 4][(i * 2 + 1) % 4]);

but unfortunately it doesn't work. am i made a mistake ?

Krohm
Krohm




but unfortunately it doesn't work. am i made a mistake ?

Asking questions 101.

Don't write "it doesn't work". Instead write: I get this error: , I was expecting to happen but I get instead. This way you can get better answers. I'm pretty sure you were told about this.

I am not aware of how linearRand works. Google results in a surprisingly small amount of links. That's not really important however as nothing in the writing for seems to be wrong.

I am not sure where those values are supposed to go; I haven't used HLSL in a while but I hardly believe the value of the global pxlSamples goes anywhere but oblivion at the end of the shader... assuming this is the same shader then cbuffer pixelSamples does not make any sense.

I am not supposed to read your mind, nor guess.

Previously "Krohm"
Nathan Drake
Nathan Drake

but unfortunately it doesn't work. am i made a mistake ?

Asking questions 101.

Don't write "it doesn't work". Instead write: I get this error: , I was expecting to happen but I get instead. This way you can get better answers. I'm pretty sure you were told about this.

I am not aware of how linearRand works. Google results in a surprisingly small amount of links. That's not really important however as nothing in the writing for seems to be wrong.

I am not sure where those values are supposed to go; I haven't used HLSL in a while but I hardly believe the value of the global pxlSamples goes anywhere but oblivion at the end of the shader... assuming this is the same shader then cbuffer pixelSamples does not make any sense.

I am not supposed to read your mind, nor guess.

Dear Krohm

Thanks for your reply, you're right

but the reason of writing that sentence was because it gives me access violation error. you can see the error in here:

[SPOILER]

Fy96EdZ.png

[/SPOILER]

i don't think saying "i receive access violation" or "it doesn't work" makes many difference since it doesn't have any specific error for that.

for that linearRand, i've written that function that generates a random number between min and max input (added into my previous post)

Update 1: if i show indexing of that "for loop" in a simple way, here it comes:


for (int i = 0; i < 1; i++)
    for (int j = 0; j < 1; j++)
		float2 data = float2(pixelSamplesArray[i][j], pixelSamplesArray[i][j]);

I can't increase that ' i ' and ' j ' variable more than one, but my array in CPU side has 32 units of float and i don't know what is the reason

Krohm
Krohm

It makes the whole thing very, very different!

Your shader is irrelevant when it comes to that error, dude, look at you debug window! PS_Buffer is 0x0, what do you think it'll happen when you try PS_Buffer->GetBufferPointer()?

Previously "Krohm"
Hodgman
Hodgman




i don't think saying "i receive access violation" or "it doesn't work" makes many difference since it doesn't have any specific error for that.
It makes a massive difference. I read your post and disregarded it the first time, because I couldn't be bothered asking you to tell us the actual problem.

Now I can see that your problem is that there was an error while compiling your shader code, so D3DX11CompileFromFile has failed. You're not checking for errors, or getting the (text) error results from the compiler and displaying them. Start by doing that, so that you can also diagnose shader compilation errors in the future. It will give you a reason that it failed to compiler the shader, and name the line of shader code that's causing the issue.

You're getting an access violation because D3DX11CompileFromFile failed, but then you tried to use it's results anyway (which are a NULL pointer, due to failure). It's important to understand that here, the access violation is a symptom arising from a series of previous errors -- it's not the actual error.

Nathan Drake
Nathan Drake

Lack of experience in this kind of errors makes such mistakes dry.png , Thanks guys for your replies and letting me know to provide more details about my issue.

now, I've written a function that can write the error message in a note. (that would be great if i could write these things inside the Visual Studio, if anybody knows, please tell me)

if we don't care about warnings, this is what I've got:


C:\Users\Nate\Documents\Visual Studio 2013\Projects\DX11Revision_Project2_1\DX11Revision_Project2\Effects.fx(162,10): error X4014: cannot have gradient operations inside loops with divergent flow control

and this is the part of code that i have an error. as you see in line 162, i try to add random neighbor pixels to my projectTexCoord and calculate Virtual point lights for my Global illumination:


// Calculate the projected texture coordinates.
    projectTexCoord.x = input.lightPos.x / input.lightPos.w / 2.0f + 0.5f;
    projectTexCoord.y = -input.lightPos.y / input.lightPos.w / 2.0f + 0.5f;	

for (int i = 0; i < 16; i++)
		for (int j = 0; j < 16; j++)
	{
		float2 data = float2(light.pixelSamplesArray[i / 4][j % 4], light.pixelSamplesArray[i+1 / 4][j+1 % 4]);

		float2 newTexCoord = projectTexCoord + data;

		float3 flux = fluxMapTexture.Sample(ObjSamplerState, newTexCoord, 0).xyz;
		float3 rsmNormal = normalMapTexture.Sample(ObjSamplerState, newTexCoord, 0).xyz;
		float3 rsmPosition = positionMapTexture.Sample(ObjSamplerState, newTexCoord, 0).xyz;
		float3 omega = normalize(input.Pos.xyz - rsmPosition).xyz;
		float distance = length(input.Pos.xyz - rsmPosition);

		float ndotl1 = max(0, dot(rsmNormal, omega));
		float ndotl2 = max(0, dot(input.normal, -omega));

		totalIrradiance += flux *((ndotl1 * ndotl2) / (distance * distance));
	}
	
	finalColor += totalIrradiance;
	
	return float4 (finalColor, 1.0f);
}

as i've searched over internet, someone said to change your SampleLevel to this and put the LOD to zero which i still get the same error.

someone said my problem solved by changing D3D11_FILTER_MIN_MAG_MIP_LINEAR to D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR of the sample state, but unfortunately it's the same.

this is my sampler state:


D3D11_SAMPLER_DESC sampDesc;
	ZeroMemory(&sampDesc, sizeof(sampDesc));
	sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
	sampDesc.MinLOD = 0;
	sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

	//Create the Sample State
	hr = d3d11Device->CreateSamplerState(&sampDesc, &CubesTexSamplerState);

moreover, the general solution was don't use texture samples inside the for loop iteration. but i'm not have any idea of how can I generate virtual point lights without for loop.

anybody has an idea ?

Thanks so much guys

Regards

Nathan Drake
Nathan Drake

damn, in the msdn page that i've mentioned above, it says the usage of SampleLevel instead of Sample, but i didn't see sleep.png

thanks

Nathan Drake
Nathan Drake

hopefully i can check variables of my array that i have in my shader using graphics diagnostics tool which is integrated with Visual Studio.

i thought maybe my random number generator in CPU side has an issue. so i've made an array in my Vertex shader and i'll pass it to the Pixel shader for lighting calculations.


float4 newSamples[2];

float temp_Def_v = 1.0 / 100;
newSamples[0][0] = float2(temp_Def_v, temp_Def_v);
newSamples[0][1] = float2(0, temp_Def_v);
newSamples[0][2] = float2(temp_Def_v, 0);
newSamples[0][3] = float2(-temp_Def_v, temp_Def_v);
newSamples[1][0] = float2(-temp_Def_v, -temp_Def_v);
newSamples[1][1] = float2(0, -temp_Def_v);
newSamples[1][2] = float2(-temp_Def_v, 0);
newSamples[1][3] = float2(temp_Def_v, -temp_Def_v);
for (int i = 0; i < 8; i++)
	output.myNewSamples[i / 4][i % 4] = newSamples[i / 4][i % 4];

and this is my array that shows how those values stored:

[SPOILER]

XcrHZUk.png

[/SPOILER]

as you see, each unit shows the first value of each float2.

but when i read this array in my pixel shader, something will happened that I'm not sure am i made a mistake or this is an issue from the debugger tool.

[SPOILER]

gUKBRr6.png

[/SPOILER]

this is second iteration of for loop ( i = 1)

but x and y are both zero. since I set it as newSamples[0][1] = float2(0, temp_Def_v);

anybody knows what is that reason ?

Thank you

Dingleberry
Dingleberry

float4 newSamples[2];

...

newSamples[0][1] = float2(0, temp_Def_v);

You're creating an array of two float4s.

Into the first float4, you're setting the index 1 component, aka 'y' to the value (0, temp_def_v).

Y is a scalar so it's probably giving you a compile warning, but also assigning 0 it because 0 is the first component in (0, temp_def_v). The compiler's just guessing about what you want.

Then you're storing the value of newSamples[0][1] into a float2. newSamples[0][1] is a scalar -- it's the index 1 component of a float4. It's zero in this case.

When you store a scalar zero into a float2, the compiler just splats it to both x and y.

Also remember arrays are funny in hlsl. Be sure to check your hlsl assembly -- it's still more like a suggestion to your driver than it is actual bytecode but still useful.

You can index a float2/3/4 like an array with [] notation, but it's not really an array. The hlsl compiler treats float4s like vector registers, even though a lot of gpus don't use vector registers like that. It's not entirely obvious what's going to happen though. In C++ if you make a vector like "float x, y, z, w", you can treat it like an array of memory. But if you make a simd register like "_m128", you can't, because it's not simply memory, it's a register. The hlsl compiler thinks you want your float4s in registers even though your gpu might not have float4 registers at all. In that case it's going to unroll loops for you in unexpected ways because the [] notation is really syntactic sugar.

In your case though you're mostly conflating the [] operator. Don't actually do this, but to better understand your program, imagine the type of newSamples was actually:

float newSamples[2][4]

You'll notice you're making a lot of type errors.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.