summaryrefslogtreecommitdiff
path: root/tests/hlsl/dxsdk/InstancingFX11
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2018-12-07 13:31:06 -0800
committerGitHub <noreply@github.com>2018-12-07 13:31:06 -0800
commit135eaff6b892fc91a398714ddcf7ef377cd4cccb (patch)
treee69f30a4fadfdb834ea141c1ec9efc862ccc70d3 /tests/hlsl/dxsdk/InstancingFX11
parentb0c2423f00b910f2f4d5010e6a04114112e294fd (diff)
Change how buffers are emitted (#741)
* Change how buffers are emitted This is a change with a lot of pieces, which can't always be separated out cleanly. I'm going to walk through them in what I hope is a logical order. The main goal of this change was to allow arrays of structured buffers to translate to Vulkan. Consider two declarations of structured buffers in HLSL/Slang: ```hlsl StructuredBuffer<X> single; StructuredBuffer<Y> multiple[10]; ``` The current translation logic was handling `single` by translating it into an *unnamed* GLSL `buffer` block like: ```glsl layout(std430) buffer _S1 { X single[]; }; ``` That syntax allows an expression like `single[i]` in Slang to be translated simply as `single[i]` in GLSL. But that naive translating doesn't work for `multiple`, since we need to declare a array of blocks in GLSL, which requires giving the whole thing a name: ```glsl layout(std430) buffer _S2 { Y _data[]; } multiple[10]; ``` Now a reference to `multiple[i][j]` in Slang needs to become `multiple[i]._data[j]` in GLSL. To avoid having way too many special cases around single structured buffers vs. arrays, it makes sense to allows emit things in the latter form, so that we instead lower `single` as: ```glsl layout(std430) buffer _S1 { X _data[]; } single; ``` So that now a reference to `single[i]` becomes `single._data[i]` in GLSL. Most of that can be handled in the standard library translation of the structured buffer indexing operations. The only wrinkle there is that there were some *old* special-case instructions in the IR intended to handle buffer load/store operations (these were added back when I was trying to keep the "VM" path working). These aren't really needed to have structured-buffer operations work; they can be handled as ordinary functions as far as the stdlib is concerned. I removed the old instructions. Along the way, it became clear that a few other cases follow the same pattern. Byte-addressed buffers are an obvious case. We were lowering HLSL/Slang: ```hlsl ByteAddressBuffer b; ... uint x = b.Load(0); ``` to GLSL like: ```glsl layout(std430) buffer _S1 { uint b[]; }; ... uint x = b[0]; ``` That logic would fail for arrays the same way that the structured buffer case was failing. The fix is the same: use named `buffer` blocks and then introduce an explicit `_data` field: ```glsl layout(std430) buffer _S1 { uint _data[]; } b; ... uint x = b._data[0]; ``` Just like with structured buffers, all of the VK translation for operations on byte-addressed buffers can be implemented directly in teh stdlib, so once the emit logic was changed it was just a matter of adding `._data` to a bunch of VK tranlsations. It turns out that arrays of constant buffers have more or less the same problem, and furthermore we have some problems with any code that directly uses the modern HLSL `ConstantBuffer<T>` type. Note: the emit logic around constant buffers sometimes refers to "parameter groups" because that is being used in the compiler as a catch-all term for constant buffers, texture buffers, and parameter blocks. The existing code was going out of its way to reproduce the way that constant buffer declarations are implicitly referenced in HLSL: ```hlsl cbuffer C { float f; } ... float tmp = f; // No reference to `C` here ``` This can be seen in the emit logic with the `isDerefBaseImplicit` function, which is used to take the internal IR representation for a reference to `f` (which is closer to the expression `(*C).f` or `C->f`) and leave off any reference to `C` so that we emit just `f`. That kind of logic just flat out doesn't work in some important cases. Arrays of constant buffers are a clear one: ```hlsl ConstantBuffer<X> cbArray[3]; ... X x = cbArray[0]; ``` There is no way to translate that to an ordinary `cbuffer` declaration at all. The same problem can be created without arrays, though: ```hlsl ConstantBuffer<X> singleCB; ... X x = singleCB; ``` The current strategy for translating constant buffers was translating `singleCB` into a `cbuffer` declaration that reproduced the fields of `X` as its members, which just wouldn't work: ```hlsl cbuffer singleCB { float f; // field of `X` } ... X x = singleCB; // ERROR: there is nothing named `singleCB` in this HLSL ``` The new strategy is more consistent. We still generate a `cbuffer` declaration for a single constant buffer, but we always give it a single field of the chosen element type: ```hlsl cbuffer singleCB { X singleCB; } ... X x = singleCB; // this works fine! ``` And in the array case we generate code that uses the explicit `ConstantBuffer<T>` type: ```hlsl ConstantBuffer<X> cbArray[3]; ... X x = cbArray[0]; ``` The GLSL output is more complicated because unlike with HLSL there is no implicit conversion from a uniform block to its element type (there is no notion of an element type). The array case thus needs a `_data` field similar to what we do for structured buffers: ```glsl layout(std140) uniform _S3 { X _data; } cbArray[3]; ... X x = cbArray[0]._data; ``` And then the non-array case needs to have a similar `_data` field for consistency: ```glsl layout(std140) uniform _S1 { X _data; } singleCB; ... X x = singleCB._data; ``` This is handled by inserting the necessary reference to `_data` whenever we dereference a constant buffer, either as part of a load instruction (loading from the whole CB as a pointer), or an `IRFieldAddress` instruction which forms a pointer into the CB (e.g., `&(singleCB->f)` becomes `singleCB._data.f`). The current emit logic handles `ParameterBlock<X>` differently from `ConstantBuffer<X>`, but really only to allow parameter blocks to be explicitly named in the output, while constant buffers were left implicit by default. Thus the only difference was a legacy one (from back when trying to exactly reproduce the HLSL text we got as input was considered an important goal), and the new approach to emitting constant buffers would get rid of it. I removed the separate logic for emitting `ParameterBlock<X>` and just let the handling for constant buffers deal with it. Note that any resource types inside of a `ParameterBlock<X>` would have been moved out as part of legalization, so that a parameter block is 100% equivalent to a constant buffer when it comes time to emit code. Unsurprisingly, changing the way we generate HLSL and GLSL output for all these buffer types meant that any tests that were directly comparing the output of `slangc` against `fxc`, `dxc`, or `glslang` broke. The basic approach to fixing the breakage in GLSL tests was to update the GLSL baseline to reflect the new output startegy. In some cases I used macros to name the various `_S<digits>` temporaries so that future renaming will hopefully be easier (it would be great if we auto-generated temporary names with a bit more context). There was one GLSL test (`tests/bugs/vk-structured-buffer-binding`) that was using raw GLSL expected output, and this was changed to use a GLSL baseline to generate SPIR-V for comparison. For HLSL tests we were sometimes running the same input file through `slangc` and `fxc`/`dxc`, and in these cases I macro-ized the various `cbuffer` declarations to generate different declarations depending on the compiler. I completely dropped the tests coming from the D3D SDK because they aren't providing much coverage, and updating them would change them so far from the original code that the purported benefit (using a body of existing shaders) would be lost. I also dropped the explicit matrix layout qualifiers in the `matrix-layout` test because the new output strategy breaks those for GLSL (you can't put matrix layout qualifiers on `struct` fields, and now the body of every constant buffer is inside a `struct`). This isn't as big of a loss as it seems, because our handling of those qualifiers wasn't really right to begin with. Slang users should only be setting the matrix layout mode globally (and we should probably switch to error out on the explicit qualifiers for now). The other thing that got dropped is tests involving `packoffset` modifiers. Slang already warns that it doesn't support these, and the way they were used in the test cases is actually misleading. For the binding/layout-related tests, the goal was to show that Slang reproduces the same layout as fxc, in which case explicitly enforcing a layout via `packoffset` seems like cheating (are we sure we enforced the layout fxc would have produced?). The real reason was that Slang used to emit explicit `packoffset` on *every* field of a `cbuffer` it would output, because of an `fxc` bug where you couldn't use `register` on textures/samplers declared inside a `cbuffer` unless *every* field in the `cbuffer` used a `register` or `packoffset` modifier. Slang hasn't required that behavior in a while because it now splits textures and samplers, and the one test case where we needed `packoffset` to work around the `fxc` bug in the baseline HLSL has been macro-ified even more to work around the bug. The amount of churn in the test cases is unfortunate, but it continues to point at the weakness of any testing strategy that checks for exact equivalent between Slang's output and that of other compilers. We need to keep working to replace these tests with better alternatives. In `check.cpp` there is logic to perform implicit dereferencing, so that if you write `obj.f` where `obj` is a `ConstantBuffer<X>` (or some other "pointer-like" type) and `f` is a field in `X`, then this effectively translates as `(*obj).f`. That is, we dereference the value of type `ConstantBuffer<X>` to get a value of type `X`, and then refer to the field of the `X` value. There was a problem where the logic to insert that kind of implicit dereference operation was using a reference (`auto& type = ...`) for the type of the expression being dereferenced, and then clobbering it. This would mean that an expression of type `ConstantBuffer<X>` would have its type overwritten to be just `X` and then codegen would break later on. I'm not sure how we haven't run into that before. The `array-of-buffers` test case was added to confirm that we now support arrays of constant, structured, and byte-address buffers for both DXIL and SPIR-V output. Okay, so that was a lot of stuff, but hopefully it is clear how this all works to make the output of the compiler more consistent and explicit, while also supporting the required new functionality. * fixup: review feedback
Diffstat (limited to 'tests/hlsl/dxsdk/InstancingFX11')
-rw-r--r--tests/hlsl/dxsdk/InstancingFX11/Instancing.fx591
1 files changed, 0 insertions, 591 deletions
diff --git a/tests/hlsl/dxsdk/InstancingFX11/Instancing.fx b/tests/hlsl/dxsdk/InstancingFX11/Instancing.fx
deleted file mode 100644
index 3c8d45078..000000000
--- a/tests/hlsl/dxsdk/InstancingFX11/Instancing.fx
+++ /dev/null
@@ -1,591 +0,0 @@
-//TEST_IGNORE_FILE:
-//--------------------------------------------------------------------------------------
-// File: Instancing.fx
-//
-// Copyright (c) Microsoft Corporation. All rights reserved.
-//--------------------------------------------------------------------------------------
-
-//--------------------------------------------------------------------------------------
-// Input and output structures
-//--------------------------------------------------------------------------------------
-struct VSInstIn
-{
- float3 pos : POSITION;
- float3 norm : NORMAL;
- float2 tex : TEXTURE0;
- row_major float4x4 mTransform : mTransform;
-};
-
-struct VSSceneIn
-{
- float3 pos : POSITION;
- float3 norm : NORMAL;
- float2 tex : TEXTURE0;
-};
-
-struct VSGrassIn
-{
- float3 pos : POSITION;
- float3 norm : NORMAL;
- float2 tex : TEXTURE0;
- row_major float4x4 mTransform : mTransform;
- uint VertexID : SV_VertexID;
-};
-
-struct VSGrassOut
-{
- float3 pos : POSITION;
- float3 norm : NORMAL;
- float2 tex : TEXTURE0;
- uint VertexID : VERTID;
-};
-
-struct VSQuadIn
-{
- float3 pos : POSITION;
- float2 tex : TEXTURE0;
- row_major float4x4 mTransform : mTransform;
- float fOcc : fOcc;
- uint InstanceId : SV_InstanceID;
-};
-
-struct PSSceneIn
-{
- float4 pos : SV_Position;
- float2 tex : TEXTURE0;
- float4 color : COLOR0;
-};
-
-struct PSQuadIn
-{
- float4 pos : SV_Position;
- float3 tex : TEXTURE0;
- float4 color : COLOR0;
-};
-
-//--------------------------------------------------------------------------------------
-// Constant buffers
-//--------------------------------------------------------------------------------------
-cbuffer crarely
-{
- float4x4 g_mTreeMatrices[50];
- uint g_iNumTrees;
-};
-
-cbuffer ceveryframe
-{
- float4x4 g_mWorldViewProj;
- float4x4 g_mWorldView;
-};
-
-cbuffer cmultipleperframe
-{
- float g_GrassWidth;
- float g_GrassHeight;
- uint g_iGrassCoverage;
-};
-
-cbuffer cusercontrolled
-{
- float g_GrassMessiness;
-};
-
-struct light_struct
-{
- float4 direction;
- float4 color;
-};
-
-cbuffer cimmutable
-{
- light_struct g_lights[4] = {
- { float4(0.620275, 0.683659, 0.384537, 1), float4(0.75, 0.599, 0.405, 1) }, //sun
- { float4(0.063288, -0.987444, 0.144735, 1), float4(0.192, 0.273, 0.275, 1) }, //bottom
- { float4(0.23007, 0.785579, -0.574422, 1), float4(0.300, 0.292, 0.223, 1) }, //highlight
- { float4(-0.620275, -0.683659, -0.384537, 1), float4(0.0, 0.0, 0.1, 1) } //blue rim-light
- };
-
- float4 g_ambient = float4(0.4945,0.465,0.5,1);
-
- float g_occDimHeight = 2400.0; //scalar that tells us how much to darken the tree near the top
-};
-
-cbuffer cgrassblade
-{
- float3 g_positions[6] =
- {
- float3( -1, 0, 0 ),
- float3( -1, 2, 0 ),
- float3( 1, 0, 0 ),
- float3( 1, 2, 0 ),
-
- float3( -1, 0, 0 ),
- float3( -1, 2, 0 ),
- };
- float2 g_texcoords[6] =
- {
- float2(0,1),
- float2(0,0),
- float2(1,1),
- float2(1,0),
-
- float2(0,1),
- float2(0,0),
- };
-};
-
-//--------------------------------------------------------------------------------------
-// Textures and Samplers
-//--------------------------------------------------------------------------------------
-Texture2D g_txDiffuse;
-Texture2DArray g_tx2dArray;
-SamplerState g_samLinear
-{
- Filter = ANISOTROPIC;
- AddressU = Wrap;
- AddressV = Wrap;
-};
-
-Texture1D g_txRandom;
-SamplerState g_samPoint
-{
- Filter = MIN_MAG_MIP_POINT;
- AddressU = Wrap;
- AddressV = Wrap;
-};
-
-//--------------------------------------------------------------------------------------
-// State structures
-//--------------------------------------------------------------------------------------
-BlendState QuadAlphaBlendState
-{
- AlphaToCoverageEnable = TRUE;
- RenderTargetWriteMask[0] = 0x0F;
-};
-
-RasterizerState EnableMSAA
-{
- CullMode = BACK;
- MultisampleEnable = TRUE;
-};
-
-DepthStencilState DisableDepthTestWrite
-{
- DepthEnable = FALSE;
- DepthWriteMask = ZERO;
-};
-
-DepthStencilState EnableDepthTestWrite
-{
- DepthEnable = TRUE;
- DepthWriteMask = ALL;
-};
-
-BlendState NoBlending
-{
- AlphaToCoverageEnable = FALSE;
- BlendEnable[0] = FALSE;
-};
-
-//--------------------------------------------------------------------------------------
-// Sky vertex shader
-//--------------------------------------------------------------------------------------
-PSSceneIn VSSkymain(VSSceneIn input)
-{
- PSSceneIn output;
-
- //
- // Transform the vert to view-space
- //
- float4 v4Position = mul(float4(input.pos, 1), g_mWorldViewProj);
- output.pos = v4Position;
-
- //
- // Transfer the rest
- //
- output.tex = input.tex;
-
- output.color = float4(1,1,1,1);
-
- return output;
-}
-
-//--------------------------------------------------------------------------------------
-// CalcLighting helper function. Calculates lighting from 4 light sources, adds ambient
-// and attenuates for depth. Used by all techniques for lighting.
-//--------------------------------------------------------------------------------------
-float4 CalcLighting( float3 norm, float depth )
-{
- float4 color = float4(0,0,0,0);
-
- // add the contributions of 4 directional lights
- [unroll] for( int i=0; i<4; i++ )
- {
- color += saturate( dot(g_lights[i].direction,norm) )*g_lights[i].color;
- }
-
- // give some attenuation due to depth
- float attenuate = depth / 10000.0;
- float4 attenColor = float4(0.15, 0.2, 0.3, 0);
-
- // add it all up plus ambient
- return (1-attenuate*0.23)*(color + g_ambient) + attenColor*attenuate;
-}
-
-//--------------------------------------------------------------------------------------
-// Instancing vertex shader. Positions the vertices based upon the matrix stored
-// in the second vertex stream.
-//--------------------------------------------------------------------------------------
-PSSceneIn VSInstmain(VSInstIn input)
-{
- PSSceneIn output;
-
- //
- // Transform by our Sceneance matrix
- //
- float4 InstancePosition = mul(float4(input.pos, 1), input.mTransform);
- float4 ViewPos = mul(InstancePosition, g_mWorldView );
-
- //
- // Transform the vert to view-space
- //
- float4 v4Position = mul(InstancePosition, g_mWorldViewProj);
- output.pos = v4Position;
-
- //
- // Transfer the rest
- //
- output.tex = input.tex;
-
- //
- // dot the norm with the light dir
- //
- float3 norm = mul(input.norm,(float3x3)input.mTransform);
- output.color = CalcLighting( norm, ViewPos.z );
-
- //
- // Dim the color by how far up the tree we are.
- // This is a nice way to fake occlusion of the branches by the leaves.
- //
- output.color *= 1.0f - saturate(input.pos.y/g_occDimHeight);
-
-
- return output;
-}
-
-//--------------------------------------------------------------------------------------
-// Quad (leaf) vertex shader. Instances the quad over multiple leaf positions and
-// multiple trees. This demonstrates how to do double instancing.
-//--------------------------------------------------------------------------------------
-PSQuadIn VSQuadmain(VSQuadIn input)
-{
- PSQuadIn output;
-
- // base our leaf texture upon which instance id we are
- uint iLeaf = input.InstanceId/g_iNumTrees;
- uint iLeafTex = iLeaf%3;
- output.tex = float3(input.tex, float(iLeafTex) );
-
- //
- // Transform the position by the Instance matrix
- //
- int iTree = input.InstanceId - (input.InstanceId/g_iNumTrees)*g_iNumTrees;
- float4 vInstancePos = mul( float4(input.pos, 1), input.mTransform );
- float4 InstancePosition = mul(vInstancePos, g_mTreeMatrices[iTree] );
- float4 ViewPos = mul(InstancePosition, g_mWorldView );
-
- //
- // Transform the Instance position to view-space
- //
- output.pos = mul(InstancePosition, g_mWorldViewProj);
-
- // pack distance from the eye into the color alpha channel
- output.color = float4(input.fOcc,input.fOcc,input.fOcc,ViewPos.z);
-
- return output;
-}
-
-//--------------------------------------------------------------------------------------
-// Grass vertex shader. Basically a passthrough except for instancing the island base
-// mesh.
-//--------------------------------------------------------------------------------------
-VSGrassOut VSGrassmain(VSGrassIn input)
-{
- // simple transform into the instance space
- VSGrassOut output;
- output.pos = mul(float4(input.pos, 1), input.mTransform);
- output.norm = mul(input.norm, (float3x3)input.mTransform);
- output.tex = input.tex;
- output.VertexID = input.VertexID;
-
- return output;
-}
-
-//--------------------------------------------------------------------------------------
-// Quad (leaf) GS. Calculates the normal and lighting for the leaf.
-//--------------------------------------------------------------------------------------
-[maxvertexcount(3)]
-void GSQuadmain(triangle PSQuadIn input[3], inout TriangleStream<PSQuadIn> QuadStream)
-{
- PSQuadIn output;
-
- //
- // Calculate the face normal
- //
- float4 faceNormalA = input[1].pos.xyzw - input[0].pos.xyzw;
- float4 faceNormalB = input[2].pos.xyzw - input[0].pos.xyzw;
-
- //
- // Cross product
- //
- float3 faceNormal = cross(faceNormalA, faceNormalB);
-
- //
- // Normalize face normal
- //
- faceNormal = normalize(faceNormal);
-
- //
- // Dot face normal with some arbitrary light vectors
- //
- float4 color1 = CalcLighting( faceNormal, input[0].color.a );
- color1 *= input[0].color;
-
- //
- // Make sure we always have an alpha of 1
- //
- color1.a = 1.0;
-
- //
- // Emit out the new tri
- //
- for(int i=0; i<3; i++)
- {
- output.pos = input[i].pos;
- output.color = color1;
- output.tex = input[i].tex;
- QuadStream.Append(output);
- }
- QuadStream.RestartStrip();
-}
-
-//--------------------------------------------------------------------------------------
-// RandomDir helper. Samples a random dir out of our 1d random texture. In this case
-// we use a texture because the offset could be anywhere. If we were sampling linearly
-// then we would probably just use a buffer and load from that.
-//--------------------------------------------------------------------------------------
-float3 RandomDir(float fOffset)
-{
- float tCoord = (fOffset) / 300.0;
- return g_txRandom.SampleLevel( g_samPoint, tCoord, 0 );
-}
-
-//--------------------------------------------------------------------------------------
-// Helper to determing if a point is within a triangle
-//--------------------------------------------------------------------------------------
-bool IsInTriangle( float3 P, float3 A, float3 B, float3 C )
-{
- float3 crossA = cross( B-A, P-A );
- float3 crossB = cross( C-B, P-B );
- float3 crossC = cross( A-C, P-C );
-
- if( dot( crossA, crossB ) > 0 &&
- dot( crossB, crossC ) > 0 )
- {
- return true;
- }
- else
- {
- return false;
- }
-}
-
-//--------------------------------------------------------------------------------------
-// Gets a random orientation matrix based upon the RandomDir funciton
-//--------------------------------------------------------------------------------------
-float4x4 GetRandomOrientation( float3 Pos, float3 Norm, float fRandOffset )
-{
- float3 Tangent = RandomDir(fRandOffset);
-
- float3 Bitangent = normalize( cross( Tangent, Norm ) );
- Tangent = normalize( cross( Bitangent, Norm ) );
-
- float4x4 matWorld = { float4( Tangent, 0 ),
- float4( Norm, 0 ),
- float4( Bitangent, 0 ),
- float4( Pos, 1 ) };
- return matWorld;
-}
-
-//--------------------------------------------------------------------------------------
-// Generates an actual grass blade
-//--------------------------------------------------------------------------------------
-void OutputGrassBlade( VSGrassOut midPoint, inout TriangleStream<PSQuadIn> GrassStream, int iGrassTex )
-{
- PSQuadIn output;
-
- float4x4 mWorld = GetRandomOrientation( midPoint.pos, midPoint.norm, (float)midPoint.VertexID );
- float4 ViewPos = mul( midPoint.pos, g_mWorldView );
-
- float3 grassNorm = midPoint.norm;
- float4 color1 = CalcLighting( grassNorm, ViewPos.z );
-
- for(int v=0; v<6; v++)
- {
- float3 pos = g_positions[v];
- pos.x *= g_GrassWidth;
- pos.y *= g_GrassHeight;
-
- output.pos = mul( float4(pos,1), mWorld );
- output.pos = mul( output.pos, g_mWorldViewProj );
- output.tex = float3( g_texcoords[v], iGrassTex );
- output.color = color1;
-
- GrassStream.Append( output );
- }
-
- GrassStream.RestartStrip();
-}
-
-//--------------------------------------------------------------------------------------
-// Midpoint of the three vertices A,B,C
-//--------------------------------------------------------------------------------------
-VSGrassOut CalcMidPoint( VSGrassOut A, VSGrassOut B, VSGrassOut C )
-{
- VSGrassOut MidPoint;
-
- MidPoint.pos = (A.pos + B.pos + C.pos)/3.0f;
- MidPoint.norm = (A.norm + B.norm + C.norm)/3.0f;
- MidPoint.tex = (A.tex + B.tex + C.tex)/3.0f;
- MidPoint.VertexID = A.VertexID + B.VertexID + C.VertexID;
-
- return MidPoint;
-}
-
-//--------------------------------------------------------------------------------------
-// The actual grass geometry shader. This generates grass blades based upon an input
-// mesh (the tops of the islands) and a coverage texture. Each of the textures channels
-// determines how much of each of the 4 types of grass to place at a particular spot.
-//--------------------------------------------------------------------------------------
-[maxvertexcount(90)]
-void GSGrassmain(triangle VSGrassOut input[3], inout TriangleStream<PSQuadIn> GrassStream )
-{
- VSGrassOut MidPoint = CalcMidPoint( input[0], input[1], input[2] );
-
- float4 CoverageMask = g_tx2dArray.SampleLevel( g_samPoint, float3(MidPoint.tex,4), 0 );
- float cm[4];
- cm[0] = CoverageMask.r;
- cm[1] = CoverageMask.g;
- cm[2] = CoverageMask.b;
- cm[3] = CoverageMask.a;
-
- for(int g=0; g<4; g++)
- {
- float MaxBlades = float(g_iGrassCoverage)*cm[g];
- for(float i=0; i<MaxBlades; i++)
- {
- float randOffset = g*5 + (i+1);
- float3 Tan = RandomDir( MidPoint.pos.x + randOffset );
- float3 Len = normalize( RandomDir( MidPoint.pos.z + randOffset ) );
- float3 Shift = Len.x*g_GrassMessiness*normalize( cross( Tan, MidPoint.norm ) );
- VSGrassOut grassPoint = MidPoint;
- grassPoint.VertexID += randOffset;
- grassPoint.pos += Shift;
-
- //uncomment this to make the grass strictly conform to the mesh
- //if( IsInTriangle( grassPoint.pos, input[0].pos, input[1].pos, input[2].pos ) )
- {
- OutputGrassBlade( grassPoint, GrassStream, g );
- }
- }
- }
-}
-
-//--------------------------------------------------------------------------------------
-// PS for non-leaf or grass items.
-//--------------------------------------------------------------------------------------
-float4 PSScenemain(PSSceneIn input) : SV_Target
-{
- float4 color = g_txDiffuse.Sample( g_samLinear, input.tex ) * input.color;
- return color;
-}
-
-//--------------------------------------------------------------------------------------
-// PS for leaves and grass
-//--------------------------------------------------------------------------------------
-float4 PSQuadmain(PSQuadIn input) : SV_Target
-{
- float4 color = g_tx2dArray.Sample( g_samLinear, input.tex );
- color.xyz *= input.color.xyz;
- return color;
-}
-
-//--------------------------------------------------------------------------------------
-// Render instanced meshes with vertex lighting
-//--------------------------------------------------------------------------------------
-technique10 RenderInstancedVertLighting
-{
- pass p0
- {
- SetVertexShader( CompileShader( vs_4_0, VSInstmain() ) );
- SetGeometryShader( NULL );
- SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
-
- SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
- SetDepthStencilState( EnableDepthTestWrite, 0 );
- SetRasterizerState( EnableMSAA );
- }
-}
-
-//--------------------------------------------------------------------------------------
-// Skybox
-//--------------------------------------------------------------------------------------
-technique10 RenderSkybox
-{
- pass p0
- {
- SetVertexShader( CompileShader( vs_4_0, VSSkymain() ) );
- SetGeometryShader( NULL );
- SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
-
- SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
- SetDepthStencilState( DisableDepthTestWrite, 0 );
- SetRasterizerState( EnableMSAA );
- }
-}
-
-//--------------------------------------------------------------------------------------
-// Render leaves
-//--------------------------------------------------------------------------------------
-technique10 RenderQuad
-{
- pass p0
- {
-
- SetVertexShader( CompileShader( vs_4_0, VSQuadmain() ) );
- SetGeometryShader( CompileShader( gs_4_0, GSQuadmain() ) );
- SetPixelShader( CompileShader( ps_4_0, PSQuadmain() ) );
-
- SetBlendState( QuadAlphaBlendState, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
- SetDepthStencilState( EnableDepthTestWrite, 0 );
- SetRasterizerState( EnableMSAA );
- }
-}
-
-//--------------------------------------------------------------------------------------
-// Render grass
-//--------------------------------------------------------------------------------------
-technique10 RenderGrass
-{
- pass p0
- {
-
- SetVertexShader( CompileShader( vs_4_0, VSGrassmain() ) );
- SetGeometryShader( CompileShader( gs_4_0, GSGrassmain() ) );
- SetPixelShader( CompileShader( ps_4_0, PSQuadmain() ) );
-
- SetBlendState( QuadAlphaBlendState, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
- SetDepthStencilState( EnableDepthTestWrite, 0 );
- SetRasterizerState( EnableMSAA );
- }
-}