File: crn_dxt5a.cpp

package info (click to toggle)
crunch-dxtc 0.55.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 3,600 kB
  • sloc: cpp: 64,979; ansic: 633; python: 321; makefile: 112
file content (189 lines) | stat: -rw-r--r-- 5,846 bytes parent folder | download | duplicates (2)
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
// File: crn_dxt5a.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_dxt5a.h"
#include "crn_ryg_dxt.hpp"
#include "crn_dxt_fast.h"
#include "crn_intersect.h"

namespace crnlib {
dxt5_endpoint_optimizer::dxt5_endpoint_optimizer()
    : m_pParams(NULL),
      m_pResults(NULL) {
  m_unique_values.reserve(16);
  m_unique_value_weights.reserve(16);
}

bool dxt5_endpoint_optimizer::compute(const params& p, results& r) {
  m_pParams = &p;
  m_pResults = &r;

  if ((!p.m_num_pixels) || (!p.m_pPixels))
    return false;

  m_unique_values.resize(0);
  m_unique_value_weights.resize(0);

  for (uint i = 0; i < 256; i++)
    m_unique_value_map[i] = -1;

  for (uint i = 0; i < p.m_num_pixels; i++) {
    uint alpha = p.m_pPixels[i][p.m_comp_index];

    int index = m_unique_value_map[alpha];

    if (index == -1) {
      index = m_unique_values.size();

      m_unique_value_map[alpha] = index;

      m_unique_values.push_back(static_cast<uint8>(alpha));
      m_unique_value_weights.push_back(0);
    }

    m_unique_value_weights[index]++;
  }

  if (m_unique_values.size() == 1) {
    r.m_block_type = 0;
    r.m_reordered = false;
    r.m_error = 0;
    r.m_first_endpoint = m_unique_values[0];
    r.m_second_endpoint = m_unique_values[0];
    memset(r.m_pSelectors, 0, p.m_num_pixels);
    return true;
  }

  m_trial_selectors.resize(m_unique_values.size());
  m_best_selectors.resize(m_unique_values.size());

  r.m_error = cUINT64_MAX;

  for (uint i = 0; i < m_unique_values.size() - 1; i++) {
    const uint low_endpoint = m_unique_values[i];

    for (uint j = i + 1; j < m_unique_values.size(); j++) {
      const uint high_endpoint = m_unique_values[j];

      evaluate_solution(low_endpoint, high_endpoint);
    }
  }

  if ((m_pParams->m_quality >= cCRNDXTQualityBetter) && (m_pResults->m_error)) {
    m_flags.resize(256 * 256);
    m_flags.clear_all_bits();

    const int cProbeAmount = (m_pParams->m_quality == cCRNDXTQualityUber) ? 16 : 8;

    for (int l_delta = -cProbeAmount; l_delta <= cProbeAmount; l_delta++) {
      const int l = m_pResults->m_first_endpoint + l_delta;
      if (l < 0)
        continue;
      else if (l > 255)
        break;

      const uint bit_index = l * 256;

      for (int h_delta = -cProbeAmount; h_delta <= cProbeAmount; h_delta++) {
        const int h = m_pResults->m_second_endpoint + h_delta;
        if (h < 0)
          continue;
        else if (h > 255)
          break;

        //if (m_flags.get_bit(bit_index + h))
        //   continue;
        if ((m_flags.get_bit(bit_index + h)) || (m_flags.get_bit(h * 256 + l)))
          continue;
        m_flags.set_bit(bit_index + h);

        evaluate_solution(static_cast<uint>(l), static_cast<uint>(h));
      }
    }
  }

  m_pResults->m_reordered = false;
  if (m_pResults->m_first_endpoint == m_pResults->m_second_endpoint) {
    for (uint i = 0; i < m_best_selectors.size(); i++)
      m_best_selectors[i] = 0;
  } else if (m_pResults->m_block_type) {
    //if (l > h)
    //   eight alpha
    // else
    //   six alpha

    if (m_pResults->m_first_endpoint > m_pResults->m_second_endpoint) {
      utils::swap(m_pResults->m_first_endpoint, m_pResults->m_second_endpoint);
      m_pResults->m_reordered = true;
      for (uint i = 0; i < m_best_selectors.size(); i++)
        m_best_selectors[i] = g_six_alpha_invert_table[m_best_selectors[i]];
    }
  } else if (!(m_pResults->m_first_endpoint > m_pResults->m_second_endpoint)) {
    utils::swap(m_pResults->m_first_endpoint, m_pResults->m_second_endpoint);
    m_pResults->m_reordered = true;
    for (uint i = 0; i < m_best_selectors.size(); i++)
      m_best_selectors[i] = g_eight_alpha_invert_table[m_best_selectors[i]];
  }

  for (uint i = 0; i < m_pParams->m_num_pixels; i++) {
    uint alpha = m_pParams->m_pPixels[i][m_pParams->m_comp_index];

    int index = m_unique_value_map[alpha];

    m_pResults->m_pSelectors[i] = m_best_selectors[index];
  }

  return true;
}

void dxt5_endpoint_optimizer::evaluate_solution(uint low_endpoint, uint high_endpoint) {
  for (uint block_type = 0; block_type < (m_pParams->m_use_both_block_types ? 2U : 1U); block_type++) {
    uint selector_values[8];

    if (!block_type)
      dxt5_block::get_block_values8(selector_values, low_endpoint, high_endpoint);
    else
      dxt5_block::get_block_values6(selector_values, low_endpoint, high_endpoint);

    uint64 trial_error = 0;

    for (uint i = 0; i < m_unique_values.size(); i++) {
      const uint val = m_unique_values[i];
      const uint weight = m_unique_value_weights[i];

      uint best_selector_error = UINT_MAX;
      uint best_selector = 0;

      for (uint j = 0; j < 8; j++) {
        int selector_error = val - selector_values[j];
        selector_error = selector_error * selector_error * (int)weight;

        if (static_cast<uint>(selector_error) < best_selector_error) {
          best_selector_error = selector_error;
          best_selector = j;
          if (!best_selector_error)
            break;
        }
      }

      m_trial_selectors[i] = static_cast<uint8>(best_selector);
      trial_error += best_selector_error;

      if (trial_error > m_pResults->m_error)
        break;
    }

    if (trial_error < m_pResults->m_error) {
      m_pResults->m_error = trial_error;
      m_pResults->m_first_endpoint = static_cast<uint8>(low_endpoint);
      m_pResults->m_second_endpoint = static_cast<uint8>(high_endpoint);
      m_pResults->m_block_type = static_cast<uint8>(block_type);
      m_best_selectors.swap(m_trial_selectors);

      if (!trial_error)
        break;
    }
  }
}

}  // namespace crnlib