group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
SMAASearchTex.h
Go to the documentation of this file.
1#pragma once
21
22#include <cmath>
23#include <cstring>
24
25static constexpr int SMAA_SEARCHTEX_WIDTH = 66;
26static constexpr int SMAA_SEARCHTEX_HEIGHT = 33;
27static constexpr int SMAA_SEARCHTEX_PITCH = SMAA_SEARCHTEX_WIDTH; // R8 = 1 byte/pixel
28
30static constexpr int SMAA_SEARCHTEX_PACKED_WIDTH = 64;
31static constexpr int SMAA_SEARCHTEX_PACKED_HEIGHT = 16;
32
33// ---------------------------------------------------------------------------
34// Internal helpers
35// ---------------------------------------------------------------------------
37{
38
55
61inline unsigned char calcSearchValue(int e1, int e2, float bias)
62{
63 // Bilinear interpolation result of two adjacent edge values.
64 float bilinear = float(e1) * (1.0f - bias) + float(e2) * bias;
65
66 // If both edges are active (bilinear ~= 1.0), the search should continue.
67 // If only one is active (bilinear ~= 0.5), we've found an endpoint.
68 // If neither is active (bilinear ~= 0.0), there's no edge -- stop.
69
70 if (bilinear < 0.25f) {
71 return 0; // No edge: stop
72 } else if (bilinear < 0.75f) {
73 return 127; // Endpoint found: return fractional offset
74 } else {
75 return 255; // Edge continues: keep searching
76 }
77}
78
79} // namespace smaa_search_detail
80
83inline void generateSearchTex(unsigned char* out)
84{
85 std::memset(out, 0, SMAA_SEARCHTEX_WIDTH * SMAA_SEARCHTEX_HEIGHT);
86
87 // The search texture is organised as two halves:
88 // Top half (y = 0..15): left/up search direction
89 // Bottom half (y = 16..32): right/down search direction
90 //
91 // Within each half:
92 // x axis (0..63): bilinear result of adjacent edge pixel pair, quantised
93 // y axis (0..15): crossing edge flag (0 or 1) and sub-type
94 //
95 // The extra 2 columns (64, 65) are padding for safe bilinear filtering.
96
97 // For each possible combination of two adjacent edge texels:
98 for (int e1 = 0; e1 < 2; e1++) { // Current pixel edge
99 for (int e2 = 0; e2 < 2; e2++) { // Adjacent pixel edge
100 // Horizontal search patterns:
101 // The bilinear filter result when sampling between e1 and e2
102 // depends on the sub-pixel offset.
103
104 // Generate entries for the search direction (left/up).
105 for (int step = 0; step < SMAA_SEARCHTEX_PACKED_WIDTH; step++) {
106 float bias = float(step) / float(SMAA_SEARCHTEX_PACKED_WIDTH - 1);
107
108 unsigned char val = smaa_search_detail::calcSearchValue(e1, e2, bias);
109
110 // Left/up half of texture.
111 int x = step;
112 int y = e1 * 8 + e2 * 4;
113
114 // Fill a 4-row band for this pattern (provides filtering margin).
115 for (int dy = 0; dy < 4; dy++) {
116 int py = y + dy;
118 out[py * SMAA_SEARCHTEX_WIDTH + x] = val;
119 }
120 }
121
122 // Right/down half of texture (y offset by PACKED_HEIGHT + 1).
123 int y2 = SMAA_SEARCHTEX_PACKED_HEIGHT + 1 + e1 * 8 + e2 * 4;
124 unsigned char val2 = smaa_search_detail::calcSearchValue(e2, e1, 1.0f - bias);
125 for (int dy = 0; dy < 4; dy++) {
126 int py = y2 + dy;
128 out[py * SMAA_SEARCHTEX_WIDTH + x] = val2;
129 }
130 }
131 }
132 }
133 }
134
135 // Fill the padding columns (64, 65) by repeating column 63.
136 for (int y = 0; y < SMAA_SEARCHTEX_HEIGHT; y++) {
137 for (int px = SMAA_SEARCHTEX_PACKED_WIDTH; px < SMAA_SEARCHTEX_WIDTH; px++) {
139 }
140 }
141}
static constexpr int SMAA_SEARCHTEX_PACKED_WIDTH
Packed texture dimensions used during bilinear sampling.
Definition SMAASearchTex.h:30
static constexpr int SMAA_SEARCHTEX_PACKED_HEIGHT
Definition SMAASearchTex.h:31
static constexpr int SMAA_SEARCHTEX_PITCH
Definition SMAASearchTex.h:27
static constexpr int SMAA_SEARCHTEX_HEIGHT
Definition SMAASearchTex.h:26
static constexpr int SMAA_SEARCHTEX_WIDTH
Definition SMAASearchTex.h:25
void generateSearchTex(unsigned char *out)
Generate the SMAA search texture into a caller-provided buffer.
Definition SMAASearchTex.h:83
Definition SMAASearchTex.h:37
unsigned char calcSearchValue(int e1, int e2, float bias)
Encode two edge values (e1, e2 in {0,1}) and their bilinear position into a search texture value.
Definition SMAASearchTex.h:61