-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblur.cpp
101 lines (79 loc) · 3.24 KB
/
blur.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
93
94
95
96
97
98
99
100
101
#include "blur.hpp"
#include "blur_x.h"
#include "blur_y.h"
static IDirect3DSurface9* rtBackup = nullptr;
static IDirect3DPixelShader9* blurShaderX = nullptr;
static IDirect3DPixelShader9* blurShaderY = nullptr;
static IDirect3DTexture9* blurTexture = nullptr;
static int backbufferWidth = 0;
static int backbufferHeight = 0;
static void BeginBlur(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
const auto device = reinterpret_cast<IDirect3DDevice9*>(cmd->UserCallbackData);
if (!blurShaderX)
{
device->CreatePixelShader(reinterpret_cast<const DWORD*>(blur_x.data()), &blurShaderX);
}
if (!blurShaderY)
{
device->CreatePixelShader(reinterpret_cast<const DWORD*>(blur_y.data()), &blurShaderY);
}
IDirect3DSurface9* backBuffer;
device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
D3DSURFACE_DESC desc;
backBuffer->GetDesc(&desc);
if (backbufferWidth != desc.Width || backbufferHeight != desc.Height)
{
if (blurTexture)
blurTexture->Release();
backbufferWidth = desc.Width;
backbufferHeight = desc.Height;
device->CreateTexture(desc.Width, desc.Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &blurTexture, nullptr);
}
device->GetRenderTarget(0, &rtBackup);
{
IDirect3DSurface9* surface;
blurTexture->GetSurfaceLevel(0, &surface);
device->StretchRect(backBuffer, NULL, surface, NULL, D3DTEXF_NONE);
device->SetRenderTarget(0, surface);
surface->Release();
}
backBuffer->Release();
device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
}
static void FirstBlurPass(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
const auto device = reinterpret_cast<IDirect3DDevice9*>(cmd->UserCallbackData);
device->SetPixelShader(blurShaderX);
const float params[4] = { 1.0f / backbufferWidth };
device->SetPixelShaderConstantF(0, params, 1);
}
static void SecondBlurPass(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
const auto device = reinterpret_cast<IDirect3DDevice9*>(cmd->UserCallbackData);
device->SetPixelShader(blurShaderY);
const float params[4] = { 1.0f / backbufferHeight };
device->SetPixelShaderConstantF(0, params, 1);
}
static void EndBlur(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
const auto device = reinterpret_cast<IDirect3DDevice9*>(cmd->UserCallbackData);
device->SetRenderTarget(0, rtBackup);
rtBackup->Release();
device->SetPixelShader(nullptr);
device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
}
inline void DrawBackgroundBlur(ImDrawList* drawList)
{
drawList->AddCallback(BeginBlur, BlurData::device);
for (int i = 0; i < 8; ++i) {
drawList->AddCallback(FirstBlurPass, BlurData::device );
drawList->AddImage(blurTexture, { 0.0f, 0.0f }, { backbufferWidth * 1.0f, backbufferHeight * 1.0f });
drawList->AddCallback(SecondBlurPass, BlurData::device );
drawList->AddImage(blurTexture, { 0.0f, 0.0f }, { backbufferWidth * 1.0f, backbufferHeight * 1.0f });
}
drawList->AddCallback(EndBlur, BlurData::device );
drawList->AddImageRounded(blurTexture, { 0.0f, 0.0f }, { backbufferWidth * 1.0f, backbufferHeight * 1.0f }, { 0.0f, 0.0f }, { 1.0f, 1.0f }, IM_COL32(255, 255, 255, 255), 7.f);
}