• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Endemoniada

      Hi guys, when I do picking followed by ray-plane intersection the results are all wrong. I am pretty sure my ray-plane intersection is correct so I'll just show the picking part. Please take a look:
       
      // get projection_matrix DirectX::XMFLOAT4X4 mat; DirectX::XMStoreFloat4x4(&mat, projection_matrix); float2 v; v.x = (((2.0f * (float)mouse_x) / (float)screen_width) - 1.0f) / mat._11; v.y = -(((2.0f * (float)mouse_y) / (float)screen_height) - 1.0f) / mat._22; // get inverse of view_matrix DirectX::XMMATRIX inv_view = DirectX::XMMatrixInverse(nullptr, view_matrix); DirectX::XMStoreFloat4x4(&mat, inv_view); // create ray origin (camera position) float3 ray_origin; ray_origin.x = mat._41; ray_origin.y = mat._42; ray_origin.z = mat._43; // create ray direction float3 ray_dir; ray_dir.x = v.x * mat._11 + v.y * mat._21 + mat._31; ray_dir.y = v.x * mat._12 + v.y * mat._22 + mat._32; ray_dir.z = v.x * mat._13 + v.y * mat._23 + mat._33;  
      That should give me a ray origin and direction in world space but when I do the ray-plane intersection the results are all wrong.
      If I click on the bottom half of the screen ray_dir.z becomes negative (more so as I click lower). I don't understand how that can be, shouldn't it always be pointing down the z-axis ?
      I had this working in the past but I can't find my old code
      Please help. Thank you.
    • By turanszkij
      Hi,
      I finally managed to get the DX11 emulating Vulkan device working but everything is flipped vertically now because Vulkan has a different clipping space. What are the best practices out there to keep these implementation consistent? I tried using a vertically flipped viewport, and while it works on Nvidia 1050, the Vulkan debug layer is throwing error messages that this is not supported in the spec so it might not work on others. There is also the possibility to flip the clip scpace position Y coordinate before writing out with vertex shader, but that requires changing and recompiling every shader. I could also bake it into the camera projection matrices, though I want to avoid that because then I need to track down for the whole engine where I upload matrices... Any chance of an easy extension or something? If not, I will probably go with changing the vertex shaders.
    • By evelyn4you
      Hello,
      in my game engine i want to implement my own bone weight painting tool, so to say a virtual brush painting tool for a mesh.
      I have already implemented my own "dual quaternion skinning" animation system with "morphs" (=blend shapes)  and "bone driven"  "corrective morphs" (= morph is dependent from a bending or twisting bone)
      But now i have no idea which is the best method to implement a brush painting system.
      Just some proposals
      a.  i would build a kind of additional "vertecie structure", that can help me to find the surrounding (neighbours) vertecie indexes from a given "central vertecie" index
      b.  the structure should also give information about the distance from the neighbour vertecsies to the given "central vertecie" index
      c.  calculate the strength of the adding color to the "central vertecie" an the neighbour vertecies by a formula with linear or quadratic distance fall off
      d.  the central vertecie would be detected as that vertecie that is hit by a orthogonal projection from my cursor (=brush) in world space an the mesh
            but my problem is that there could be several  vertecies that can be hit simultaniously. e.g. i want to paint the inward side of the left leg. the right leg will also be hit.
      I think the given problem is quite typical an there are standard approaches that i dont know.
      Any help or tutorial are welcome
      P.S. I am working with SharpDX, DirectX11
        
    • By Luca Davidian
      Hi, I'm implementing a simple 3D engine based on DirectX11. I'm trying to render a skybox with a cubemap on it and to do so I'm using DDS Texture Loader from DirectXTex library. I use texassemble to generate the cubemap (texture array of 6 textures) into a DDS file that I load at runtime. I generated a cube "dome" and sample the texture using the position vector of the vertex as the sample coordinates (so far so good), but I always get the same face of the cubemap mapped on the sky. As I look around I always get the same face (and it wobbles a bit if I move the camera). My code:   
      //Texture.cpp:         Texture::Texture(const wchar_t *textureFilePath, const std::string &textureType) : mType(textureType)         {             //CreateDDSTextureFromFile(Game::GetInstance()->GetDevice(), Game::GetInstance()->GetDeviceContext(), textureFilePath, &mResource, &mShaderResourceView);             CreateDDSTextureFromFileEx(Game::GetInstance()->GetDevice(), Game::GetInstance()->GetDeviceContext(), textureFilePath, 0, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, D3D11_RESOURCE_MISC_TEXTURECUBE, false, &mResource, &mShaderResourceView);         }     // SkyBox.cpp:          void SkyBox::Draw()     {         // set cube map         ID3D11ShaderResourceView *resource = mTexture.GetResource();         Game::GetInstance()->GetDeviceContext()->PSSetShaderResources(0, 1, &resource);              // set primitive topology         Game::GetInstance()->GetDeviceContext()->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);              mMesh.Bind();         mMesh.Draw();     }     // Vertex Shader:     cbuffer Transform : register(b0)     {         float4x4 viewProjectionMatrix;     };          float4 main(inout float3 pos : POSITION) : SV_POSITION     {         return mul(float4(pos, 1.0f), viewProjectionMatrix);     }     // Pixel Shader:     SamplerState cubeSampler;     TextureCube cubeMap;          float4 main(in float3 pos : POSITION) : SV_TARGET     {         float4 color = cubeMap.Sample(cubeSampler, pos.xyz);         return color;     } I tried both functions grom DDS loader but I keep getting the same result. All results I found on the web are about the old SDK toolkits, but I'm using the new DirectXTex lib.
    • By B. /
      Hi Guys,
      i want to draw shadows of a direction light but the shadows always disappear, if i translate my mesh (cube) in the world to far of the bounds of my orthographic projection matrix.
      That my code (Based of an XNA sample i recode for my project):
      // Matrix with that will rotate in points the direction of the light Matrix lightRotation = Matrix.LookAtLH(Vector3.Zero, lightDir, Vector3.Up); BoundingFrustum cameraFrustum = new BoundingFrustum(Matrix.Identity); // Get the corners of the frustum Vector3[] frustumCorners = cameraFrustum.GetCorners(); // Transform the positions of the corners into the direction of the light for (int i = 0; i < frustumCorners.Length; i++) frustumCorners[i] = Vector4F.ToVector3(Vector3.Transform(frustumCorners[i], lightRotation)); // Find the smallest box around the points BoundingBox lightBox = BoundingBox.FromPoints(frustumCorners); Vector3 boxSize = lightBox.Maximum - lightBox.Minimum; Vector3 halfBoxSize = boxSize * 0.5f; // The position of the light should be in the center of the back pannel of the box. Vector3 lightPosition = lightBox.Minimum + halfBoxSize; lightPosition.Z = lightBox.Minimum.Z; // We need the position back in world coordinates so we transform // the light position by the inverse of the lights rotation lightPosition = Vector4F.ToVector3(Vector3.Transform(lightPosition, Matrix.Invert(lightRotation))); // Create the view matrix for the light this.view = Matrix.LookAtLH(lightPosition, lightPosition + lightDir, Vector3.Up); // Create the projection matrix for the light // The projection is orthographic since we are using a directional light int amount = 25; this.projection = Matrix.OrthoOffCenterLH(boxSize.X - amount, boxSize.X + amount, boxSize.Y + amount, boxSize.Y - amount, -boxSize.Z - amount, boxSize.Z + amount); I believe the bug is by cameraFrustum to set a Matrix Idetity. I also tried with a Translation Matrix of my Camera Position and also the View Matrix of my Camera, but without success
      Can anyone tell me, how to draw shadows of my direction light always where my camera is current in my scene?
      Greets
      Benjamin
  • Advertisement
  • Advertisement
Sign in to follow this  

DX11 Use an array in HLSL

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites


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

 

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

Share this post


Link to post
Share on other sites

 


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

 

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

Edited by Nathan Drake

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement