File: gltrace_unpack_compressed.cpp

package info (click to toggle)
apitrace 11.1%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,648 kB
  • sloc: cpp: 183,110; python: 33,685; ansic: 25,073; sh: 143; makefile: 88
file content (250 lines) | stat: -rw-r--r-- 9,693 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright © 2020 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */


#include "gltrace_unpack_compressed.hpp"
#include "gltrace.hpp"


template<typename X, typename Y>
auto _div_round_up(X a, Y b) -> decltype(a / b) {
    return (a + b - 1) / b;
}

struct UnpackParams
{
    GLint block_size = 0;
    GLint block_width = 1;
    GLint block_height = 1;
    GLint block_depth = 1;

    GLint skip_pixels = 0;
    GLint row_length = 0;
    GLint skip_rows = 0;
    GLint image_height = 0;
    GLint skip_images = 0;
};

UnpackParams
getUnpackParams(bool compressed) {
    UnpackParams unpack_params;

    _glGetIntegerv(GL_UNPACK_SKIP_PIXELS,  &unpack_params.skip_pixels);
    _glGetIntegerv(GL_UNPACK_ROW_LENGTH,   &unpack_params.row_length);
    _glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &unpack_params.image_height);
    _glGetIntegerv(GL_UNPACK_SKIP_ROWS,    &unpack_params.skip_rows);
    _glGetIntegerv(GL_UNPACK_SKIP_IMAGES,  &unpack_params.skip_images);

    if (compressed) {
        _glGetIntegerv(GL_UNPACK_COMPRESSED_BLOCK_SIZE,   &unpack_params.block_size);
        _glGetIntegerv(GL_UNPACK_COMPRESSED_BLOCK_WIDTH,  &unpack_params.block_width);
        _glGetIntegerv(GL_UNPACK_COMPRESSED_BLOCK_HEIGHT, &unpack_params.block_height);
        _glGetIntegerv(GL_UNPACK_COMPRESSED_BLOCK_DEPTH,  &unpack_params.block_depth);
    }

    return unpack_params;
}

bool
canTakeFastPath(const UnpackParams& unpack_params, GLsizei width, GLsizei height, GLsizei depth) {
  /* OpenGL 4.6, 8.7 Compressed Texture Images:
   *
   *  By default the pixel storage modes UNPACK_ROW_LENGTH, UNPACK_SKIP_ROWS,
   *  UNPACK_SKIP_PIXELS, UNPACK_IMAGE_HEIGHT and UNPACK_SKIP_IMAGES are ignored
   *  for compressed images. To enable UNPACK_SKIP_PIXELS and UNPACK_ROW_LENGTH,
   *  block size and b_w must both be non-zero.
   */
  if (unpack_params.block_size == 0 || unpack_params.block_width == 0)
    return true;

  if (unpack_params.skip_pixels)
    return false;

  if (width > 0 && unpack_params.row_length > 0 && width < unpack_params.row_length)
    return false;

   /* OpenGL 4.6, 8.7 Compressed Texture Images:
    *
    *  To also enable UNPACK_SKIP_ROWS and UNPACK_IMAGE_HEIGHT, b_h must be non-zero.
    *
    * If zero - other paramters won't matter.
    */
  if (height == 0 || unpack_params.block_height == 0)
    return true;

  if (unpack_params.skip_rows)
    return false;

  if (unpack_params.image_height > 0 && height < unpack_params.image_height)
    return false;

   /* OpenGL 4.6, 8.7 Compressed Texture Images:
    *
    *  And to also enable UNPACK_SKIP_IMAGES, b_d must be non-zero.
    */
  if (depth == 0 || unpack_params.block_depth == 0)
    return true;

  if(unpack_params.skip_images)
    return false;

  return true;
}

void
writeCompressedTex(const void * data, GLenum format, GLsizei width, GLsizei height, GLsizei depth,
                   GLsizei imageSize, GLboolean has_unpack_subimage, std::function<void(const void*, GLsizei)> callback)
{
   /* imageSize may not be the amount of bytes we must copy from user's ptr
    * if the pixel storage modes were set by user. Supplied imageSize​ has to be
    *   GL_UNPACK_COMPRESSED_BLOCK_SIZE * (width / GL_UNPACK_COMPRESSED_BLOCK_WIDTH) *
    *   (height / GL_UNPACK_COMPRESSED_BLOCK_HEIGHT) * (depth / GL_UNPACK_COMPRESSED_BLOCK_DEPTH)
    * which doesn't account storage modes. However storage mods do affect how
    * much bytes we should copy.
    */

    if (!has_unpack_subimage) {
        callback(data, imageSize);
        return;
    }

    UnpackParams unpack_params = getUnpackParams(true);

    if (canTakeFastPath(unpack_params, width, height, depth)) {
        callback(data, imageSize);
        return;
    }

    size_t blocks_in_row = _div_round_up(width, unpack_params.block_width);
    size_t copy_bytes_per_row= blocks_in_row * unpack_params.block_size;
    size_t total_bytes_per_row = copy_bytes_per_row;

    size_t copy_rows_per_slice = height > 0 ? _div_round_up(height, unpack_params.block_height) : 1;
    size_t total_rows_per_slice = copy_rows_per_slice;

    size_t copy_slices = depth > 0 ? _div_round_up(depth, unpack_params.block_depth) : 1;

    /* OpenGL 4.6, 8.7 Compressed Texture Images:
     *
     *  Before obtaining the first compressed image block from memory,
     *  the data pointer is advanced by:
     *    (UNPACK_SKIP_PIXELS / b_w) * n + (UNPACK_SKIP_ROWS / b_h) * k
     *  elements.
     *
     * Where n = 1 and k is blocks in a row.
     * This is "(UNPACK_SKIP_PIXELS / b_w) * n" part
     */
    size_t skip_bytes = unpack_params.skip_pixels / unpack_params.block_width * 1 * unpack_params.block_size;

    /* OpenGL 4.6, 8.4.4.1 Unpacking:
     *
     *  If the value of UNPACK_ROW_LENGTH is zero, then the number of groups in a row
     *  is width; otherwise the number of groups is the value of UNPACK_ROW_LENGTH.
     */

    if (unpack_params.row_length) {
        total_bytes_per_row = unpack_params.block_size * _div_round_up(unpack_params.row_length, unpack_params.block_width);
    }

    /* OpenGL 4.6, 8.7 Compressed Texture Images:
     *
     *  To also enable UNPACK_SKIP_ROWS and UNPACK_IMAGE_HEIGHT, b_h must be non-zero.
     */
    if (height > 0 && unpack_params.block_height > 0) {
        /* OpenGL 4.6, 8.7 Compressed Texture Images:
         *
         *  + (UNPACK_SKIP_ROWS / b_h) * k
         */
        skip_bytes += unpack_params.skip_rows / unpack_params.block_height * total_bytes_per_row;

        if (unpack_params.image_height) {
            total_rows_per_slice = _div_round_up(unpack_params.image_height, unpack_params.block_height);
        }
    }

    /* OpenGL 4.6, 8.5 Texture Image Specification:
     *
     *  And to also enable UNPACK_SKIP_IMAGES, b_d must be non-zero.
     */
    if (depth > 0 && unpack_params.block_depth > 0) {
        /* OpenGL 4.6, 8.7 Compressed Texture Images:
         *
         *  For three-dimensional compressed images the pointer is advanced by
         *  UNPACK_SKIP_IMAGES / b_d times the number of elements in one two-dimensional
         *  image before obtaining the first group from memory.
         */
        skip_bytes += unpack_params.skip_images * total_bytes_per_row * total_rows_per_slice / unpack_params.block_depth;
    }

    size_t alloc_size = skip_bytes + depth * total_bytes_per_row * total_rows_per_slice / std::max(unpack_params.block_depth, 1);
    alloc_size += total_bytes_per_row * copy_rows_per_slice;
    std::vector<uint8_t> copied_data(alloc_size, 0);

    uint8_t* dst_pointer = copied_data.data();
    const uint8_t* src_pointer = reinterpret_cast<const uint8_t*>(data);

    src_pointer += skip_bytes;
    dst_pointer += skip_bytes;

    for (size_t slice = 0; slice < copy_slices; slice++) {
         assert(dst_pointer < (copied_data.data() + copied_data.size()));

         if (total_bytes_per_row == copy_bytes_per_row) {
            assert(dst_pointer < (copied_data.data() + copied_data.size()));

            // No skips between rows so we can get away with one copy
            size_t to_copy = copy_bytes_per_row * copy_rows_per_slice;
            memcpy(reinterpret_cast<void*>(dst_pointer), src_pointer, to_copy);
            src_pointer += to_copy;
            dst_pointer += to_copy;
         } else {
            /* OpenGL 4.6, 8.7 Compressed Texture Images:
             *
             * Then width / b_w blocks are obtained from contiguous blocks in memory (without advancing the pointer),
             * after which the pointer is advanced by k elements.
             * height / b_h sets of width / b_w blocks are obtained this way.
             */
            for (size_t row = 0; row < copy_rows_per_slice; row++) {
               assert(dst_pointer < (copied_data.data() + copied_data.size()));

               memcpy(reinterpret_cast<void*>(dst_pointer), src_pointer, copy_bytes_per_row);
               src_pointer += total_bytes_per_row;
               dst_pointer += total_bytes_per_row;
            }
         }

         /* OpenGL 4.6, 8.7 Compressed Texture Images:
          *
          *  Then after height rows are obtained, if UNPACK_IMAGE_HEIGHT is positive,
          *  the pointer skips over the (UNPACK_IMAGE_HEIGHT - height) / b_h rows,
          *  before obtaining the next set of blocks.
          *
          * Pointer will be right at the start of the sub-image in the next slice.
          */
         size_t advance = total_bytes_per_row * (total_rows_per_slice - copy_rows_per_slice);
         src_pointer += advance;
         dst_pointer += advance;
    }

    callback(copied_data.data(), copied_data.size());
}