-
Notifications
You must be signed in to change notification settings - Fork 4
/
FXAA.cpp
92 lines (72 loc) · 2.42 KB
/
FXAA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "FXAA.h"
#include <string>
#include <sstream>
#include <vector>
using namespace std;
#include "Settings.h"
#include "RenderstateManager.h"
FXAA::FXAA(IDirect3DDevice9 *device, int width, int height, Quality quality)
: Effect(device), width(width), height(height) {
// Setup the defines for compiling the effect
vector<D3DXMACRO> defines;
stringstream s;
// Setup pixel size macro
s << "float2(1.0 / " << width << ", 1.0 / " << height << ")";
string pixelSizeText = s.str();
D3DXMACRO pixelSizeMacro = { "PIXEL_SIZE", pixelSizeText.c_str() };
defines.push_back(pixelSizeMacro);
D3DXMACRO qualityMacros[] = {
{ "FXAA_QUALITY__PRESET", "10" },
{ "FXAA_QUALITY__PRESET", "20" },
{ "FXAA_QUALITY__PRESET", "28" },
{ "FXAA_QUALITY__PRESET", "39" }
};
defines.push_back(qualityMacros[(int)quality]);
D3DXMACRO null = { NULL, NULL };
defines.push_back(null);
DWORD flags = D3DXFX_NOT_CLONEABLE | D3DXSHADER_OPTIMIZATION_LEVEL3;
// Load effect from file
SDLOG(0, "FXAA load\n");
ID3DXBuffer* errors;
HRESULT hr = D3DXCreateEffectFromFile(device, GetDirectoryFile("dpfix\\FXAA.fx"), &defines.front(), NULL, flags, NULL, &effect, &errors);
if(hr != D3D_OK) SDLOG(0, "ERRORS:\n %s\n", errors->GetBufferPointer());
// Create buffer
device->CreateTexture(width, height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &buffer1Tex, NULL);
buffer1Tex->GetSurfaceLevel(0, &buffer1Surf);
// get handles
frameTexHandle = effect->GetParameterByName(NULL, "frameTex2D");
}
FXAA::~FXAA() {
SAFERELEASE(effect);
SAFERELEASE(buffer1Surf);
SAFERELEASE(buffer1Tex);
}
void FXAA::go(IDirect3DTexture9 *frame, IDirect3DSurface9 *dst) {
device->SetVertexDeclaration(vertexDeclaration);
lumaPass(frame, buffer1Surf);
fxaaPass(buffer1Tex, dst);
}
void FXAA::lumaPass(IDirect3DTexture9 *frame, IDirect3DSurface9 *dst) {
device->SetRenderTarget(0, dst);
// Setup variables
effect->SetTexture(frameTexHandle, frame);
// Do it!
UINT passes;
effect->Begin(&passes, 0);
effect->BeginPass(0);
quad(width, height);
effect->EndPass();
effect->End();
}
void FXAA::fxaaPass(IDirect3DTexture9 *src, IDirect3DSurface9* dst) {
device->SetRenderTarget(0, dst);
// Setup variables
effect->SetTexture(frameTexHandle, src);
// Do it!
UINT passes;
effect->Begin(&passes, 0);
effect->BeginPass(1);
quad(width, height);
effect->EndPass();
effect->End();
}