0001: //--------------------------------------------------------------------------------------
0002: // File: main.fx
0003: //
0004: // The effect file for the main sample.  
0005: // 
0006: // Copyright (c) Microsoft Corporation. All rights reserved.
0007: //--------------------------------------------------------------------------------------
0008: 
0009: 
0010: //--------------------------------------------------------------------------------------
0011: // Global variables
0012: //--------------------------------------------------------------------------------------
0013: float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
0014: 
0015: float    g_fTexSize;
0016: float    g_fInvTexSize;
0017: 
0018: // @@@
0019: float    g_fWangTextureSize;
0020: float    g_fWangLODCount;
0021: 
0022: //-----------------------------------------------------------------------------
0023: // Texture samplers
0024: //-----------------------------------------------------------------------------
0025: texture Texture;
0026: sampler Sampler = 
0027: sampler_state
0028: {
0029:     Texture = <Texture>;
0030:     MinFilter = LINEAR;
0031:     MagFilter = LINEAR;
0032:     MipFilter = LINEAR;
0033: 
0034:     AddressU = Clamp;
0035:     AddressV = Clamp;
0036: };
0037: //-----------------------------------------------------------------------------
0038: texture WangTexture;
0039: sampler WangSampler = 
0040: sampler_state
0041: {
0042:     Texture = <WangTexture>;
0043:     MinFilter = LINEAR;
0044:     MagFilter = LINEAR;
0045:     MipFilter = NONE;
0046: 
0047:     AddressU = Clamp;
0048:     AddressV = Clamp;
0049: };
0050: //-----------------------------------------------------------------------------
0051: texture RandTexture;
0052: sampler RandSampler = 
0053: sampler_state
0054: {
0055:     Texture = <RandTexture>;
0056:     MinFilter = POINT;
0057:     MagFilter = POINT;
0058:     MipFilter = NONE;
0059: 
0060:     AddressU = Wrap;
0061:     AddressV = Wrap;
0062: };
0063: //-----------------------------------------------------------------------------
0064: // @@@
0065: texture SpreadWangTexture;
0066: sampler SpreadWangSampler = 
0067: sampler_state
0068: {
0069:     Texture = <SpreadWangTexture>;
0070:     MinFilter = LINEAR;
0071:     MagFilter = LINEAR;
0072:     MipFilter = NONE;
0073: 
0074:     AddressU = Wrap;
0075:     AddressV = Wrap;
0076: };
0077: //-----------------------------------------------------------------------------
0078: texture LODLookupTexture;
0079: sampler LODLookupSampler = 
0080: sampler_state
0081: {
0082:     Texture = <LODLookupTexture>;
0083:     MinFilter = POINT;
0084:     MagFilter = POINT;
0085:     MipFilter = LINEAR;
0086: 
0087:     AddressU = Wrap;
0088:     AddressV = Wrap;
0089: };
0090: 
0091: 
0092: //-----------------------------------------------------------------------------
0093: // Vertex shader output structure
0094: //-----------------------------------------------------------------------------
0095: struct VS_OUTPUT
0096: {
0097:     float4 Position : POSITION;
0098:     float2 TextureUV : TEXCOORD0;
0099: };
0100: 
0101: 
0102: //-----------------------------------------------------------------------------
0103: // Name: WangVS
0104: // Type: Vertex shader                                      
0105: // Desc: standard transform and lighting.
0106: //-----------------------------------------------------------------------------
0107: VS_OUTPUT WangVS( float4 vPos : POSITION, 
0108:                              float3 vNormal : NORMAL,
0109:                              float2 vTexCoord0 : TEXCOORD0 )
0110: {
0111:     VS_OUTPUT Output;
0112:   
0113:     // tranform vertex position into screen space
0114:     Output.Position = mul(vPos, g_mWorldViewProjection );
0115:     
0116:     // Just copy the texture coordinate through
0117:     Output.TextureUV.xy = vTexCoord0;
0118:   
0119:     return Output;    
0120: }
0121: 
0122: 
0123: //-----------------------------------------------------------------------------
0124: // Name: WangPS
0125: // Type: Pixel shader
0126: // Desc: This shader simply outputs the pixel's color 
0127: //-----------------------------------------------------------------------------
0128: float4 WangPS( VS_OUTPUT In ) : COLOR
0129: { 
0130:     float2 TexCoord;
0131:     float2 Tex;
0132:     
0133:     TexCoord.x = 0.25 * frac( g_fTexSize * In.TextureUV.x );
0134:     TexCoord.y = 0.25 * frac( g_fTexSize * In.TextureUV.y );
0135:     
0136:     float2 e0 = tex2D( RandSampler, In.TextureUV ).xy;
0137:     float ex = tex2D( RandSampler, In.TextureUV + float2( g_fInvTexSize, 0.0 ) ).x;
0138:     float ey = tex2D( RandSampler, In.TextureUV + float2( 0.0, g_fInvTexSize ) ).y;
0139:     
0140:     if( e0.x < 0.5 )
0141:     {
0142:         if( ex < 0.5 )
0143:         {
0144: //          TexCoord.x += 0.0;
0145:         }else{
0146:             TexCoord.x += 0.25;
0147:         }
0148:     }else{
0149:         if( ex < 0.5 )
0150:         {
0151:             TexCoord.x += 0.75;
0152:         }else{
0153:             TexCoord.x += 0.50;
0154:         }
0155:     }
0156:     if( ey < 0.5 )
0157:     {
0158:         if( e0.y < 0.5 )
0159:         {
0160:             TexCoord.y += 0.75;
0161:         }else{
0162:             TexCoord.y += 0.50;
0163:         }
0164:     }else{
0165:         if( e0.y < 0.5 )
0166:         {
0167: //          TexCoord.y += 0.0;
0168:         }else{
0169:             TexCoord.y += 0.25;
0170:         }
0171:     }
0172: 
0173:    return tex2D( WangSampler, TexCoord );
0174: }
0175: 
0176: // @@@
0177: //-----------------------------------------------------------------------------
0178: // Name: MipmapWangPS
0179: // Type: Pixel shader
0180: // Desc: This shader simply outputs the pixel's color 
0181: //-----------------------------------------------------------------------------
0182: float4 MipmapWangPS( VS_OUTPUT In ) : COLOR
0183: { 
0184:     float2 LODLookupCoord;
0185:     float2 TileCoord;
0186:     float2 TileOffset;
0187: 
0188:     float scale;
0189:     float texHeight;
0190:     float texHeightAll = g_fWangTextureSize*2 + g_fWangLODCount*2;
0191: 
0192:     //------------------------------------------------------------------
0193:     // fetch lod index
0194: 
0195:     LODLookupCoord.x = g_fTexSize * In.TextureUV.x;
0196:     LODLookupCoord.y = g_fTexSize * In.TextureUV.y;
0197:     float4 LODValue = tex2D( LODLookupSampler, LODLookupCoord );
0198: 
0199:     float lod;
0200:     float lodIntpl;
0201:     lodIntpl = modf( LODValue.x * 255, lod );
0202: 
0203:     //------------------------------------------------------------------
0204:     // fetch tile offset
0205: 
0206:     TileOffset = tex2D( RandSampler, In.TextureUV ).xy;
0207: 
0208:     //------------------------------------------------------------------
0209:     // fetch front lod texture
0210: 
0211:     scale     = pow( 0.5, lod );
0212:     texHeight = g_fWangTextureSize * scale;
0213: 
0214:     TileCoord.xy = frac( LODLookupCoord.xy );
0215:     TileCoord.xy *= 0.25;
0216:     TileCoord.xy += TileOffset.xy;
0217: 
0218:     TileCoord.x = TileCoord.x * scale;
0219:     TileCoord.y = TileCoord.y * texHeight / texHeightAll;
0220: 
0221:     TileCoord.y += ( texHeight + 2*(g_fWangLODCount-1-lod) + 1 ) / texHeightAll;
0222: 
0223:     float4 FrontTex = tex2D( SpreadWangSampler, TileCoord );
0224: 
0225:     //------------------------------------------------------------------
0226:     // fetch back lod texture
0227: 
0228:     lod += 1;
0229: 
0230:     scale     = pow( 0.5, lod );
0231:     texHeight = g_fWangTextureSize * scale;
0232: 
0233:     TileCoord.xy = frac( LODLookupCoord.xy );
0234:     TileCoord.xy *= 0.25;
0235:     TileCoord.xy += TileOffset.xy;
0236: 
0237:     TileCoord.x = TileCoord.x * scale;
0238:     TileCoord.y = TileCoord.y * texHeight / texHeightAll;
0239: 
0240:     TileCoord.y += ( texHeight + 2*(g_fWangLODCount-1-lod) + 1 ) / texHeightAll;
0241: 
0242:     float4 BackTex = tex2D( SpreadWangSampler, TileCoord );
0243: 
0244:     //------------------------------------------------------------------
0245:     // blend between front and back
0246: 
0247:     return (1-lodIntpl) * FrontTex + lodIntpl * BackTex;
0248: }
0249: 
0250: 
0251: //--------------------------------------------------------------------------------------
0252: // Techniques
0253: //--------------------------------------------------------------------------------------
0254: technique WangTechnique
0255: {
0256:     pass P0
0257:     {
0258:         VertexShader = compile vs_1_1 WangVS();
0259: //        PixelShader  = compile ps_2_0 WangPS();
0260:         PixelShader  = compile ps_2_0 MipmapWangPS();   // @@@
0261:     }
0262: }
0263: 
0264: 
0265: 
0266: 
0267: //-----------------------------------------------------------------------------
0268: // Name: WorldVertexShader
0269: // Type: Vertex shader                                      
0270: // Desc: standard transform and lighting.
0271: //-----------------------------------------------------------------------------
0272: VS_OUTPUT SimpleVS( float4 vPos : POSITION, 
0273:                     float3 vNormal : NORMAL,
0274:                     float2 vTexCoord0 : TEXCOORD0 )
0275: {
0276:     VS_OUTPUT Output;
0277:   
0278:     // tranform vertex position into screen space
0279:     Output.Position = mul(vPos, g_mWorldViewProjection );
0280:     
0281:     // Just copy the texture coordinate through
0282:     Output.TextureUV.xy = vTexCoord0;
0283:     
0284:     return Output;    
0285: }
0286: 
0287: 
0288: //-----------------------------------------------------------------------------
0289: // Name: SimplePS
0290: // Type: Pixel shader
0291: // Desc: This shader simply outputs the pixel's color 
0292: //-----------------------------------------------------------------------------
0293: float4 SimplePS( VS_OUTPUT In ) : COLOR
0294: { 
0295:     return tex2D( Sampler, In.TextureUV );
0296: }
0297: 
0298: 
0299: //--------------------------------------------------------------------------------------
0300: // Techniques
0301: //--------------------------------------------------------------------------------------
0302: technique SimpleTechnique
0303: {
0304:     pass P0
0305:     {
0306:         VertexShader = compile vs_1_1 SimpleVS();
0307:         PixelShader  = compile ps_1_1 SimplePS();
0308:     }
0309: }
0310: 
0311: 
0312: 
0313: 
0314: