Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
#include "Platform/Video/OpenGL/Headers/glResources.h"
#include "Headers/glPixelBuffer.h"

#include "Core/Headers/Console.h"
#include "Utility/Headers/Localization.h"
#include "Platform/Video/Headers/GFXDevice.h"

namespace Divide {

size_t glPixelBuffer::sizeOf(GLenum dataType) const{
    switch(_dataType){
        case GL_FLOAT: return sizeof(GLfloat);
        case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
        case GL_UNSIGNED_INT: return sizeof(GLuint);
        case GL_UNSIGNED_SHORT: return sizeof(GLushort);
        case GL_BYTE: return sizeof(GLbyte);
        case GL_SHORT: return sizeof(GLshort);
        case GL_INT: return sizeof(GLint);
    };
    return 0;
}

glPixelBuffer::glPixelBuffer(PBType type) : PixelBuffer(type) {
    switch(_pbtype){
        case PB_TEXTURE_1D:
            _textureType = GL_TEXTURE_1D;
            break;
        case PB_TEXTURE_2D:
            _textureType = GL_TEXTURE_2D;
            break;
        case PB_TEXTURE_3D:
            _textureType = GL_TEXTURE_3D;
            break;
        default:
            ERROR_FN(Locale::get("ERROR_PB_INVALID_TYPE"));
            break;
    };
}

void glPixelBuffer::Destroy() {
    if(_textureId > 0){
        glDeleteTextures(1, &_textureId);
        _textureId = 0;
    }

    if(_pixelBufferHandle > 0){
        glDeleteBuffers(1, &_pixelBufferHandle);
        _pixelBufferHandle = 0;
    }
    _width = 0;
    _height = 0;
    _depth = 0;
}

void* glPixelBuffer::Begin(GLubyte nFace) const {
    DIVIDE_ASSERT(nFace < 6, "glPixelBuffer error: Tried to map an invalid PBO texture's face!");

    GL_API::bindTexture(0, _textureId, _textureType);
    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, _pixelBufferHandle);
    switch(_pbtype){
        case PB_TEXTURE_1D:
            glTexSubImage1D(_textureType, 0, 0, _width, _format, _dataType, 0);
            break;
        case PB_TEXTURE_2D:
            glTexSubImage2D(_textureType, 0, 0, 0, _width, _height, _format, _dataType, 0);
            break;
        case PB_TEXTURE_3D:
            glTexSubImage3D(_textureType, 0, 0, 0, 0, _width, _height, _depth, _format, _dataType, 0);
            break;
    };

    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, _pixelBufferHandle);
    switch(_pbtype){
        case PB_TEXTURE_1D:
            glBufferData(GL_PIXEL_UNPACK_BUFFER, (_width*4) * sizeOf(_dataType), NULL, GL_STREAM_DRAW);
            break;
        case PB_TEXTURE_2D:
            glBufferData(GL_PIXEL_UNPACK_BUFFER, (_width*_height*4) * sizeOf(_dataType), NULL, GL_STREAM_DRAW);
            break;
        case PB_TEXTURE_3D:
            glBufferData(GL_PIXEL_UNPACK_BUFFER, (_width*_height*_depth*4) * sizeOf(_dataType), NULL, GL_STREAM_DRAW);
            break;
    };

    return glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
}

void glPixelBuffer::End() const {

    glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // release the mapped buffer
    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
    GL_API::unbindTexture(0, _textureType);
}

void glPixelBuffer::Bind(GLubyte unit) const {
    GL_API::bindTexture(unit, _textureId, _textureType);
}

bool glPixelBuffer::Create(GLushort width, 
                           GLushort height,
                           GLushort depth, 
                           GFXImageFormat internalFormatEnum, 
                           GFXImageFormat formatEnum,
                           GFXDataFormat dataTypeEnum) {

    _internalFormat = GLUtil::GL_ENUM_TABLE::glImageFormatTable[internalFormatEnum];
    _format         = GLUtil::GL_ENUM_TABLE::glImageFormatTable[formatEnum];
    _dataType       = GLUtil::GL_ENUM_TABLE::glDataFormat[dataTypeEnum];

    Destroy();
    PRINT_FN(Locale::get("GL_PB_GEN"),width,height);
    _width = width;
    _height = height;
    _depth = depth;
    GLuint size = _width;
    if(_pbtype != PB_TEXTURE_1D)
        size *= _height;
    if(_pbtype == PB_TEXTURE_3D)
        size *= _depth;

    size *= 4/*channels*/;

    glGenTextures(1, &_textureId);
    GL_API::setPixelPackUnpackAlignment();
    GL_API::bindTexture(0, _textureId, _textureType);
    glTexParameteri(_textureType, GL_GENERATE_MIPMAP, GL_FALSE);
    glTexParameteri(_textureType,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(_textureType,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(_textureType, GL_TEXTURE_BASE_LEVEL, 0);
    glTexParameteri(_textureType, GL_TEXTURE_MAX_LEVEL, 1000);
    glTexParameteri(_textureType, GL_TEXTURE_WRAP_S, GL_REPEAT);
    if(_pbtype != PB_TEXTURE_1D){
        glTexParameteri(_textureType, GL_TEXTURE_WRAP_T, GL_REPEAT);
    }
    if(_pbtype == PB_TEXTURE_3D){
        glTexParameteri(_textureType, GL_TEXTURE_WRAP_R, GL_REPEAT);
    }

    void *pixels = nullptr;

    switch(_dataType){
        default                : pixels = MemoryManager_NEW GLubyte[size];  break;
        case GL_FLOAT          : pixels = MemoryManager_NEW GLfloat[size];  break;
        case GL_UNSIGNED_INT   : pixels = MemoryManager_NEW GLuint[size];   break;
        case GL_UNSIGNED_SHORT : pixels = MemoryManager_NEW GLushort[size]; break;
        case GL_BYTE           : pixels = MemoryManager_NEW GLbyte[size];   break;
        case GL_SHORT          : pixels = MemoryManager_NEW GLshort[size];  break;
        case GL_INT            : pixels = MemoryManager_NEW GLint[size];    break;
    };

    memset(pixels, 0, size * sizeOf(_dataType) );

    switch(_pbtype){
        case PB_TEXTURE_1D:
            glTexImage1D(_textureType, 0, _internalFormat, _width, 0, _format, _dataType, pixels);
            break;
        case PB_TEXTURE_2D:
            glTexImage2D(_textureType, 0, _internalFormat, _width, _height, 0, _format, _dataType, pixels);
            break;
        case PB_TEXTURE_3D:
            glTexImage3D(_textureType, 0, _internalFormat, _width, _height,_depth, 0, _format, _dataType, pixels);
            break;
    };
    MemoryManager::DELETE_ARRAY( pixels );

    glGenBuffers(1, &_pixelBufferHandle);
    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, _pixelBufferHandle);
    glBufferData(GL_PIXEL_UNPACK_BUFFER, size * sizeOf(_dataType), NULL, GL_STREAM_DRAW);

    GL_API::unbindTexture(0, _textureType);
    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
    return true;
}

void glPixelBuffer::updatePixels(const GLfloat * const pixels) {
    GL_API::bindTexture(0, _textureId, _textureType);
    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, _pixelBufferHandle);
    switch(_pbtype){
        case PB_TEXTURE_1D:
            glBufferData(GL_PIXEL_UNPACK_BUFFER, (_width*4) * sizeOf(_dataType), NULL, GL_STREAM_DRAW);
            break;
        case PB_TEXTURE_2D:
            glBufferData(GL_PIXEL_UNPACK_BUFFER, (_width*_height*4) * sizeOf(_dataType), NULL, GL_STREAM_DRAW);
            break;
        case PB_TEXTURE_3D:
            glBufferData(GL_PIXEL_UNPACK_BUFFER, (_width*_height*_depth*4) * sizeOf(_dataType), NULL, GL_STREAM_DRAW);
            break;
    };

    GLfloat* ptr = (GLfloat*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
    if(ptr)    {
        memcpy(ptr, pixels, _width * _height * 4 * sizeOf(_dataType));
        glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // release the mapped buffer
    }

    GL_API::setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}

};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Buffers/PixelBuffer/glPixelBuffer.cpp

Diff revisions: vs.
Revision Author Commited Message
337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

334 IonutCava picture IonutCava Wed 10 Dec, 2014 17:21:22 +0000

[Ionut]
- Reorganized the Hardware project and renamed to "Platform"
- Reorganized the Server project