Sign in to follow this  
ekba89

DX11 nvidia rain demo

Recommended Posts

I'm trying to render rain with dx11 and hlsl and there aren't a lot of examples to create realistic rain. The best I've found so far is nvidia's rain demo from their directx 10 sdk. I've managed to convert it to directx 11 and it is working but I'm having hard time to understand the main part of their code which i added below.
My first problem is how they indexed their streak images. There are 370 rain streak images and if I'm not mistaken the maximum index value can be 368.
And my second problem is "if"s in the code. I didn't understand their purpose. And why they are using is_EpLp_angle_ccw to invert only one of the horizontal texture coordinates.
Also if there are any other rain tutorial that you can suggest I would like to check it out.

[code]void rainResponse(PSSceneIn input, float3 lightVector, float lightIntensity, float3 lightColor, float3 eyeVector, bool fallOffFactor, inout float4 rainResponseVal)
{

float opacity = 0.0;
float fallOff;
if(fallOffFactor)
{
float distToLight = length(lightVector);
fallOff = 1.0/( distToLight * distToLight);
fallOff = saturate(fallOff);
}
else
{ fallOff = 1;
}
if(fallOff > 0.01 && lightIntensity > 0.01 )
{
float3 dropDir = g_TotalVel;
#define MAX_VIDX 4
#define MAX_HIDX 8
// Inputs: lightVector, eyeVector, dropDir
float3 L = normalize(lightVector);
float3 E = normalize(eyeVector);
float3 N = normalize(dropDir);

bool is_EpLp_angle_ccw = true;
float hangle = 0;
float vangle = abs( (acos(dot(L,N)) * 180/PI) - 90 ); // 0 to 90

{
float3 Lp = normalize( L - dot(L,N)*N );
float3 Ep = normalize( E - dot(E,N)*N );
hangle = acos( dot(Ep,Lp) ) * 180/PI; // 0 to 180
hangle = (hangle-10)/20.0; // -0.5 to 8.5
is_EpLp_angle_ccw = dot( N, cross(Ep,Lp)) > 0;
}

if(vangle>=88.0)
{
hangle = 0;
is_EpLp_angle_ccw = true;
}

vangle = (vangle-10.0)/20.0; // -0.5 to 4.5

// Outputs:
// verticalLightIndex[1|2] - two indices in the vertical direction
// t - fraction at which the vangle is between these two indices (for lerp)
int verticalLightIndex1 = floor(vangle); // 0 to 5
int verticalLightIndex2 = min(MAX_VIDX, (verticalLightIndex1 + 1) );
verticalLightIndex1 = max(0, verticalLightIndex1);
float t = frac(vangle);
// textureCoordsH[1|2] used in case we need to flip the texture horizontally
float textureCoordsH1 = input.tex.x;
float textureCoordsH2 = input.tex.x;

// horizontalLightIndex[1|2] - two indices in the horizontal direction
// s - fraction at which the hangle is between these two indices (for lerp)
int horizontalLightIndex1 = 0;
int horizontalLightIndex2 = 0;
float s = 0;

s = frac(hangle);
horizontalLightIndex1 = floor(hangle); // 0 to 8
horizontalLightIndex2 = horizontalLightIndex1+1;
if( horizontalLightIndex1 < 0 )
{
horizontalLightIndex1 = 0;
horizontalLightIndex2 = 0;
}

if( is_EpLp_angle_ccw )
{
if( horizontalLightIndex2 > MAX_HIDX )
{
horizontalLightIndex2 = MAX_HIDX;
textureCoordsH2 = 1.0 - textureCoordsH2;
}
}
else
{
textureCoordsH1 = 1.0 - textureCoordsH1;
if( horizontalLightIndex2 > MAX_HIDX )
{
horizontalLightIndex2 = MAX_HIDX;
} else
{
textureCoordsH2 = 1.0 - textureCoordsH2;
}
}

if( verticalLightIndex1 >= MAX_VIDX )
{
textureCoordsH2 = input.tex.x;
horizontalLightIndex1 = 0;
horizontalLightIndex2 = 0;
s = 0;
}

// Generate the final texture coordinates for each sample
uint type = input.type;
uint2 texIndicesV1 = uint2(verticalLightIndex1*90 + horizontalLightIndex1*10 + type,
verticalLightIndex1*90 + horizontalLightIndex2*10 + type);
float3 tex1 = float3(textureCoordsH1, input.tex.y, texIndicesV1.x);
float3 tex2 = float3(textureCoordsH2, input.tex.y, texIndicesV1.y);
if( (verticalLightIndex1<4) && (verticalLightIndex2>=4) )
{
s = 0;
horizontalLightIndex1 = 0;
horizontalLightIndex2 = 0;
textureCoordsH1 = input.tex.x;
textureCoordsH2 = input.tex.x;
}

uint2 texIndicesV2 = uint2(verticalLightIndex2*90 + horizontalLightIndex1*10 + type,
verticalLightIndex2*90 + horizontalLightIndex2*10 + type);
float3 tex3 = float3(textureCoordsH1, input.tex.y, texIndicesV2.x);
float3 tex4 = float3(textureCoordsH2, input.tex.y, texIndicesV2.y);
// Sample opacity from the textures
float col1 = rainTextureArray.Sample( samAniso, tex1) * g_rainfactors[texIndicesV1.x];
float col2 = rainTextureArray.Sample( samAniso, tex2) * g_rainfactors[texIndicesV1.y];
float col3 = rainTextureArray.Sample( samAniso, tex3) * g_rainfactors[texIndicesV2.x];
float col4 = rainTextureArray.Sample( samAniso, tex4) * g_rainfactors[texIndicesV2.y];
// Compute interpolated opacity using the s and t factors
float hOpacity1 = lerp(col1,col2,s);
float hOpacity2 = lerp(col3,col4,s);
opacity = lerp(hOpacity1,hOpacity2,t);
opacity = pow(opacity,0.7); // inverse gamma correction (expand dynamic range)
opacity = 4*lightIntensity * opacity * fallOff;
}

rainResponseVal = float4(lightColor,opacity);
}[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628370
    • Total Posts
      2982300
  • Similar Content

    • By joeblack
      Hi,
      im reading about specular aliasing because of mip maps, as far as i understood it, you need to compute fetched normal lenght and detect now its changed from unit length. I’m currently using BC5 normal maps, so i reconstruct z in shader and therefore my normals are normalized. Can i still somehow use antialiasing or its not needed? Thanks.
    • By 51mon
      I want to change the sampling behaviour to SampleLevel(coord, ddx(coord.y).xx, ddy(coord.y).xx). I was just wondering if it's possible without explicit shader code, e.g. with some flags or so?
    • By GalacticCrew
      Hello,
      I want to improve the performance of my game (engine) and some of your helped me to make a GPU Profiler. After creating the GPU Profiler, I started to measure the time my GPU needs per frame. I refined my GPU time measurements to find my bottleneck.
      Searching the bottleneck
      Rendering a small scene in an Idle state takes around 15.38 ms per frame. 13.54 ms (88.04%) are spent while rendering the scene, 1.57 ms (10.22%) are spent during the SwapChain.Present call (no VSync!) and the rest is spent on other tasks like rendering the UI. I further investigated the scene rendering, since it takes über 88% of my GPU frame rendering time.
      When rendering my scene, most of the time (80.97%) is spent rendering my models. The rest is spent to render the background/skybox, updating animation data, updating pixel shader constant buffer, etc. It wasn't really suprising that most of the time is spent for my models, so I further refined my measurements to find the actual bottleneck.
      In my example scene, I have five animated NPCs. When rendering these NPCs, most actions are almost for free. Setting the proper shaders in the input layout (0.11%), updating vertex shader constant buffers (0.32%), setting textures (0.24%) and setting vertex and index buffers (0.28%). However, the rest of the GPU time (99.05% !!) is spent in two function calls: DrawIndexed and DrawIndexedInstance.
      I searched this forum and the web for other articles and threads about these functions, but I haven't found a lot of useful information. I use SharpDX and .NET Framework 4.5 to develop my game (engine). The developer of SharpDX said, that "The method DrawIndexed in SharpDX is a direct call to DirectX" (Source). DirectX 11 is widely used and SharpDX is "only" a wrapper for DirectX functions, I assume the problem is in my code.
      How I render my scene
      When rendering my scene, I render one model after another. Each model has one or more parts and one or more positions. For example, a human model has parts like head, hands, legs, torso, etc. and may be placed in different locations (on the couch, on a street, ...). For static elements like furniture, houses, etc. I use instancing, because the positions never change at run-time. Dynamic models like humans and monster don't use instancing, because positions change over time.
      When rendering a model, I use this work-flow:
      Set vertex and pixel shaders, if they need to be updated (e.g. PBR shaders, simple shader, depth info shaders, ...) Set animation data as constant buffer in the vertex shader, if the model is animated Set generic vertex shader constant buffer (world matrix, etc.) Render all parts of the model. For each part: Set diffuse, normal, specular and emissive texture shader views Set vertex buffer Set index buffer Call DrawIndexedInstanced for instanced models and DrawIndexed models What's the problem
      After my GPU profiling, I know that over 99% of the rendering time for a single model is spent in the DrawIndexedInstanced and DrawIndexed function calls. But why do they take so long? Do I have to try to optimize my vertex or pixel shaders? I do not use other types of shaders at the moment. "Le Comte du Merde-fou" suggested in this post to merge regions of vertices to larger vertex buffers to reduce the number of Draw calls. While this makes sense to me, it does not explain why rendering my five (!) animated models takes that much GPU time. To make sure I don't analyse something I wrong, I made sure to not use the D3D11_CREATE_DEVICE_DEBUG flag and to run as Release version in Visual Studio as suggested by Hodgman in this forum thread.
      My engine does its job. Multi-texturing, animation, soft shadowing, instancing, etc. are all implemented, but I need to reduce the GPU load for performance reasons. Each frame takes less than 3ms CPU time by the way. So the problem is on the GPU side, I believe.
    • By noodleBowl
      I was wondering if someone could explain this to me
      I'm working on using the windows WIC apis to load in textures for DirectX 11. I see that sometimes the WIC Pixel Formats do not directly match a DXGI Format that is used in DirectX. I see that in cases like this the original WIC Pixel Format is converted into a WIC Pixel Format that does directly match a DXGI Format. And doing this conversion is easy, but I do not understand the reason behind 2 of the WIC Pixel Formats that are converted based on Microsoft's guide
      I was wondering if someone could tell me why Microsoft's guide on this topic says that GUID_WICPixelFormat40bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA and why GUID_WICPixelFormat80bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA
      In one case I would think that: 
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat32bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA, because the black channel (k) values would get readded / "swallowed" into into the CMY channels
      In the second case I would think that:
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat128bppRGBA, because the black channel (k) bits would get redistributed amongst the remaining 4 channels (CYMA) and those "new bits" added to those channels would fit in the GUID_WICPixelFormat64bppRGBA and GUID_WICPixelFormat128bppRGBA formats. But also seeing as there is no GUID_WICPixelFormat128bppRGBA format this case is kind of null and void
      I basically do not understand why Microsoft says GUID_WICPixelFormat40bppCMYKAlpha and GUID_WICPixelFormat80bppCMYKAlpha should convert to GUID_WICPixelFormat64bppRGBA in the end
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
  • Popular Now