首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在XNA4.0 winforms中使用Nvidia (着色器抗混叠)的例子?

在XNA4.0 winforms中使用Nvidia (着色器抗混叠)的例子?
EN

Stack Overflow用户
提问于 2011-10-06 20:28:02
回答 2查看 6.9K关注 0票数 6

我正在编写的一个应用程序中使用这个示例项目的XNA4.0表单控件:http://creators.xna.com/en-US/sample/winforms_series1

如果您不熟悉FXAA,请查看创建者的站点:http://timothylottes.blogspot.com/2011/03/nvidia-fxaa.html

到目前为止,我已经花了相当长的时间试图弄清楚如何使用它而没有任何运气(到目前为止.)。的确,我对图形编程一点经验都没有,但我目前确实有一个运行良好的应用程序,只是在锯齿状的线条上看起来真的很糟糕。我知道AA的内置方法,但对我和我的笔记本电脑不起作用。所以我的要求是使用FXAA而不是内置的方法。

此时:我的内容项目中有FXAA 3.11头文件。我有一个由visual studio生成的通用FX文件,其中包含以下几项内容:

代码语言:javascript
复制
#define FXAA_PC_CONSOLE 1
#define FXAA_HLSL_5 1
#define FXAA_QUALITY__PRESET 12
#include "Includes/Fxaa3_11.h"

我只是在这里询问是否有人可以提供一些XNA4.0示例,特别是使用自定义的windows窗体方法。

我很感激有人能提供的任何帮助。

编辑3: --自从我发布了这条消息以来,我一直在努力研究如何使FXAA工作。我发现了这个:http://www.gamedev.net/topic/609638-fxaa-help/page__st__20http://fxaa-pp-inject.assembla.me/trunk/DirectX9/shader.fx

我从FXAA到裸骨FXAA_PC_CONSOLE类型,并进行了编译。我只需要计算出参数fxaaConsolePosPos,它是每个像素的左上角和右下角位置。无论如何,看起来FXAA_PC_CONSOLE可能与着色器型号2.0一起工作,我需要使用REACH和基于winforms的XNA。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-12 05:16:33

因此,我想出了办法,至少使用了为控制台和低端个人电脑设计的较小版本的FXAA。我不能保证我的参数对着色器代码是正确的,但我确实看到了一个明显的差别,当它运行。

下面是使用我的切碎着色器和部分C# XNA4.0代码的完整解决方案:

着色器代码(放在内容子项目中的.fx文件中):注意,我用tex2D替换了tex2Dlod,因为有人建议SM2.0不支持第一个类型的

代码语言:javascript
复制
#define FxaaBool bool
#define FxaaDiscard clip(-1)
#define FxaaFloat float
#define FxaaFloat2 float2
#define FxaaFloat3 float3
#define FxaaFloat4 float4
#define FxaaHalf half
#define FxaaHalf2 half2
#define FxaaHalf3 half3
#define FxaaHalf4 half4
#define FxaaSat(x) saturate(x)

#define FxaaInt2 float2
#define FxaaTex sampler2D
#define FxaaTexTop(t, p) tex2D(t, float4(p, 0.0, 0.0))
#define FxaaTexOff(t, p, o, r) tex2D(t, float4(p + (o * r), 0, 0))

FxaaFloat FxaaLuma(FxaaFloat4 rgba) { 
    rgba.w = dot(rgba.rgb, FxaaFloat3(0.299, 0.587, 0.114));
return  rgba.w; }

/*============================================================================
                         FXAA3 CONSOLE - PC VERSION
============================================================================*/
FxaaFloat4 FxaaPixelShader(
    FxaaFloat2 pos,
    FxaaFloat4 fxaaConsolePosPos,
    FxaaTex tex,
    FxaaFloat4 fxaaConsoleRcpFrameOpt,
    FxaaFloat4 fxaaConsoleRcpFrameOpt2,
    FxaaFloat fxaaConsoleEdgeSharpness,
    FxaaFloat fxaaConsoleEdgeThreshold,
    FxaaFloat fxaaConsoleEdgeThresholdMin) {
    FxaaFloat lumaNw = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.xy));
    FxaaFloat lumaSw = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.xw));
    FxaaFloat lumaNe = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.zy));
    FxaaFloat lumaSe = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.zw));
    FxaaFloat4 rgbyM = FxaaTexTop(tex, pos.xy);
    #if (FXAA_GREEN_AS_LUMA == 0)
        FxaaFloat lumaM = rgbyM.w;
    #else
        FxaaFloat lumaM = rgbyM.y;
    #endif
    FxaaFloat lumaMaxNwSw = max(lumaNw, lumaSw);
    lumaNe += 1.0/384.0;
    FxaaFloat lumaMinNwSw = min(lumaNw, lumaSw);
    FxaaFloat lumaMaxNeSe = max(lumaNe, lumaSe);
    FxaaFloat lumaMinNeSe = min(lumaNe, lumaSe);
    FxaaFloat lumaMax = max(lumaMaxNeSe, lumaMaxNwSw);
    FxaaFloat lumaMin = min(lumaMinNeSe, lumaMinNwSw);
    FxaaFloat lumaMaxScaled = lumaMax * fxaaConsoleEdgeThreshold;
    FxaaFloat lumaMinM = min(lumaMin, lumaM);
    FxaaFloat lumaMaxScaledClamped = max(fxaaConsoleEdgeThresholdMin, lumaMaxScaled);
    FxaaFloat lumaMaxM = max(lumaMax, lumaM);
    FxaaFloat dirSwMinusNe = lumaSw - lumaNe;
    FxaaFloat lumaMaxSubMinM = lumaMaxM - lumaMinM;
    FxaaFloat dirSeMinusNw = lumaSe - lumaNw;
    if(lumaMaxSubMinM < lumaMaxScaledClamped) return rgbyM;
    FxaaFloat2 dir;
    dir.x = dirSwMinusNe + dirSeMinusNw;
    dir.y = dirSwMinusNe - dirSeMinusNw;
    FxaaFloat2 dir1 = normalize(dir.xy);
    FxaaFloat4 rgbyN1 = FxaaTexTop(tex, pos.xy - dir1 * fxaaConsoleRcpFrameOpt.zw);
    FxaaFloat4 rgbyP1 = FxaaTexTop(tex, pos.xy + dir1 * fxaaConsoleRcpFrameOpt.zw);
    FxaaFloat dirAbsMinTimesC = min(abs(dir1.x), abs(dir1.y)) * fxaaConsoleEdgeSharpness;
    FxaaFloat2 dir2 = clamp(dir1.xy / dirAbsMinTimesC, -2.0, 2.0);
    FxaaFloat4 rgbyN2 = FxaaTexTop(tex, pos.xy - dir2 * fxaaConsoleRcpFrameOpt2.zw);
    FxaaFloat4 rgbyP2 = FxaaTexTop(tex, pos.xy + dir2 * fxaaConsoleRcpFrameOpt2.zw);
    FxaaFloat4 rgbyA = rgbyN1 + rgbyP1;
    FxaaFloat4 rgbyB = ((rgbyN2 + rgbyP2) * 0.25) + (rgbyA * 0.25);
    #if (FXAA_GREEN_AS_LUMA == 0)
        FxaaBool twoTap = (rgbyB.w < lumaMin) || (rgbyB.w > lumaMax);
    #else
        FxaaBool twoTap = (rgbyB.y < lumaMin) || (rgbyB.y > lumaMax);
    #endif
    if(twoTap) rgbyB.xyz = rgbyA.xyz * 0.5;
    return rgbyB; 
}
/*==========================================================================*/

uniform extern float SCREEN_WIDTH;
uniform extern float SCREEN_HEIGHT;
uniform extern texture gScreenTexture;

sampler screenSampler = sampler_state
{
    Texture = <gScreenTexture>;
    /*MinFilter = LINEAR;
    MagFilter = LINEAR;
    MipFilter = LINEAR;
    AddressU = Clamp;
    AddressV = Clamp;*/
};

float4 PixelShaderFunction(float2 tc : TEXCOORD0) : COLOR0
{
    float pixelWidth = (1 / SCREEN_WIDTH);
    float pixelHeight = (1 / SCREEN_HEIGHT);

    float2 pixelCenter = float2(tc.x - pixelWidth, tc.y - pixelHeight);
    float4 fxaaConsolePosPos = float4(tc.x, tc.y, tc.x + pixelWidth, tc.y + pixelHeight);

    return FxaaPixelShader(
        pixelCenter,
        fxaaConsolePosPos,
        screenSampler,
        float4(-0.50 / SCREEN_WIDTH, -0.50 / SCREEN_HEIGHT, 0.50 / SCREEN_WIDTH, 0.50 / SCREEN_HEIGHT),
        float4(-2.0 / SCREEN_WIDTH, -2.0 / SCREEN_HEIGHT, 2.0 / SCREEN_WIDTH, 2.0 / SCREEN_HEIGHT),
        8.0,
        0.125,
        0.05);
}

technique ppfxaa
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

下面是应用着色器的C-夏普代码片段:

代码语言:javascript
复制
//..........................................
//these objects are used in managing the FXAA operation

//FXAA objects (anti-aliasing)
RenderTarget2D renderTarget;
SpriteBatch spriteBatch;
Effect fxaaAntialiasing;

//..........................................
//initialize the render target and set effect parameters

//code to handle a final antialiasing using a pixel shader
renderTarget = new RenderTarget2D(
    GraphicsDevice,
    GraphicsDevice.PresentationParameters.BackBufferWidth,
    GraphicsDevice.PresentationParameters.BackBufferHeight,
    false,
    GraphicsDevice.PresentationParameters.BackBufferFormat,
    DepthFormat.Depth24);

spriteBatch = new SpriteBatch(GraphicsDevice);
fxaaAntialiasing = content.Load<Effect>("sfxaa");
fxaaAntialiasing.CurrentTechnique = fxaaAntialiasing.Techniques["ppfxaa"];
fxaaAntialiasing.Parameters["SCREEN_WIDTH"].SetValue(renderTarget.Width);
fxaaAntialiasing.Parameters["SCREEN_HEIGHT"].SetValue(renderTarget.Height);
fxaaAntialiasing.Parameters["gScreenTexture"].SetValue(renderTarget as Texture2D);


//..........................................
//this should happen in your Draw() method

//change to our offscreen render target
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.CornflowerBlue);

//
//draw all of your models and such here...
//

GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);

//this where the shader antialiasing happens to the frame we just filled with content
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
    SamplerState.LinearClamp, DepthStencilState.Default, 
    RasterizerState.CullNone, fxaaAntialiasing);

//draw the buffer we made to the screen
spriteBatch.Draw(renderTarget as Texture2D, 
    new Rectangle(0, 0, renderTarget.Width, renderTarget.Height), 
    Color.White);

spriteBatch.End();
票数 6
EN

Stack Overflow用户

发布于 2013-08-24 16:06:45

我最近在我的游戏中实现了这个技术,但由于某种原因,我的优势仍然“锯齿状”。有没有办法在某个地方调整代码,使锯齿状的边缘变软?谢谢。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7680073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档