-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgfx_cas.lua
265 lines (213 loc) · 6.77 KB
/
gfx_cas.lua
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
if gl.CreateShader == nil then
return
end
function widget:GetInfo()
return {
name = "Contrast Adaptive Sharpen",
desc = "Spring port of AMD FidelityFX' Contrast Adaptive Sharpen (CAS). MODIFIED PEAK VALUE.",
author = "Errrrrrr, martymcmodding, ivand",
layer = 2000,
enabled = true,
}
end
-- Shameless port from https://gist.github.com/martymcmodding/30304c4bffa6e2bd2eb59ff8bb09d135
-----------------------------------------------------------------
-- Constants
-----------------------------------------------------------------
local GL_RGBA8 = 0x8058
local SHARPNESS = 1.0
local version = 1.06
-----------------------------------------------------------------
-- Lua Shortcuts
-----------------------------------------------------------------
local glTexture = gl.Texture
local glBlending = gl.Blending
local glCopyToTexture = gl.CopyToTexture
-----------------------------------------------------------------
-- File path Constants
-----------------------------------------------------------------
local luaShaderDir = "LuaUI/Widgets/Include/"
-----------------------------------------------------------------
-- Shader Sources
-----------------------------------------------------------------
local vsCAS = [[
#version 330
// full screen triangle
const vec2 vertices[3] = vec2[3](
vec2(-1.0, -1.0),
vec2( 3.0, -1.0),
vec2(-1.0, 3.0)
);
void main()
{
gl_Position = vec4(vertices[gl_VertexID], 0.0, 1.0);
}
]]
local fsCAS = [[
#version 330
#line 20058
uniform sampler2D screenCopyTex;
uniform float sharpness;
#if 0
#define TEXEL_FETCH_OFFSET(t, c, l, o) texelFetch(t, c + o, l)
#else
#define TEXEL_FETCH_OFFSET texelFetchOffset
#endif
out vec4 fragColor;
vec3 CASPass(ivec2 tc) {
// fetch a 3x3 neighborhood around the pixel 'e',
// a b c
// d(e)f
// g h i
vec3 a = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2(-1, -1)).rgb;
vec3 b = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2( 0, -1)).rgb;
vec3 c = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2( 1, -1)).rgb;
vec3 d = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2(-1, 0)).rgb;
vec3 e = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2( 0, 0)).rgb;
vec3 f = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2( 1, 0)).rgb;
vec3 g = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2(-1, 1)).rgb;
vec3 h = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2( 0, 1)).rgb;
vec3 i = TEXEL_FETCH_OFFSET(screenCopyTex, tc, 0, ivec2( 1, 1)).rgb;
// Soft min and max.
// a b c b
// d e f * 0.5 + d e f * 0.5
// g h i h
// These are 2.0x bigger (factored out the extra multiply).
vec3 mnRGB = min(min(min(d, e), min(f, b)), h);
vec3 mnRGB2 = min(mnRGB, min(min(a, c), min(g, i)));
mnRGB += mnRGB2;
vec3 mxRGB = max(max(max(d, e), max(f, b)), h);
vec3 mxRGB2 = max(mxRGB, max(max(a, c), max(g, i)));
mxRGB += mxRGB2;
// Smooth minimum distance to signal limit divided by smooth max.
vec3 rcpMRGB = vec3(1.0) / mxRGB;
vec3 ampRGB = clamp(min(mnRGB, 2.0 - mxRGB) * rcpMRGB, vec3(0.0), vec3(1.0));
// Shaping amount of sharpening.
ampRGB = inversesqrt(ampRGB);
// sharpness is rescaled from 0.75-1.1 to 0-1
float peak = -3.0 * ((sharpness - 0.75) / (1.1 - 0.75)) + 8.0;
vec3 wRGB = vec3(-1.0) / (ampRGB * peak);
vec3 rcpWeightRGB = vec3(1.0) / (1.0 + 4.0 * wRGB);
// 0 w 0
// Filter shape: w 1 w
// 0 w 0
vec3 window = (b + d) + (f + h);
vec3 outColor = clamp((window * wRGB + e) * rcpWeightRGB, vec3(0.0), vec3(1.0));
// Linear interpolation of outColor using the rescaled sharpness value
// Turn this on if you want to experiment with sharpness more
// replace ((sharpness - 0.75) / (1.1 - 0.75)) with a value between 0 and 1
//outColor = e + ((sharpness - 0.75) / (1.1 - 0.75)) * (outColor - e);
return outColor;
}
void main() {
fragColor = vec4(CASPass(ivec2(gl_FragCoord.xy)), 1.0);
//fragColor = vec4(1.0, 0.0, 0.0, 0.5);
}
]]
-----------------------------------------------------------------
-- Global Variables
-----------------------------------------------------------------
local LuaShader = VFS.Include(luaShaderDir.."LuaShader.lua")
local vsx, vsy, vpx, vpy
local screenCopyTex
local casShader
local fullTexQuad
-----------------------------------------------------------------
-- Local Functions
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Widget Functions
-----------------------------------------------------------------
local function UpdateShader()
casShader:ActivateWith(function()
casShader:SetUniform("sharpness", SHARPNESS)
end)
end
function widget:Initialize()
if gl.CreateShader == nil then
Spring.Echo("CAS: createshader not supported, removing")
widgetHandler:RemoveWidget()
return
end
vsx, vsy, vpx, vpy = Spring.GetViewGeometry()
local commonTexOpts = {
target = GL_TEXTURE_2D,
border = false,
min_filter = GL.NEAREST,
mag_filter = GL.NEAREST,
wrap_s = GL.CLAMP_TO_EDGE,
wrap_t = GL.CLAMP_TO_EDGE,
}
commonTexOpts.format = GL_RGBA8
--screenCopyTex = gl.CreateTexture(vsx, vsy, commonTexOpts)
casShader = LuaShader({
vertex = vsCAS,
fragment = fsCAS,
uniformInt = {
screenCopyTex = 0,
},
}, ": Contrast Adaptive Sharpen")
local shaderCompiled = casShader:Initialize()
if not shaderCompiled then
Spring.Echo("Failed to compile Contrast Adaptive Sharpen shader, removing widget")
widgetHandler:RemoveWidget()
return
end
UpdateShader()
fullTexQuad = gl.GetVAO()
if fullTexQuad == nil then
widgetHandler:RemoveWidget() --no fallback for potatoes
return
end
WG.cas = {}
WG.cas.setSharpness = function(value)
SHARPNESS = value
UpdateShader()
end
WG.cas.getSharpness = function(value)
return SHARPNESS
end
end
function widget:Shutdown()
--gl.DeleteTexture(screenCopyTex)
if casShader then
casShader:Finalize()
end
if fullTexQuad then
fullTexQuad:Delete()
end
end
function widget:ViewResize()
widget:Shutdown()
widget:Initialize()
end
function widget:DrawScreenEffects()
--glCopyToTexture(screenCopyTex, 0, 0, vpx, vpy, vsx, vsy)
if WG['screencopymanager'] and WG['screencopymanager'].GetScreenCopy then
screenCopyTex = WG['screencopymanager'].GetScreenCopy()
else
--glCopyToTexture(screenCopyTex, 0, 0, vpx, vpy, vsx, vsy)
Spring.Echo("Missing Screencopy Manager, exiting", WG['screencopymanager'] )
widgetHandler:RemoveWidget()
return false
end
if screenCopyTex == nil then return end
glTexture(0, screenCopyTex)
glBlending(false)
casShader:Activate()
fullTexQuad:DrawArrays(GL.TRIANGLES, 3)
casShader:Deactivate()
glBlending(true)
glTexture(0, false)
end
function widget:GetConfigData(data)
return {
version = version,
SHARPNESS = SHARPNESS
}
end
function widget:SetConfigData(data)
if data.SHARPNESS ~= nil and data.version ~= nil and data.version == version then
SHARPNESS = data.SHARPNESS
end
end