VR-Vantage 3.1 API Documentation
Home
Modules
Namespaces
Classes
Files
Libraries
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
sensorFx
include
cgconvolve.h
Go to the documentation of this file.
1
2
// Copyright JRM Enterprises, Inc. 2006
3
// All rights reserved.
4
//
5
// This code is the intellectual property of JRM Enterprises, Inc.
6
// It may not be used or released as source or compiled binary form
7
// without the prior written consent of JRM Enterprises, Inc.
8
//
9
// Original Authors: Ken George
11
12
const
char
*
cgconvolve
=
13
" \n"
14
" \n"
15
"#ifndef NVI_TYPES \n"
16
"#define NVI_TYPES \n"
17
" \n"
18
"#ifdef USE_HALF \n"
19
"typedef half4 color4; \n"
20
"typedef half3 color3; \n"
21
"typedef half2 color2; \n"
22
"typedef half color; \n"
23
"#define texRECT h4texRECT \n"
24
"#define tex2D h4tex2D \n"
25
"#else \n"
26
" \n"
27
"typedef float4 color4; \n"
28
"typedef float3 color3; \n"
29
"typedef float2 color2; \n"
30
"typedef float color; \n"
31
" \n"
32
"#endif \n"
33
" \n"
34
"#if COMPONENTS == 1 \n"
35
" #define COLORTYPE color \n"
36
"#elif COMPONENTS == 2 \n"
37
" #define COLORTYPE color2 \n"
38
"#elif COMPONENTS == 3 \n"
39
" #define COLORTYPE color3 \n"
40
"#else \n"
41
" #define COLORTYPE color4 \n"
42
"#endif \n"
43
" \n"
44
"#endif \n"
45
" \n"
46
"// bilinear interpolation for texture rectangles - 16/32 bit float \n"
47
"COLORTYPE \n"
48
"bilinear (samplerIMG image, // the input image \n"
49
" float2 pos // texture coordinate \n"
50
" ) \n"
51
"{ \n"
52
" COLORTYPE c00, c01, c10, c11; \n"
53
" float2 fracTex = frac(pos); \n"
54
" float2 texC = floor(pos); \n"
55
" texC = texC + float2(0.5, 0.5); \n"
56
" c00 = texIMG(image, texC).rgb; \n"
57
" texC.x++; \n"
58
" c10 = texIMG(image, texC).rgb; \n"
59
" texC.y++; \n"
60
" c11 = texIMG(image, texC).rgb; \n"
61
" texC.x--; \n"
62
" c01 = texIMG(image, texC).rgb; \n"
63
" \n"
64
" float2 fracTex1 = 1.0 - fracTex; \n"
65
" COLORTYPE c0 = c00 * fracTex1.x + c10 * fracTex.x; \n"
66
" COLORTYPE c1 = c01 * fracTex1.x + c11 * fracTex.x; \n"
67
" COLORTYPE c = c0 * fracTex1.y + c1 * fracTex.y; \n"
68
"// COLORTYPE c0 = lerp(c00, c10, fracTex.x); \n"
69
"// COLORTYPE c1 = lerp(c01, c11, fracTex.x); \n"
70
"// COLORTYPE c = lerp(c0, c1, fracTex.y); \n"
71
" \n"
72
" return c; \n"
73
"} \n"
74
" \n"
75
"// 1D or 2D convolution with inline weights and offsets \n"
76
"// Note that loops will be unrolled by the compiler and the correct constants inserted inline \n"
77
"// This should compile to 3 instructions per sample (add, tex, mad) \n"
78
" \n"
79
"COLORTYPE \n"
80
"convolve(uniform samplerIMG image : TEXUNIT0, // the input image \n"
81
" uniform float4 offsetWeights[%d], // filter offsets and weights \n"
82
" uniform int nsamples, // number of samples \n"
83
" uniform int texRectFilter, // flag indicating use of texture rectangular filtering \n"
84
" float2 pos : TEXCOORD0 // position in image \n"
85
" ) : COLOR \n"
86
"{ \n"
87
" COLORTYPE c = 0; \n"
88
" COLORTYPE fc; \n"
89
" if (texRectFilter) { \n"
90
" pos = pos - float2(0.5, 0.5); \n"
91
" } \n"
92
" for(int i=0; i<nsamples; i++) { \n"
93
" if (!texRectFilter) { \n"
94
" c += texIMG(image, pos + offsetWeights[i].xy).rgb * offsetWeights[i].z; \n"
95
" } else { \n"
96
" float2 texC = pos + offsetWeights[i].xy; \n"
97
" fc = bilinear(image, texC); \n"
98
" c += fc * offsetWeights[i].z; \n"
99
" } \n"
100
" } \n"
101
" return c; \n"
102
"} \n"
103
" \n"
104
"COLORTYPE \n"
105
"convolveTex(uniform samplerIMG image : TEXUNIT0, // the input image \n"
106
" uniform sampler2D kernel : TEXUNIT1, // the kernel data (coefficients and offsets) \n"
107
" uniform int nsamples, // number of samples \n"
108
" uniform int texRectFilter, // flag indicating use of texture rectangular filtering \n"
109
" float2 pos : TEXCOORD0 // position in image \n"
110
" ) : COLOR \n"
111
"{ \n"
112
" COLORTYPE c = 0; \n"
113
" COLORTYPE fc; \n"
114
" if (texRectFilter) { \n"
115
" pos = pos - float2(0.5, 0.5); \n"
116
" } \n"
117
" int4 offset = int4(0); \n"
118
" float4 kernelData = float4(0.0); \n"
119
" for(int i=0; i<nsamples; i++) { \n"
120
" kernelData = tex2Dfetch(kernel, offset); \n"
121
" offset.x++; \n"
122
" if (!texRectFilter) { \n"
123
" c += texIMG(image, pos + kernelData.xy) * kernelData.z; \n"
124
" } else { \n"
125
" float2 texC = pos + kernelData.xy; \n"
126
" fc = bilinear(image, texC); \n"
127
" c += fc * kernelData.z; \n"
128
" } \n"
129
" } \n"
130
" return c; \n"
131
"} \n"
132
" \n"
133
"// mip level convolution - combine the different mip levels to form a large filter \n"
134
"// Note that loops will be unrolled by the compiler and the correct constants inserted inline \n"
135
"// This should compile to 3 instructions per sample (mad, tex, mad) \n"
136
" \n"
137
"COLORTYPE \n"
138
"mipConvolve(uniform samplerIMG image, // the input image \n"
139
" uniform float4 offsetWeights[%d], // filter offsets and weights \n"
140
" uniform float2 scale[%d], // mip level scale \n"
141
" uniform int nsamples, // number of levels \n"
142
" uniform int texRectFilter, // flag indicating use of texture rectangular filtering \n"
143
" float2 pos : TEXCOORD0 // position in image \n"
144
" ) : COLOR \n"
145
"{ \n"
146
" COLORTYPE c = 0; \n"
147
" COLORTYPE fc; \n"
148
" if (texRectFilter) { \n"
149
" pos = pos - float2(0.5, 0.5); \n"
150
" } \n"
151
" for(int i=0; i<nsamples; i++) { \n"
152
" if (!texRectFilter) { \n"
153
" c += texIMG(image, pos * scale[i] + offsetWeights[i].xy) * offsetWeights[i].z; \n"
154
" } else { \n"
155
" float2 texC = pos * scale[i] + offsetWeights[i].xy; \n"
156
" fc = bilinear(image, texC); \n"
157
" c += fc * offsetWeights[i].z; \n"
158
" } \n"
159
" } \n"
160
" \n"
161
" return c; \n"
162
"} \n"
163
" \n"
164
"// Version that looks up kernel weights from texture \n"
165
"// Note - not as efficient as inline weights (above), but it's easier to modify the kernel this way \n"
166
"// This should compile to 4 instructions per sample (add, tex, tex, mad) \n"
167
" \n"
168
"COLORTYPE \n"
169
"convolve_tex_2D(uniform samplerIMG image : TEXUNIT0, // the input image \n"
170
" uniform samplerRECT kernel : TEXUNIT1, // the kernel texture \n"
171
" uniform int kernel_width, // kernel width \n"
172
" uniform int kernel_height, // kernel height \n"
173
" uniform float2 kernel_offset, // kernel offset \n"
174
" uniform float2 texel_size, // texel size \n"
175
" float2 pos : TEXCOORD0 // position in image \n"
176
" ) : COLOR \n"
177
"{ \n"
178
" COLORTYPE c = 0; \n"
179
" for(int y=0; y<kernel_height; y++) { \n"
180
" for(int x=0; x<kernel_width; x++) { \n"
181
" color weight = texRECT(kernel, float2(x, y)).r; \n"
182
" c += texIMG(image, pos + (float2(x, y) + kernel_offset) * texel_size) * weight; \n"
183
" } \n"
184
" } \n"
185
" return c; \n"
186
"} \n"
187
" \n"
188
"// 1D version \n"
189
"COLORTYPE \n"
190
"convolve_tex_1D(uniform samplerIMG image : TEXUNIT0, // the input image \n"
191
" uniform samplerRECT kernel : TEXUNIT1, // the kernel texture \n"
192
" uniform int kernel_width, // kernel width \n"
193
" uniform float kernel_offset, // kernel offset \n"
194
" uniform float2 texel_size, \n"
195
" float2 pos : TEXCOORD0 // position in image \n"
196
" ) : COLOR \n"
197
"{ \n"
198
" COLORTYPE c = 0; \n"
199
" for(int x=0; x<kernel_width; x++) { \n"
200
" color weight = texRECT(kernel, float2(x, 0)).r; \n"
201
" c += texIMG(image, pos + (float2(x, x) + kernel_offset.xx) * texel_size) * weight; \n"
202
" } \n"
203
" return c; \n"
204
"} \n"
205
" \n"
206
"// 2D steerable filter with kernels stored in texture atlas \n"
207
"COLORTYPE \n"
208
"convolve_tex_steerable(uniform samplerIMG image : TEXUNIT0, // the input image \n"
209
" uniform samplerRECT kernel : TEXUNIT1, // the kernel texture \n"
210
" uniform int kernel_width, // kernel width \n"
211
" uniform int kernel_height, // kernel height \n"
212
" uniform half2 kernel_offset, // kernel offset \n"
213
" uniform int nkernels, // number of kernels in atlas \n"
214
" uniform half depth_scale = 1.0, \n"
215
" uniform half depth_bias = 0.0, \n"
216
" uniform float2 texel_size, \n"
217
" float2 pos : TEXCOORD0 // position in image \n"
218
" ) : COLOR \n"
219
"{ \n"
220
" // read blur amount from alpha channel \n"
221
" half depth = abs(texIMG(image, pos).a - depth_bias) * depth_scale; \n"
222
" half kernel_no = max(min(floor(depth * nkernels), nkernels), 0); \n"
223
" COLORTYPE c = 0; \n"
224
" for(int y=0; y<kernel_height; y++) { \n"
225
" for(int x=0; x<kernel_width; x++) { \n"
226
" color weight = texRECT(kernel, float2(x + kernel_no*kernel_width, y)).r; \n"
227
" c += texIMG(image, pos + (float2(x, y) + kernel_offset) * texel_size) * weight; \n"
228
" } \n"
229
" } \n"
230
" return c; \n"
231
"// return kernel_no / nkernels; \n"
232
"} \n"
233
" \n"
234
"#if 0 \n"
235
"// test values \n"
236
"color4 test_weights[3] = { \n"
237
" { 0.25, 0.25, 0.25, 1.0 }, \n"
238
" { 0.5, 0.5, 0.5, 1.0 }, \n"
239
" { 0.25, 0.25, 0.25, 1.0 }, \n"
240
"}; \n"
241
" \n"
242
"half test_weights2[3] = { \n"
243
" 0.25, \n"
244
" 0.5, \n"
245
" 0.25 \n"
246
"}; \n"
247
" \n"
248
" \n"
249
"half2 test_offsets[3] = { \n"
250
" { -1.0, 0.0 }, \n"
251
" { 0.0, 0.0 }, \n"
252
" { 1.0, 0.0 }, \n"
253
"}; \n"
254
" \n"
255
"color4 main(uniform samplerRECT image, \n"
256
" float2 wpos : WPOS \n"
257
" ) : COLOR \n"
258
"{ \n"
259
" return convolve(image, wpos, test_weights2, test_offsets, 3); \n"
260
"} \n"
261
"#endif \n"
262
" \n"
263
" \n"
264
" \n"
;
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (
www.mak.com
)