Skip to content

Commit 2c9d8de

Browse files
authored
Merge pull request #228 from RainbowZerg/xd_dev
Point-lights, light virtual size and r2_parallax_h fixes.
2 parents d47af9c + 1099772 commit 2c9d8de

File tree

8 files changed

+482
-2
lines changed

8 files changed

+482
-2
lines changed

res/gamedata/shaders/r2/common.h

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
#ifndef COMMON_H
2+
#define COMMON_H
3+
4+
// #define USE_SUPER_SPECULAR
5+
6+
#include "shared\common.h"
7+
//////////////////////////////////////////////////////////////////////////////////////////
8+
// *** options
9+
10+
// #define DBG_TEST_NMAP
11+
// #define DBG_TEST_NMAP_SPEC
12+
// #define DBG_TEST_SPEC
13+
// #define DBG_TEST_LIGHT
14+
// #define DBG_TEST_LIGHT_SPEC
15+
16+
// #define USE_GAMMA_22
17+
// #define USE_SJITTER
18+
// #define USE_SUNFILTER
19+
// #define USE_FETCH4
20+
// #define USE_MBLUR //- HW-options defined
21+
// #define USE_HWSMAP //- HW-options defined
22+
23+
// #define USE_HWSMAP_PCF //- nVidia GF3+, R600+
24+
25+
// #define USE_BRANCHING //- HW-options defined
26+
// #define USE_VTF //- HW-options defined, VertexTextureFetch
27+
// #define FP16_FILTER //- HW-options defined
28+
// #define FP16_BLEND //- HW-options defined
29+
//
30+
// #define USE_PARALLAX //- shader defined
31+
// #define USE_TDETAIL //- shader defined
32+
// #define USE_LM_HEMI //- shader defined
33+
// #define USE_DISTORT //- shader defined
34+
// #define USE_SUNMASK //- shader defined
35+
// #define DBG_TMAPPING
36+
//////////////////////////////////////////////////////////////////////////////////////////
37+
#ifndef SMAP_size
38+
#define SMAP_size 1024
39+
#endif
40+
41+
#ifdef USE_R2_STATIC_SUN
42+
# define xmaterial half(1.0h/4.h)
43+
#else
44+
# define xmaterial half(L_material.w)
45+
#endif
46+
//////////////////////////////////////////////////////////////////////////////////////////
47+
uniform half4 hemi_cube_pos_faces;
48+
uniform half4 hemi_cube_neg_faces;
49+
uniform half4 L_material; // 0,0,0,mid
50+
uniform half4 Ldynamic_color; // dynamic light color (rgb1) - spot/point
51+
uniform half4 Ldynamic_pos; // dynamic light pos+1/range(w) - spot/point
52+
uniform half4 Ldynamic_dir; // dynamic light direction - sun
53+
54+
uniform half4 J_direct [6];
55+
uniform half4 J_spot [6];
56+
57+
half calc_fogging (half4 w_pos) { return dot(w_pos,fog_plane); }
58+
half2 calc_detail (half3 w_pos) {
59+
float dtl = distance (w_pos,eye_position)*dt_params.w;
60+
dtl = min (dtl*dtl, 1);
61+
half dt_mul = 1 - dtl; // dt* [1 .. 0 ]
62+
half dt_add = .5 * dtl; // dt+ [0 .. 0.5]
63+
return half2 (dt_mul,dt_add);
64+
}
65+
float3 calc_reflection (float3 pos_w, float3 norm_w)
66+
{
67+
return reflect(normalize(pos_w-eye_position), norm_w);
68+
}
69+
70+
float3 calc_sun_r1 (float3 norm_w) { return L_sun_color*saturate(dot((norm_w),-L_sun_dir_w)); }
71+
float3 calc_model_hemi_r1 (float3 norm_w) { return max(0,norm_w.y)*L_hemi_color; }
72+
float3 calc_model_lq_lighting (float3 norm_w) { return L_material.x*calc_model_hemi_r1(norm_w) + L_ambient + L_material.y*calc_sun_r1(norm_w); }
73+
74+
//////////////////////////////////////////////////////////////////////////////////////////
75+
struct v_static {
76+
float4 P : POSITION; // (float,float,float,1)
77+
float4 Nh : NORMAL; // (nx,ny,nz,hemi occlusion)
78+
float4 T : TANGENT; // tangent
79+
float4 B : BINORMAL; // binormal
80+
float2 tc : TEXCOORD0; // (u,v)
81+
float2 lmh : TEXCOORD1; // (lmu,lmv)
82+
float4 color : COLOR0; // (r,g,b,dir-occlusion)
83+
};
84+
85+
struct v_tree {
86+
float4 P : POSITION; // (float,float,float,1)
87+
float4 Nh : NORMAL; // (nx,ny,nz)
88+
float3 T : TANGENT; // tangent
89+
float3 B : BINORMAL; // binormal
90+
float4 tc : TEXCOORD0; // (u,v,frac,???)
91+
};
92+
93+
struct v_model {
94+
float4 P : POSITION; // (float,float,float,1)
95+
float3 N : NORMAL; // (nx,ny,nz)
96+
float3 T : TANGENT; // (nx,ny,nz)
97+
float3 B : BINORMAL; // (nx,ny,nz)
98+
float2 tc : TEXCOORD0; // (u,v)
99+
};
100+
101+
struct v_detail {
102+
float4 pos : POSITION; // (float,float,float,1)
103+
int4 misc : TEXCOORD0; // (u(Q),v(Q),frac,matrix-id)
104+
};
105+
106+
#ifdef USE_HWSMAP
107+
struct v_shadow_direct_aref
108+
{
109+
float4 hpos: POSITION; // Clip-space position (for rasterization)
110+
float2 tc0: TEXCOORD1; // Diffuse map for aref
111+
};
112+
struct v_shadow_direct
113+
{
114+
float4 hpos: POSITION; // Clip-space position (for rasterization)
115+
};
116+
#else
117+
struct v_shadow_direct_aref
118+
{
119+
float4 hpos: POSITION; // Clip-space position (for rasterization)
120+
float depth: TEXCOORD0; // Depth
121+
float2 tc0: TEXCOORD1; // Diffuse map for aref
122+
};
123+
struct v_shadow_direct
124+
{
125+
float4 hpos: POSITION; // Clip-space position (for rasterization)
126+
float depth: TEXCOORD0; // Depth
127+
};
128+
129+
130+
#endif
131+
132+
//////////////////////////////////////////////////////////////////////////////////////////
133+
struct p_bumped {
134+
float4 hpos : POSITION;
135+
#if defined(USE_R2_STATIC_SUN) && !defined(USE_LM_HEMI)
136+
float4 tcdh : TEXCOORD0; // Texture coordinates, w=sun_occlusion
137+
#else
138+
float2 tcdh : TEXCOORD0; // Texture coordinates
139+
#endif
140+
float4 position : TEXCOORD1; // position + hemi
141+
half3 M1 : TEXCOORD2; // nmap 2 eye - 1
142+
half3 M2 : TEXCOORD3; // nmap 2 eye - 2
143+
half3 M3 : TEXCOORD4; // nmap 2 eye - 3
144+
#ifdef USE_TDETAIL
145+
float2 tcdbump : TEXCOORD5; // d-bump
146+
#ifdef USE_LM_HEMI
147+
float2 lmh : TEXCOORD6; // lm-hemi
148+
#endif
149+
#else
150+
#ifdef USE_LM_HEMI
151+
float2 lmh : TEXCOORD5; // lm-hemi
152+
#endif
153+
#endif
154+
};
155+
//////////////////////////////////////////////////////////////////////////////////////////
156+
struct p_flat {
157+
float4 hpos : POSITION;
158+
#if defined(USE_R2_STATIC_SUN) && !defined(USE_LM_HEMI)
159+
float4 tcdh : TEXCOORD0; // Texture coordinates, w=sun_occlusion
160+
#else
161+
float2 tcdh : TEXCOORD0; // Texture coordinates
162+
#endif
163+
float4 position : TEXCOORD1; // position + hemi
164+
half3 N : TEXCOORD2; // Eye-space normal (for lighting)
165+
#ifdef USE_TDETAIL
166+
float2 tcdbump : TEXCOORD3; // d-bump
167+
#ifdef USE_LM_HEMI
168+
float2 lmh : TEXCOORD4; // lm-hemi
169+
#endif
170+
#else
171+
#ifdef USE_LM_HEMI
172+
float2 lmh : TEXCOORD3; // lm-hemi
173+
#endif
174+
#endif
175+
};
176+
177+
//////////////////////////////////////////////////////////////////////////////////////////
178+
struct f_deffer {
179+
half4 position : COLOR0; // px,py,pz, m-id
180+
half4 Ne : COLOR1; // nx,ny,nz, hemi
181+
half4 C : COLOR2; // r, g, b, gloss
182+
};
183+
//////////////////////////////////////////////////////////////////////////////////////////
184+
struct p_screen {
185+
float4 hpos : POSITION;
186+
float2 tc0 : TEXCOORD0; // Texture coordinates (for sampling maps)
187+
};
188+
//////////////////////////////////////////////////////////////////////////////////////////
189+
// Geometry phase / deferring //
190+
uniform sampler2D s_base; //
191+
uniform sampler2D s_bump; //
192+
uniform sampler2D s_bumpX; //
193+
uniform sampler2D s_detail; //
194+
uniform sampler2D s_detailBump; //
195+
uniform sampler2D s_detailBumpX; // Error for bump detail
196+
uniform sampler2D s_bumpD; //
197+
uniform sampler2D s_hemi; //
198+
199+
uniform sampler2D s_mask; //
200+
201+
uniform sampler2D s_dt_r; //
202+
uniform sampler2D s_dt_g; //
203+
uniform sampler2D s_dt_b; //
204+
uniform sampler2D s_dt_a; //
205+
206+
uniform sampler2D s_dn_r; //
207+
uniform sampler2D s_dn_g; //
208+
uniform sampler2D s_dn_b; //
209+
uniform sampler2D s_dn_a; //
210+
211+
//////////////////////////////////////////////////////////////////////////////////////////
212+
// Lighting/shadowing phase //
213+
uniform sampler2D s_depth; //
214+
uniform sampler2D s_position; //
215+
uniform sampler2D s_normal; //
216+
uniform sampler s_lmap; // 2D/cube projector lightmap
217+
uniform sampler3D s_material; //
218+
uniform sampler1D s_attenuate; //
219+
//////////////////////////////////////////////////////////////////////////////////////////
220+
// Combine phase //
221+
uniform sampler2D s_diffuse; // rgb.a = diffuse.gloss
222+
uniform sampler2D s_accumulator; // rgb.a = diffuse.specular
223+
uniform sampler2D s_generic; //
224+
uniform sampler2D s_bloom; //
225+
uniform sampler s_image; // used in various post-processing
226+
uniform sampler2D s_tonemap; // actually MidleGray / exp(Lw + eps)
227+
//////////////////////////////////////////////////////////////////////////////////////////
228+
// Defines //
229+
#define def_gloss half(2.f /255.f)
230+
#define def_aref half(200.f/255.f)
231+
#define def_dbumph half(0.333f)
232+
#define def_virtualh half(0.05f) // 5cm
233+
#define def_distort half(0.05f) // we get -0.5 .. 0.5 range, this is -512 .. 512 for 1024, so scale it
234+
#define def_hdr half(9.h) // hight luminance range half(3.h)
235+
#define def_hdr_clip half(0.75h) //
236+
237+
//////////////////////////////////////////////////////////////////////////////////////////
238+
#define LUMINANCE_VECTOR half3(0.3f, 0.38f, 0.22f)
239+
void tonemap (out half4 low, out half4 high, half3 rgb, half scale)
240+
{
241+
rgb = rgb*scale ;
242+
243+
const float fWhiteIntensity = 1.7;
244+
245+
const float fWhiteIntensitySQR = fWhiteIntensity*fWhiteIntensity;
246+
#ifdef USE_BRANCHING // ps_3_0
247+
//low = rgb.xyzz ;
248+
249+
low = ( (rgb*(1+rgb/fWhiteIntensitySQR)) / (rgb+1) ).xyzz;
250+
251+
high = low/def_hdr ; // 8x dynamic range
252+
#else
253+
low = half4 ( ( (rgb*(1+rgb/fWhiteIntensitySQR)) / (rgb+1) ), 0 ) ;
254+
high = half4 (rgb/def_hdr, 0 ) ; // 8x dynamic range
255+
#endif
256+
257+
/*
258+
rgb = rgb*scale;
259+
260+
low = rgb.xyzz;
261+
high = low/def_hdr; // 8x dynamic range
262+
*/
263+
264+
// low = half4 (rgb, 0);
265+
// rgb /= def_hdr ;
266+
// high = half4 (rgb, dot(rgb,0.333f)-def_hdr_clip) ;
267+
}
268+
half4 combine_bloom (half3 low, half4 high) {
269+
return half4(low + high*high.a, 1.h);
270+
}
271+
272+
float3 v_hemi (float3 n) { return L_hemi_color*(.5f + .5f*n.y); }
273+
float3 v_hemi_wrap (float3 n, float w) { return L_hemi_color*(w + (1-w)*n.y); }
274+
float3 v_sun (float3 n) { return L_sun_color*dot(n,-L_sun_dir_w); }
275+
float3 v_sun_wrap (float3 n, float w) { return L_sun_color*(w+(1-w)*dot(n,-L_sun_dir_w)); }
276+
half3 p_hemi (float2 tc) {
277+
// half3 t_lmh = tex2D (s_hemi, tc);
278+
// return dot (t_lmh,1.h/4.h);
279+
half4 t_lmh = tex2D (s_hemi, tc);
280+
return t_lmh.a;
281+
}
282+
283+
half get_hemi( half4 lmh)
284+
{
285+
return lmh.a;
286+
}
287+
288+
half get_sun( half4 lmh)
289+
{
290+
return lmh.g;
291+
}
292+
293+
// contrast function
294+
half Contrast(half Input, half ContrastPower)
295+
{
296+
//piecewise contrast function
297+
bool IsAboveHalf = Input > 0.5 ;
298+
half ToRaise = saturate(2*(IsAboveHalf ? 1-Input : Input));
299+
half Output = 0.5*pow(ToRaise, ContrastPower);
300+
Output = IsAboveHalf ? 1-Output : Output;
301+
return Output;
302+
}
303+
304+
#define FXPS technique _render{pass _code{PixelShader=compile ps_3_0 main();}}
305+
#define FXVS technique _render{pass _code{VertexShader=compile vs_3_0 main();}}
306+
307+
#endif
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//////////////////////////////////////////////////
2+
// All comments by Nivenhbro are preceded by !
3+
/////////////////////////////////////////////////
4+
5+
6+
#ifndef SHARED_COMMON_H
7+
#define SHARED_COMMON_H
8+
//
9+
uniform half3x4 m_W;
10+
uniform half3x4 m_V;
11+
uniform half4x4 m_P;
12+
uniform half3x4 m_WV;
13+
uniform half4x4 m_VP;
14+
uniform half4x4 m_WVP;
15+
uniform float4x4 m_texgen;
16+
uniform float4x4 mVPTexgen;
17+
uniform half4 timers;
18+
uniform half4 fog_plane;
19+
uniform float4 fog_params; // x=near*(1/(far-near)), ?,?, w = -1/(far-near)
20+
uniform half4 fog_color;
21+
uniform float3 L_sun_color;
22+
uniform half3 L_sun_dir_w;
23+
uniform half3 L_sun_dir_e;
24+
uniform half4 L_hemi_color;
25+
uniform half4 L_ambient; // L_ambient.w = skynbox-lerp-factor
26+
uniform float3 eye_position;
27+
uniform half3 eye_direction;
28+
uniform half3 eye_normal;
29+
uniform float4 dt_params;
30+
uniform float4 parallax;
31+
32+
half3 unpack_normal (half3 v) { return 2*v-1; }
33+
half3 unpack_bx2 (half3 v) { return 2*v-1; }
34+
float3 unpack_bx4 (float3 v) { return 4*v-2; } //!reduce the amount of stretching from 4*v-2 and increase precision
35+
36+
float2 unpack_tc_base (float2 tc, float du, float dv) {
37+
return (tc.xy + float2 (du,dv))*(32.f/32768.f); //!Increase from 32bit to 64bit floating point
38+
}
39+
40+
float2 unpack_tc_lmap (half2 tc) { return tc*(1.f/32768.f); } // [-1 .. +1 ]
41+
42+
float calc_cyclic (float x) {
43+
float phase = 1/(2*3.141592653589f);
44+
float sqrt2 = 1.4142136f;
45+
float sqrt2m2 = 2.8284271f;
46+
float f = sqrt2m2*frac(x)-sqrt2; // [-sqrt2 .. +sqrt2] !No changes made, but this controls the grass wave (which is violent if I must say)
47+
return f*f - 1.f; // [-1 .. +1]
48+
}
49+
float2 calc_xz_wave (float2 dir2D, float frac) {
50+
// Beizer
51+
float2 ctrl_A = float2(0.f, 0.f );
52+
float2 ctrl_B = float2(dir2D.x, dir2D.y );
53+
return lerp (ctrl_A, ctrl_B, frac); //!This calculates tree wave. No changes made
54+
}
55+
56+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef common_defines_h_included
2+
#define common_defines_h_included
3+
4+
//////////////////////////////////////////////////////////////////////////////////////////
5+
// Defines //
6+
#define def_gloss float(2.f /255.f)
7+
#define def_aref float(200.f/255.f)
8+
#define def_dbumph float(0.333f)
9+
#define def_virtualh float(0.05f) // 5cm
10+
#define def_distort float(0.05f) // we get -0.5 .. 0.5 range, this is -512 .. 512 for 1024, so scale it
11+
#define def_hdr float(9.h) // hight luminance range float(3.h)
12+
#define def_hdr_clip float(0.75h) //
13+
14+
#define LUMINANCE_VECTOR float3(0.3f, 0.38f, 0.22f)
15+
16+
//////////////////////////////////////////////////////////////////////////////////////////
17+
#ifndef SMAP_size
18+
#define SMAP_size 1024
19+
#endif
20+
//////////////////////////////////////////////////////////////////////////////////////////
21+
22+
#endif // common_defines_h_included

0 commit comments

Comments
 (0)