# DX11 Use an array in HLSL

This topic is 1132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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,

Edited by Nathan Drake

##### Share on other sites

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

##### Share on other sites

exactly, thanks a lot !

##### Share on other sites

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 ?

Edited by Nathan Drake

##### Share on other sites

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

Don't write "it doesn't work". Instead write: I get this error: <error description>, I was expecting <this> to happen but I get <this other result> 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.

##### Share on other sites

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

Don't write "it doesn't work". Instead write: I get this error: <error description>, I was expecting <this> to happen but I get <this other result> 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.

Dear Krohm

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

[SPOILER]

[/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

##### Share on other sites

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()?

##### Share on other sites

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.

##### Share on other sites

Lack of experience in this kind of errors makes such mistakes , 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));
}

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.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

Edited by Nathan Drake

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634146
• Total Posts
3015774
×