FFmpeg
vsrc_gfxcapture_shader.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVFILTER_VSRC_GFXCAPTURE_SHADER_H
20 #define AVFILTER_VSRC_GFXCAPTURE_SHADER_H
21 
22 #define HLSL_SHADER(shader) #shader
23 
24 static const char render_shader_src[] = HLSL_SHADER(
25  struct VSOut {
26  float4 pos : SV_Position;
27  float2 uv : TEXCOORD0;
28  };
29 
30  cbuffer cb : register(b0) {
31  float2 tS;
32  float2 dS;
33  float2 uvMin;
34  float2 uvMax;
35  uint to_unpremult;
36  uint to_srgb;
37  uint2 pad;
38  };
39 
40  Texture2D t0 : register(t0);
41  SamplerState s0 : register(s0);
42 
43  VSOut main_vs(uint id : SV_VertexID) {
44  VSOut o;
45  o.pos = float4(id == 2 ? 3.0 : -1.0, id == 1 ? 3.0 : -1.0, 0, 1);
46  o.uv = lerp(uvMin, uvMax, float2((o.pos.x + 1) * 0.5, 1 - (o.pos.y + 1) * 0.5));
47  return o;
48  }
49 
50  float4 cubic(float v) {
51  float4 n = float4(1.0, 2.0, 3.0, 4.0) - v;
52  float4 s = n * n * n;
53  float x = s.x;
54  float y = s.y - 4.0 * s.x;
55  float z = s.z - 4.0 * s.y + 6.0 * s.x;
56  float w = 6.0 - x - y - z;
57  return float4(x, y, z, w) * (1.0 / 6.0);
58  }
59 
60  float4 texBicubic(Texture2D t, SamplerState ss, float2 uv) {
61  float2 itS = 1.0 / tS;
62 
63  float2 tc = uv * tS - 0.5;
64  float2 fxy = frac(tc);
65  tc -= fxy;
66 
67  float4 xc = cubic(fxy.x);
68  float4 yc = cubic(fxy.y);
69 
70  float4 s = float4(xc.xz + xc.yw, yc.xz + yc.yw);
71  float4 o = tc.xxyy + (float2(-0.5, 1.5)).xyxy + float4(xc.yw, yc.yw) / s;
72  o *= itS.xxyy;
73 
74  float4 s0 = t.Sample(ss, o.xz);
75  float4 s1 = t.Sample(ss, o.yz);
76  float4 s2 = t.Sample(ss, o.xw);
77  float4 s3 = t.Sample(ss, o.yw);
78 
79  float sx = s.x / (s.x + s.y);
80  float sy = s.z / (s.z + s.w);
81 
82  return lerp(lerp(s3, s2, sx), lerp(s1, s0, sx), sy);
83  }
84 
85  float4 unpremultiply(float4 c) {
86  if (c.a < 1e-6)
87  return float4(0.0, 0.0, 0.0, 0.0);
88  return float4(c.rgb / c.a, c.a);
89  }
90 
91  float4 premultiply(float4 c) {
92  return float4(c.rgb * c.a, c.a);
93  }
94 
95  float3 linear_to_srgb(float3 c) {
96  c = max(c, 0.0);
97  float3 lo = 12.92 * c;
98  float3 hi = 1.055 * pow(c, 1.0 / 2.4) - 0.055;
99  return saturate(lerp(hi, lo, step(c, 0.0031308)));
100  }
101 
102  float4 fix_color(float4 c) {
103  if (to_unpremult || to_srgb)
104  c = unpremultiply(c);
105  if (to_srgb) {
106  c.rgb = linear_to_srgb(c.rgb);
107  if (!to_unpremult)
108  c = premultiply(c);
109  }
110  return c;
111  }
112 
113  float4 main_ps(VSOut i) : SV_Target {
114  return fix_color(t0.Sample(s0, i.uv));
115  }
116 
117  float4 main_ps_bicubic(VSOut i) : SV_Target {
118  if (all(tS == dS))
119  return main_ps(i);
120  return fix_color(texBicubic(t0, s0, i.uv));
121  }
122 );
123 
124 #undef HLSL_SHADER
125 
126 #endif /* AVFILTER_VSRC_GFXCAPTURE_SHADER_H */
float4
float4
Definition: cuda_runtime.h:87
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:247
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
w
uint8_t w
Definition: llviddspenc.c:38
max
#define max(a, b)
Definition: cuda_runtime.h:33
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:202
render_shader_src
static const char render_shader_src[]
Definition: vsrc_gfxcapture_shader.h:24
HLSL_SHADER
#define HLSL_SHADER(shader)
Definition: vsrc_gfxcapture_shader.h:22
s
#define s(width, name)
Definition: cbs_vp9.c:198
saturate
static float saturate(float input)
Definition: vf_colortemperature.c:51
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
lerp
static double lerp(double a, double b, double x)
Definition: perlin.c:75
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
pos
unsigned int pos
Definition: spdifenc.c:414
float2
float2
Definition: cuda_runtime.h:55
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:2033