0001: //--------------------------------------------------------------------------------------
0002: // File: main.fx
0003: //
0004: // The effect file for the main sample.  
0005: // 
0006: // Copyright (c) 2004 IMAGIRE Takashi. All rights reserved.
0007: //--------------------------------------------------------------------------------------
0008: 
0009: 
0010: //--------------------------------------------------------------------------------------
0011: // Global variables
0012: //--------------------------------------------------------------------------------------
0013: 
0014: float    g_fInvTexSize;
0015: 
0016: //-----------------------------------------------------------------------------
0017: // Texture samplers
0018: //-----------------------------------------------------------------------------
0019: texture Texture;
0020: sampler Sampler = sampler_state
0021: {
0022:     Texture = <Texture>;
0023:     MinFilter = POINT;
0024:     MagFilter = POINT;
0025:     MipFilter = NONE;
0026: 
0027:     AddressU = Clamp;
0028:     AddressV = Clamp;
0029: };
0030: //-----------------------------------------------------------------------------
0031: texture TextureInfo;
0032: sampler SamplerInfo = sampler_state
0033: {
0034:     Texture = <TextureInfo>;
0035:     MinFilter = POINT;
0036:     MagFilter = POINT;
0037:     MipFilter = NONE;
0038: 
0039:     AddressU = Clamp;
0040:     AddressV = Clamp;
0041: };
0042: 
0043: 
0044: //=============================================================================
0045: // 対数をとる
0046: //=============================================================================
0047: //-----------------------------------------------------------------------------
0048: // Vertex shader output structure
0049: //-----------------------------------------------------------------------------
0050: struct LogVS_OUTPUT
0051: {
0052:     float4 Position : POSITION;
0053:     float2 TexCoord0 : TEXCOORD0;
0054:     float2 TexCoord1 : TEXCOORD1;
0055:     float2 TexCoord2 : TEXCOORD2;
0056:     float2 TexCoord3 : TEXCOORD3;
0057: };
0058: 
0059: 
0060: //-----------------------------------------------------------------------------
0061: // Name: LogVS
0062: //-----------------------------------------------------------------------------
0063: LogVS_OUTPUT LogVS( float4 vPos : POSITION, 
0064:                     float2 vTexCoord0 : TEXCOORD0 )
0065: {
0066:     LogVS_OUTPUT Output;
0067:   
0068:     // tranform vertex position into screen space
0069:     Output.Position = vPos;
0070:     
0071:     // Just copy the texture coordinate through
0072:     Output.TexCoord0 = vTexCoord0 + float2( 0.5*g_fInvTexSize, 0.5*g_fInvTexSize );
0073:     Output.TexCoord1 = vTexCoord0 + float2( 1.5*g_fInvTexSize, 0.5*g_fInvTexSize );
0074:     Output.TexCoord2 = vTexCoord0 + float2( 0.5*g_fInvTexSize, 1.5*g_fInvTexSize );
0075:     Output.TexCoord3 = vTexCoord0 + float2( 1.5*g_fInvTexSize, 1.5*g_fInvTexSize );
0076:     
0077:     return Output;    
0078: }
0079: 
0080: 
0081: //-----------------------------------------------------------------------------
0082: // Name: LogPS
0083: //-----------------------------------------------------------------------------
0084: float4 LogPS( LogVS_OUTPUT In ) : COLOR
0085: {
0086:     const float3 RGB2Y   = float3( +0.29900f, +0.58700f, +0.11400f );
0087:     const float EPSILON = 0.00001;
0088:     float4 output;
0089:     
0090:     float l0 = dot( RGB2Y, tex2D( Sampler, In.TexCoord0 ));
0091:     float l1 = dot( RGB2Y, tex2D( Sampler, In.TexCoord1 ));
0092:     float l2 = dot( RGB2Y, tex2D( Sampler, In.TexCoord2 ));
0093:     float l3 = dot( RGB2Y, tex2D( Sampler, In.TexCoord3 ));
0094:     
0095:     float l_max = max(max(l0,l1),max(l2,l3));
0096:     
0097:     float total =  log( EPSILON + l0 )
0098:                  + log( EPSILON + l1 )
0099:                  + log( EPSILON + l2 )
0100:                  + log( EPSILON + l3 );
0101:                  
0102:     output.r = l_max;
0103:     output.gba = 0.25f * total;
0104:     
0105:     return output;
0106: }
0107: 
0108: 
0109: //--------------------------------------------------------------------------------------
0110: // Techniques
0111: //--------------------------------------------------------------------------------------
0112: technique LogTechnique
0113: {
0114:     pass P0
0115:     {
0116:         VertexShader = compile vs_1_1 LogVS();
0117:         PixelShader  = compile ps_2_0 LogPS();
0118:     }
0119: }
0120: 
0121: 
0122: 
0123: 
0124: 
0125: //=============================================================================
0126: // 縮小する
0127: //=============================================================================
0128: //-----------------------------------------------------------------------------
0129: // Vertex shader output structure
0130: //-----------------------------------------------------------------------------
0131: struct SmallVS_OUTPUT
0132: {
0133:     float4 Position : POSITION;
0134:     float2 TexCoord0 : TEXCOORD0;
0135:     float2 TexCoord1 : TEXCOORD1;
0136:     float2 TexCoord2 : TEXCOORD2;
0137:     float2 TexCoord3 : TEXCOORD3;
0138:     float2 TexCoord4 : TEXCOORD4;
0139:     float2 TexCoord5 : TEXCOORD5;
0140:     float2 TexCoord6 : TEXCOORD6;
0141:     float2 TexCoord7 : TEXCOORD7;
0142: };
0143: 
0144: 
0145: //-----------------------------------------------------------------------------
0146: // Name: LogVS
0147: //-----------------------------------------------------------------------------
0148: SmallVS_OUTPUT SmallVS( float4 vPos : POSITION, 
0149:                     float2 vTexCoord0 : TEXCOORD0 )
0150: {
0151:     SmallVS_OUTPUT Output;
0152:   
0153:     // tranform vertex position into screen space
0154:     Output.Position = vPos;
0155:     
0156:     // Just copy the texture coordinate through
0157:     Output.TexCoord0 = vTexCoord0 + float2( 0.5*g_fInvTexSize, 0.5*g_fInvTexSize );
0158:     Output.TexCoord1 = vTexCoord0 + float2( 1.5*g_fInvTexSize, 0.5*g_fInvTexSize );
0159:     Output.TexCoord2 = vTexCoord0 + float2( 3.5*g_fInvTexSize, 0.5*g_fInvTexSize );
0160:     Output.TexCoord3 = vTexCoord0 + float2( 5.5*g_fInvTexSize, 0.5*g_fInvTexSize );
0161:     Output.TexCoord4 = vTexCoord0 + float2( 0.5*g_fInvTexSize, 1.5*g_fInvTexSize );
0162:     Output.TexCoord5 = vTexCoord0 + float2( 1.5*g_fInvTexSize, 1.5*g_fInvTexSize );
0163:     Output.TexCoord6 = vTexCoord0 + float2( 3.5*g_fInvTexSize, 1.5*g_fInvTexSize );
0164:     Output.TexCoord7 = vTexCoord0 + float2( 5.5*g_fInvTexSize, 1.5*g_fInvTexSize );
0165:     
0166:     return Output;    
0167: }
0168: 
0169: 
0170: //-----------------------------------------------------------------------------
0171: // Name: SmallPS
0172: //-----------------------------------------------------------------------------
0173: float4 SmallPS( SmallVS_OUTPUT In ) : COLOR
0174: {
0175:     float4 output;
0176:     
0177:     float4 t0 = tex2D( Sampler, In.TexCoord0 );
0178:     float4 t1 = tex2D( Sampler, In.TexCoord1 );
0179:     float4 t2 = tex2D( Sampler, In.TexCoord2 );
0180:     float4 t3 = tex2D( Sampler, In.TexCoord3 );
0181:     float4 t4 = tex2D( Sampler, In.TexCoord4 );
0182:     float4 t5 = tex2D( Sampler, In.TexCoord5 );
0183:     float4 t6 = tex2D( Sampler, In.TexCoord6 );
0184:     float4 t7 = tex2D( Sampler, In.TexCoord7 );
0185:     float4 t8 = tex2D( Sampler, In.TexCoord0 + float2(0,2.0*g_fInvTexSize) );
0186:     float4 t9 = tex2D( Sampler, In.TexCoord1 + float2(0,2.0*g_fInvTexSize) );
0187:     float4 ta = tex2D( Sampler, In.TexCoord2 + float2(0,2.0*g_fInvTexSize) );
0188:     float4 tb = tex2D( Sampler, In.TexCoord3 + float2(0,2.0*g_fInvTexSize) );
0189:     float4 tc = tex2D( Sampler, In.TexCoord4 + float2(0,2.0*g_fInvTexSize) );
0190:     float4 td = tex2D( Sampler, In.TexCoord5 + float2(0,2.0*g_fInvTexSize) );
0191:     float4 te = tex2D( Sampler, In.TexCoord6 + float2(0,2.0*g_fInvTexSize) );
0192:     float4 tf = tex2D( Sampler, In.TexCoord7 + float2(0,2.0*g_fInvTexSize) );
0193:     
0194:     float l_max = max(max(max(max(t0.r,t1.r),max(t2.r,t3.r)),
0195:                           max(max(t4.r,t5.r),max(t6.r,t7.r))),
0196:                       max(max(max(t8.r,t9.r),max(ta.r,tb.r)),
0197:                           max(max(tc.r,td.r),max(te.r,tf.r))));
0198:     
0199:     output.r = l_max;
0200:     output.gba = (1.0/16.0)* (t0.gba + t1.gba + t2.gba + t3.gba
0201:                             + t4.gba + t5.gba + t6.gba + t7.gba
0202:                             + t8.gba + t9.gba + ta.gba + tb.gba
0203:                             + tc.gba + td.gba + te.gba + tf.gba);
0204:     
0205:     return output;
0206: }
0207: 
0208: 
0209: //--------------------------------------------------------------------------------------
0210: // Techniques
0211: //--------------------------------------------------------------------------------------
0212: technique SmallTechnique
0213: {
0214:     pass P0
0215:     {
0216:         VertexShader = compile vs_1_1 SmallVS();
0217:         PixelShader  = compile ps_2_0 SmallPS();
0218:     }
0219: }
0220: 
0221: 
0222: 
0223: 
0224: 
0225: //-----------------------------------------------------------------------------
0226: // Vertex shader output structure
0227: //-----------------------------------------------------------------------------
0228: struct FinalVS_OUTPUT
0229: {
0230:     float4 Position : POSITION;
0231:     float2 TexCoord : TEXCOORD0;
0232: };
0233: 
0234: 
0235: //-----------------------------------------------------------------------------
0236: // Name: FinalVS
0237: //-----------------------------------------------------------------------------
0238: FinalVS_OUTPUT FinalVS( float4 vPos : POSITION, 
0239:                     float2 vTexCoord0 : TEXCOORD0 )
0240: {
0241:     FinalVS_OUTPUT Output;
0242:   
0243:     // tranform vertex position into screen space
0244:     Output.Position = vPos - float4( 0.5*g_fInvTexSize, 0.5*g_fInvTexSize, 0, 0 );
0245:     
0246:     // Just copy the texture coordinate through
0247:     Output.TexCoord = vTexCoord0;
0248:     
0249:     return Output;    
0250: }
0251: 
0252: 
0253: //-----------------------------------------------------------------------------
0254: // Name: FinalPS
0255: //-----------------------------------------------------------------------------
0256: float4 FinalPS( FinalVS_OUTPUT In ) : COLOR
0257: {
0258:     const float3 RGB2Y   = float3( +0.29900f, +0.58700f, +0.11400f );
0259:     const float3 RGB2Cb  = float3( -0.16874f, -0.33126f, +0.50000f );
0260:     const float3 RGB2Cr  = float3( +0.50000f, -0.41869f, -0.08131f );
0261:     const float3 YCbCr2R = float3( +1.00000f, +0.00000f, +1.40200f );
0262:     const float3 YCbCr2G = float3( +1.00000f, -0.34414f, -0.71414f );
0263:     const float3 YCbCr2B = float3( +1.00000f, +1.77200f, +0.00000f );
0264:     
0265:     float4 info  = tex2D( SamplerInfo, float2(0.5,0.5) );
0266:     float3 texel = tex2D( Sampler, In.TexCoord ).rgb;
0267:     
0268:     float coeff = 0.18 * exp( -info.g );
0269:     float l_max = coeff * info.r;
0270: 
0271:     // YCbCr系に変換
0272:     float3 YCbCr;
0273:     YCbCr.y = dot( RGB2Cb, texel );
0274:     YCbCr.z = dot( RGB2Cr, texel );
0275: 
0276:     // 色の強さは補正
0277:     float lum = coeff * dot( RGB2Y,  texel );
0278:     YCbCr.x = lum * (1.0f+lum/(l_max*l_max)) / (1.0f+lum);
0279:     
0280:     // RGB系にして出力
0281:     float4 color;
0282:     color.r = dot( YCbCr2R,  YCbCr );
0283:     color.g = dot( YCbCr2G,  YCbCr );
0284:     color.b = dot( YCbCr2B,  YCbCr );
0285:     color.a = 0;
0286:     
0287:     return color;
0288: }
0289: 
0290: 
0291: //--------------------------------------------------------------------------------------
0292: // Techniques
0293: //--------------------------------------------------------------------------------------
0294: technique FinalTechnique
0295: {
0296:     pass P0
0297:     {
0298:         VertexShader = compile vs_1_1 FinalVS();
0299:         PixelShader  = compile ps_2_0 FinalPS();
0300:     }
0301: }
0302: 
0303: 
0304: 
0305: 
0306: 
0307: