-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlockDataGPU.cpp
More file actions
231 lines (198 loc) · 6.65 KB
/
Copy pathBlockDataGPU.cpp
File metadata and controls
231 lines (198 loc) · 6.65 KB
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
#include "BlockDataGPU.hpp"
#include "betsy/EncoderBC1.h"
#include "betsy/EncoderBC4.h"
#include "betsy/EncoderBC6H.h"
#include "betsy/EncoderEAC.h"
#include "betsy/EncoderETC1.h"
#include "betsy/EncoderETC2.h"
#include "betsy/CpuImage.h"
#include "Timing.hpp"
#define TEXTURE_WIDTH 8
#define TEXTURE_HEIGHT 8
#define TEXTURE_CHANNEL 3
static bool cmp(PixBlock a, PixBlock b)
{
return a.error > b.error;
}
// save FTX format file
template <typename T>
void saveToOffData(T& encoder, const char* file_path)
{
betsy::CpuImage downloadImg;
encoder.startDownload();
encoder.downloadTo(downloadImg);
betsy::FormatKTX::save(file_path, downloadImg);
downloadImg.data = 0;
}
BlockDataGPU::BlockDataGPU()
{
m_Repeat = 1u;
m_Quality = 2;
}
BlockDataGPU::~BlockDataGPU()
{
m_Encoder.deinitResources();
}
void BlockDataGPU::setEncodingEnv()
{
m_Encoder.encoderShaderCompile(false, false);
glFinish();
}
void BlockDataGPU::setEncodingEnv(bool alpha)
{
m_Encoder.encoderShaderCompile(alpha, false);
glFinish();
}
void BlockDataGPU::initGPU(const char* input)
{
betsy::CpuImage cpuImage = betsy::CpuImage(input);
m_Encoder.initResources(cpuImage, false, false);
glFinish(); // wait initResource
}
// not use this function
void BlockDataGPU::ProcessWithGPU(std::shared_ptr<ErrorBlockData> pipeline, uint64_t blockLimit)
{
size_t repeat = 1u;
// ErrorBlock errorBlock = pipeline->getHighErrorBlocks();
// get pipeline
std::vector<PixBlock> pipe = pipeline->getPipe(); // 3ms
////std::cout << "Pipe size = " << pipe.size() << std::endl;
std::sort(pipe.begin(), pipe.end(), cmp); // 4ms
uint64_t limit = 8 * blockLimit;
limit = pipe.size() < limit ? pipe.size() : limit; // 4ms
std::vector<PixBlock> buffer;
std::vector<unsigned char> image;
buffer.resize(limit);
std::copy(pipe.begin(), pipe.begin() + limit, buffer.begin());
//std::cout << "Buffer Size = " << buffer.size() << std::endl;
for (int i = 0; i < buffer.size(); i++)
{
for (int t = 0; t < 48; t++)
{
image.push_back(buffer[i].bgrData[t]);
}
} // 5ms
////const int width = 4 * buffer.size();
////const int height = 4 * buffer.size();
////const int width = 800;
////const int height = 4 * pipe.size() / width;
////const int channel = 3;
////const int pitch = channel * width;
////const int arraySize = width * height * channel;
////unsigned char* data = new unsigned char[arraySize];
////int off_x = 0;
////int off_y = 0;
////for (int t = 0; t < buffer.size(); t++)
////{
//// for (int j = 0; j < 4; j++)
//// {
//// for (int i = 0; i < 4; i++)
//// {
//// int offset = pitch * (off_y + j) + channel * (off_x + i);
//// data[offset + 0] = buffer[t].bgrData[channel * (4 * j + i) + 0];
//// data[offset + 1] = buffer[t].bgrData[channel * (4 * j + i) + 1];
//// data[offset + 2] = buffer[t].bgrData[channel * (4 * j + i) + 2];
//// }
//// }
//// off_x += 4;
//// if (off_x > width)
//// {
//// off_y += 4;
//// off_x = 0;
//// }
////}
////betsy::CpuImage cpuImage = betsy::CpuImage(data, arraySize, width, height, channel);
////m_Encoder.initResources(cpuImage, false, false);
int w = 4;
int h = 4 * buffer.size();
int c = 3;
int arraySize = w * h * c;
betsy::CpuImage cpuImage = betsy::CpuImage(image.data(), arraySize, w, h, c);
m_Encoder.initResources(cpuImage, false, false); // 5ms
////start = GetTime();
while (repeat--) // this loop is 13 ms
{
m_Encoder.execute00();
m_Encoder.execute01(static_cast<betsy::EncoderETC1::Etc1Quality>(1)); // setting mid quality
m_Encoder.execute02();
}
//saveToOffData(m_Encoder, "res.ktx");
uint8_t* result = m_Encoder.getDownloadData();
uint32_t offset = 0u;
for (int i=buffer.size() - 1; i>=0; --i)
{
auto dst = buffer[i].address;
m_Encoder.saveToOffset(dst, result);
result += 8; // for jump 64bits.
}
}
void BlockDataGPU::ProcessWithGPU(PixBlock* pipeline, int pipeSize, uint64_t blockLimit, float quality)
{
size_t repeat = 1u;
uint64_t limit = pipeSize * quality;
PixBlock* buf = new PixBlock[limit];
if (quality < 1.0) // Not Bset mode
{
std::sort(pipeline, pipeline + pipeSize, cmp); // sorting about error
std::memcpy(buf, pipeline, limit * sizeof(PixBlock));
}
else // Best mode
{
// not sorting, only store process
std::memcpy(buf, pipeline, limit * sizeof(PixBlock));
}
// GPU image implementation.
int a = std::sqrt(limit);
int gpuImageWidth = (int)a * 4 + 4;
int gpuImageHeight = (int)a * 4 + 4;
int gpuImageChannel = 3;
int gpuImagePicth = gpuImageWidth * gpuImageChannel;
int gpuImageArraySize = gpuImageChannel * gpuImageWidth * gpuImageHeight;
unsigned char* GpuImage = new unsigned char[gpuImageArraySize](); // initalization zero
// make GPU image
int idx = 0;
unsigned char r = 255;
int image_offset_x = 0;
int image_offset_y = 0;
int width = 0;
int x = 0;
int y = 0;
for (int i = 0; i < limit; i++) // buffer size if 4,
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
int offset = gpuImagePicth * (image_offset_y + (3 - y)) + gpuImageChannel * (image_offset_x + x);
GpuImage[offset + 0] = buf[i].bgrData[gpuImageChannel * (4 * y + x) + 0];
GpuImage[offset + 1] = buf[i].bgrData[gpuImageChannel * (4 * y + x) + 1];
GpuImage[offset + 2] = buf[i].bgrData[gpuImageChannel * (4 * y + x) + 2];
}
}
image_offset_x += 4;
if (image_offset_x >= gpuImageWidth)
{
image_offset_y += 4;
image_offset_x = 0;
}
}
betsy::CpuImage cpuImage = betsy::CpuImage(GpuImage, gpuImageArraySize, gpuImageWidth, gpuImageHeight, gpuImageChannel);
m_Encoder.initResources(cpuImage, false, false);
while (repeat--)
{
m_Encoder.execute00();
m_Encoder.execute01(static_cast<betsy::EncoderETC1::Etc1Quality>(1)); // setting mid quality
m_Encoder.execute02();
}
// saveToOffData(m_Encoder, "res.ktx"); // to save mid result
uint8_t* result = m_Encoder.getDownloadData();
uint32_t offset = 0u;
for (int i = 0; i < limit; i++)
{
auto dst = buf[i].address;
m_Encoder.saveToOffset(dst, result);
result += 8; // for jump 64bits.
}
delete[] buf;
delete[] GpuImage;
}