Repository navigation
Expand file tree
/
Copy pathPBO.cpp
More file actions
69 lines (54 loc) · 2.21 KB
/
Copy pathPBO.cpp
File metadata and controls
69 lines (54 loc) · 2.21 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
/*
This file is part of the Rendering library.
Copyright (C) 2007-2012 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2007-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2007-2012 Ralf Petring <ralf@petring.net>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#if defined(LIB_GL)
#include "PBO.h"
#include "Texture/Texture.h"
#include "BufferObject.h"
#include "GLHeader.h"
#include "Helper.h"
#include <Util/Graphics/Bitmap.h>
#include <algorithm>
#include <cstddef>
#include <stdexcept>
#include <cstdint>
namespace Rendering {
PBO::PBO() : bufferObject(), width(0), height(0) {
if(!isExtensionSupported("GL_ARB_pixel_buffer_object")) {
throw std::runtime_error("Fatal error: OpenGL extension GL_ARB_pixel_buffer_object is not supported.");
}
}
void PBO::allocateBufferData(uint32_t newWidth, uint32_t newHeight) {
const uint32_t numBytes = 4; // BGRA
const uint32_t oldDataSize = numBytes * width * height;
const uint32_t newDataSize = numBytes * newWidth * newHeight;
width = newWidth;
height = newHeight;
if(newDataSize == oldDataSize || newDataSize == 0) {
return;
}
bufferObject.allocateData<uint8_t>(GL_PIXEL_PACK_BUFFER, newDataSize, GL_STREAM_READ);
}
void PBO::asynchronousReadPixels(uint32_t x, uint32_t y, uint32_t _width, uint32_t _height) {
allocateBufferData(_width, _height);
bufferObject.bind(GL_PIXEL_PACK_BUFFER);
glReadPixels(static_cast<int>(x), static_cast<int>(y), static_cast<int>(_width), static_cast<int>(_height), GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
bufferObject.unbind(GL_PIXEL_PACK_BUFFER);
}
bool PBO::fillTexture(Texture * texture) const {
const Texture::Format & texFormat = texture->getFormat();
if(texFormat.sizeX != width || texFormat.sizeY != height || texFormat.pixelFormat.glLocalDataFormat != GL_BGRA || texFormat.pixelFormat.glLocalDataType != GL_UNSIGNED_BYTE) {
return false;
}
const uint32_t numBytes = 4 * width * height;
texture->getLocalBitmap()->setData(bufferObject.downloadData<uint8_t>(GL_PIXEL_PACK_BUFFER, numBytes));
return true;
}
}
#endif /* defined(LIB_GL) */