TDME2  1.9.200
BC7TextureWriter.cpp
Go to the documentation of this file.
2 
3 #include <ext/bc7enc_rdo/bc7enc.h>
4 
5 #include <string>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
9 #include <tdme/engine/Texture.h>
10 #include <tdme/math/Math.h>
12 #include <tdme/utilities/Console.h>
13 
14 using std::string;
15 using std::to_string;
16 using std::vector;
17 
19 
21 using tdme::math::Math;
24 
25 bool BC7TextureWriter::write(int width, int height, int bytesPerPixel, const ByteBuffer& textureByteBuffer, vector<uint8_t>& bc7Data) {
26  //
27  bc7enc_compress_block_params bc7encParameters;
28  bc7encParameters.m_uber_level = BC7ENC_MAX_UBER_LEVEL;
29  bc7enc_compress_block_params_init(&bc7encParameters);
30  bc7enc_compress_block_params_init_linear_weights(&bc7encParameters);
31 
32  //
33  auto xBlocks = static_cast<int>(Math::ceil(width / 4.0f));
34  auto yBlocks = static_cast<int>(Math::ceil(height / 4.0f));
35  bc7Data.resize(xBlocks * yBlocks * 16);
36 
37  //
38  for (auto yBlock = 0; yBlock < yBlocks; yBlock++) {
39  for (auto xBlock = 0; xBlock < xBlocks; xBlock++) {
40  //
41  array<uint8_t, 4 * 4 * 4> rgbaBlockPixels { 0 };
42  auto blockPixelIdx = 0;
43  auto xBlockOffset = xBlock * 4;
44  auto yBlockOffset = yBlock * 4;
45  for (int y = 0; y < 4; y++) {
46  for (int x = 0; x < 4; x++) {
47  auto offset = (yBlockOffset + y) * width * bytesPerPixel + (xBlockOffset + x) * bytesPerPixel;
48  auto red = 0xff;
49  auto green = 0;
50  auto blue = 0;
51  auto alpha = 0;
52  if (offset < textureByteBuffer.getCapacity()) {
53  red = textureByteBuffer.get(offset + 0);
54  green = textureByteBuffer.get(offset + 1);
55  blue = textureByteBuffer.get(offset + 2);
56  alpha = bytesPerPixel == 4?textureByteBuffer.get(offset + 3):0xff;
57  }
58  rgbaBlockPixels[blockPixelIdx * 4 + 0] = red;
59  rgbaBlockPixels[blockPixelIdx * 4 + 1] = green;
60  rgbaBlockPixels[blockPixelIdx * 4 + 2] = blue;
61  rgbaBlockPixels[blockPixelIdx * 4 + 3] = alpha;
62  blockPixelIdx++;
63  }
64  }
65  //
66  bc7enc_compress_block_init();
67  bc7enc_compress_block(&bc7Data.data()[yBlock * xBlocks * 16 + xBlock * 16], rgbaBlockPixels.data(), &bc7encParameters);
68  }
69  }
70 
71  //
72  return true;
73 }
Texture entity.
Definition: Texture.h:24
Standard math functions.
Definition: Math.h:19
uint8_t get(int64_t position) const
Definition: Buffer.h:107
virtual int64_t getCapacity() const
Definition: Buffer.h:82
Byte buffer class.
Definition: ByteBuffer.h:27
Console class.
Definition: Console.h:29