Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/pps/MarkusWegmann/Godray Shader Proof of Concept/Post_glow.cgfx @ 9767

Last change on this file since 9767 was 8708, checked in by marwegma, 14 years ago

My Stuff

File size: 9.2 KB
Line 
1/*********************************************************************NVMH3****
2$Revision$
3
4Copyright NVIDIA Corporation 2007
5TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
6*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
7OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
8AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
9BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
10WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
11BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
12LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
13NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
14
15
16To learn more about shading, shaders, and to bounce ideas off other shader
17    authors and users, visit the NVIDIA Shader Library Forums at:
18
19    http://developer.nvidia.com/forums/
20
21******************************************************************************/
22
23// Relative filter weights indexed by distance (in texels) from "home" texel
24//   (WT_0 is the "home" or center of the filter, WT_4 is four texels away)
25// Try changing these around for different filter patterns....
26#define WT_0 1.0
27#define WT_1 0.8
28#define WT_2 0.6
29#define WT_3 0.4
30#define WT_4 0.2
31// these ones are based on the above....
32#define WT_NORMALIZE (WT_0+2.0*(WT_1+WT_2+WT_3+WT_4))
33#define KW_0 (WT_0/WT_NORMALIZE)
34#define KW_1 (WT_1/WT_NORMALIZE)
35#define KW_2 (WT_2/WT_NORMALIZE)
36#define KW_3 (WT_3/WT_NORMALIZE)
37#define KW_4 (WT_4/WT_NORMALIZE)
38
39float Script : STANDARDSGLOBAL <
40    string UIWidget = "none";
41    string ScriptClass = "scene";
42    string ScriptOrder = "postprocess";
43    string ScriptOutput = "color";
44    string Script = "Technique=Main;";
45> = 0.8;
46
47// Standard full-screen imaging value
48float4 ClearColor <
49    string UIWidget = "color";
50    string UIName = "Clear (Bg) Color";
51> = {0,0,0,1.0};
52
53float ClearDepth <
54    string UIWidget = "None";
55> = 1.0;
56
57float2 ViewportSize : VIEWPORTPIXELSIZE <
58    string UIName="Screen Size";
59    string UIWidget="None";
60>;
61
62///////////////////////////////////////////////////////////
63/////////////////////////////////////// Tweakables ////////
64///////////////////////////////////////////////////////////
65
66float Glowness <
67    string UIName = "Glow Strength";
68    string UIWidget = "slider";
69    float UIMin = 0.0f;
70    float UIMax = 2.0f;
71    float UIStep = 0.05f;
72> = 0.7f;
73
74float Sceneness <
75    string UIName = "Scene Strength";
76    string UIWidget = "slider";
77    float UIMin = 0.0f;
78    float UIMax = 2.0f;
79    float UIStep = 0.05f;
80> = 0.3f;
81
82float GlowSpan <
83    string UIName = "Glow Step Size (Texels)";
84    string UIWidget = "slider";
85    float UIMin = 0.2f;
86    float UIMax = 8.0f;
87    float UIStep = 0.5f;
88> = 2.5f;
89
90///////////////////////////////////////////////////////////
91///////////////////////////// Render-to-Texture Targets ///
92///////////////////////////////////////////////////////////
93
94texture ScnMap : RENDERCOLORTARGET <
95    float2 ViewPortRatio = {1.0,1.0};
96    int MipLevels = 1;
97    string Format = "X8R8G8B8" ;
98    string UIWidget = "None";
99>;
100
101sampler2D ScnSamp = sampler_state {
102    texture = <ScnMap>;
103    WrapS = ClampToEdge;
104    WrapT = ClampToEdge;
105    MinFilter = Linear;
106    MagFilter = Linear;
107};
108
109texture GlowMap1 : RENDERCOLORTARGET <
110    float2 ViewPortRatio = {1.0,1.0};
111    int MipLevels = 1;
112    string Format = "X8R8G8B8" ;
113    string UIWidget = "None";
114>;
115
116sampler2D GlowSamp1 = sampler_state {
117    texture = <GlowMap1>;
118    WrapS = ClampToEdge;
119    WrapT = ClampToEdge;
120    MinFilter = Linear;
121    MagFilter = Linear;
122};
123
124texture GlowMap2 : RENDERCOLORTARGET <
125    float2 ViewPortRatio = {1.0,1.0};
126    int MipLevels = 1;
127    string Format = "X8R8G8B8" ;
128    string UIWidget = "None";
129>;
130
131sampler2D GlowSamp2 = sampler_state {
132    texture = <GlowMap2>;
133    WrapS = ClampToEdge;
134    WrapT = ClampToEdge;
135    MinFilter = Linear;
136    MagFilter = Linear;
137};
138
139texture DepthBuffer : RENDERDEPTHSTENCILTARGET <
140    float2 ViewPortRatio = {1.0,1.0};
141    string Format = "D24S8";
142    string UIWidget = "None";
143>;
144
145///////////////////////////////////////////////////////////
146///////////////////////////// Connector Data Struct ///////
147///////////////////////////////////////////////////////////
148
149struct OneTexelVertex {
150    float4 Position     : POSITION;
151    float2 UV           : TEXCOORD0;
152};
153
154// nine texcoords, to sample nine in-line texels
155struct NineTexelVertex
156{
157    float4 Position   : POSITION;
158    float2 UV    : TEXCOORD0;
159    float4 UV1   : TEXCOORD1; // xy AND zw used as UV coords
160    float4 UV2   : TEXCOORD2; // xy AND zw used as UV coords
161    float4 UV3   : TEXCOORD3; // xy AND zw used as UV coords
162    float4 UV4   : TEXCOORD4; // xy AND zw used as UV coords
163};
164
165///////////////////////////////////////////////////////////
166/////////////////////////////////// Vertex Shaders ////////
167///////////////////////////////////////////////////////////
168
169// vertex shader to align blur samples vertically
170NineTexelVertex vert9BlurVS(
171                float3 Position : POSITION,
172                float2 UV : TEXCOORD0
173) {
174    NineTexelVertex OUT = (NineTexelVertex)0;
175    OUT.Position = float4(Position, 1);
176    float TexelIncrement = GlowSpan/ViewportSize.y;
177    float2 Coord = float2(UV.xy);
178    OUT.UV = Coord;
179    OUT.UV1 = float4(Coord.x, Coord.y + TexelIncrement,
180                     Coord.x, Coord.y - TexelIncrement);
181    OUT.UV2 = float4(Coord.x, Coord.y + TexelIncrement*2,
182                     Coord.x, Coord.y - TexelIncrement*2);
183    OUT.UV3 = float4(Coord.x, Coord.y + TexelIncrement*3,
184                     Coord.x, Coord.y - TexelIncrement*3);
185    OUT.UV4 = float4(Coord.x, Coord.y + TexelIncrement*4,
186                     Coord.x, Coord.y - TexelIncrement*4);
187    return OUT;
188}
189
190// vertex shader to align blur samples horizontally
191NineTexelVertex horiz9BlurVS(
192                float3 Position : POSITION,
193                float2 UV : TEXCOORD0
194) {
195    NineTexelVertex OUT = (NineTexelVertex)0;
196    OUT.Position = float4(Position, 1);
197    float TexelIncrement = GlowSpan/ViewportSize.x;
198    float2 Coord = float2(UV.xy);
199    OUT.UV = Coord;
200    OUT.UV1 = float4(Coord.x + TexelIncrement, Coord.y,
201                     Coord.x - TexelIncrement, Coord.y);
202    OUT.UV2 = float4(Coord.x + TexelIncrement*2, Coord.y,
203                     Coord.x - TexelIncrement*2, Coord.y);
204    OUT.UV3 = float4(Coord.x + TexelIncrement*3, Coord.y,
205                     Coord.x - TexelIncrement*3, Coord.y);
206    OUT.UV4 = float4(Coord.x + TexelIncrement*4, Coord.y,
207                     Coord.x - TexelIncrement*4, Coord.y);
208    return OUT;
209}
210
211OneTexelVertex ScreenQuadVS(
212                float3 Position : POSITION,
213                float2 UV       : TEXCOORD0
214) {
215    OneTexelVertex OUT = (OneTexelVertex)0;
216    OUT.Position = float4(Position, 1);
217    OUT.UV = float2(UV.xy);
218    return OUT;
219}
220
221///////////////////////////////////////////////////////////
222/////////////////////////////////// Pixel Shaders /////////
223///////////////////////////////////////////////////////////
224
225float4 blur9PS(NineTexelVertex IN,
226                uniform sampler2D SrcSamp) : COLOR
227{   
228    float4 OutCol = tex2D(SrcSamp, IN.UV4.zw) * KW_4;
229    OutCol += tex2D(SrcSamp, IN.UV3.zw) * KW_3;
230    OutCol += tex2D(SrcSamp, IN.UV2.zw) * KW_2;
231    OutCol += tex2D(SrcSamp, IN.UV1.zw) * KW_1;
232    OutCol += tex2D(SrcSamp, IN.UV) * KW_0;
233    OutCol += tex2D(SrcSamp, IN.UV1.xy) * KW_1;
234    OutCol += tex2D(SrcSamp, IN.UV2.xy) * KW_2;
235    OutCol += tex2D(SrcSamp, IN.UV3.xy) * KW_3;
236    OutCol += tex2D(SrcSamp, IN.UV4.xy) * KW_4;
237    return OutCol;
238}
239
240// add glow on top of model
241
242float4 PS_GlowPass(OneTexelVertex IN) : COLOR
243{   
244    float4 scn = Sceneness * tex2D(ScnSamp, IN.UV);
245    float3 glow = Glowness * tex2D(GlowSamp2, IN.UV).xyz;
246    return float4(scn.xyz+glow,scn.w);
247
248
249////////////////////////////////////////////////////////////
250/////////////////////////////////////// techniques /////////
251////////////////////////////////////////////////////////////
252
253technique Main <
254        string Script =
255        "RenderColorTarget0=ScnMap;"
256        "RenderDepthStencilTarget=DepthBuffer;"
257                "ClearSetColor=ClearColor;"
258                "ClearSetDepth=ClearDepth;"
259                "Clear=Color;"
260                "Clear=Depth;"
261            "ScriptExternal=color;"
262        "Pass=BlurGlowBuffer_Horz;"
263        "Pass=BlurGlowBuffer_Vert;"
264        "Pass=GlowPass;";
265> {
266    pass BlurGlowBuffer_Horz <
267        string Script = "RenderColorTarget=GlowMap1;"
268                        "RenderDepthStencilTarget=DepthBuffer;"
269                        "Draw=Buffer;";
270    > {
271        VertexShader = compile vp40 horiz9BlurVS();
272        CullFaceEnable = false;
273        DepthTestEnable = false;
274        PixelShader = compile fp40 blur9PS(ScnSamp);
275    }
276    pass BlurGlowBuffer_Vert <
277        string Script = "RenderColorTarget=GlowMap2;"
278                        "RenderDepthStencilTarget=DepthBuffer;"
279                        "Draw=Buffer;";
280    > {
281        VertexShader = compile vp40 vert9BlurVS();
282        CullFaceEnable = false;
283        DepthTestEnable = false;
284        PixelShader = compile fp40 blur9PS(GlowSamp1);
285    }
286    pass GlowPass <
287        string Script= "RenderColorTarget0=;"
288                        "RenderDepthStencilTarget=;"
289                        "Draw=Buffer;";         
290    >
291    {
292        CullFaceEnable = false;
293        DepthTestEnable = false;
294        DepthMask = false;
295        BlendEnable = false;
296        VertexShader = compile vp40 ScreenQuadVS();
297        PixelShader = compile fp40 PS_GlowPass();       
298    }
299}
300
301////////////// eof ///
Note: See TracBrowser for help on using the repository browser.