Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vbocontainer.cpp « Graphics « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0977a3e57a2b7a79ae753a39e177a52894f566b5 (plain)
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
//-----------------------------------------------------------------------------
//           Name: vbocontainer.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "vbocontainer.h"

#include <Graphics/graphics.h>
#include <Logging/logdata.h>
#include <Utility/assert.h>
#include <Internal/profiler.h>

#include <SDL_assert.h>

VBOContainer::VBOContainer():
    is_valid(false),
    storage(0)
{}

void VBOContainer::Fill(char flags, GLuint size, void* data) {
    if( size > 0  ) {
        LOG_ASSERT(data);

        Graphics* graphics = Graphics::Instance();
        GLenum target;
        if(flags & kVBOElement){
            target = GL_ELEMENT_ARRAY_BUFFER;
        } else if(flags & kVBOFloat){
            target = GL_ARRAY_BUFFER;
        } else {
            target = 0xFFFFFFFF;
            // No target flag set
            SDL_assert(false);
        }
        GLenum hint;
        if(flags & kVBOStatic){
            hint = GL_STATIC_DRAW;
        } else if(flags & kVBODynamic){
            hint = GL_DYNAMIC_DRAW;
        } else if(flags & kVBOStream){
            hint = GL_STREAM_DRAW;
        } else {
            hint = 0xFFFFFFFF;
            // No hint flag set
            SDL_assert(false);
        }
        if(flags & kVBOStatic || !is_valid){
            element = flags & kVBOElement;
            if(is_valid) {
                LOGS << "Disposing " << gl_VBO << std::endl;
                Dispose();
            }
            glGenBuffers( 1, &gl_VBO );  
            //LOGI << "Generated vbo: " << gl_VBO << std::endl;
            graphics->BindVBO(target, gl_VBO);
            glBufferData( target, size, data, hint );
            is_valid = true;
            storage = size;
        } else {
            graphics->BindVBO(target, gl_VBO);
            
            /*
             * Some early tests indicate that no BufferData orphaning and memory mapped transfers are
             * the most efficient combination for MacOSX intel.
             * Generally, i never expect BufferData orphaning in combination with mapping will ever be more efficient
             * as it tells the driver to invalidate or orphan the data twice.
             *
             * MacOSX Intel:
             *    kAlwaysOrphan = false
             *    kUseMapRange = true
             * MacOSX Nvidia:
             *    ....
             *    ....
             * Windows ATI:
             *    ....
             *    ....
             * Windows NVIDIA:
             *    ....
             *    ....
             * Windows Intel:
             *    ....
             *    ....
             * Linux NVIDIA:
             *    ....
             *    ....
             * Linux Intel:
             *    ....
             *    ....
             * Linux ATI:
             *    ....
             *    ....
             */
            const bool kAlwaysOrphan = false;
            bool orphaned = false;
            if(size > storage ) {
                LOGW << "Reallocating buffer because of resize" << std::endl;
                PROFILER_ZONE(g_profiler_ctx, "glBufferData");
                storage = size;
                glBufferData( target, storage, NULL, hint );
                orphaned = true;
            }
            
            //if( !(flags & kVBOForceReBufferData) ){
            if( orphaned == false && kAlwaysOrphan)
            {
                PROFILER_ZONE(g_profiler_ctx, "glBufferData orphan");
                glBufferData( target, storage, NULL, hint );
            }
            
            const bool kUseMapRange = true;
            if(kUseMapRange) {
                PROFILER_ZONE(g_profiler_ctx, "glMapBufferRange");
                //Using the Unsynchronized bit here seems incorrect.
                //Because we can't actually guarantee on the client side
                //that the GL environment isn't done with the buffer yet.
                //For this we have to do a ring buffer implementation instead.
                //Which we might do anyways..
                //void* mapped = glMapBufferRange(target, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT );
                void* mapped = glMapBufferRange(target, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_INVALIDATE_RANGE_BIT );
                memcpy(mapped, data, size);
                glUnmapBuffer(target);
            } else {
                PROFILER_ZONE(g_profiler_ctx, "glBufferSubData");
                glBufferSubData( target, 0, size, data);
            }
        }
        graphics->BindVBO(target, 0);
    }
    size_ = size;
}

void VBOContainer::Dispose() {
    if(!is_valid){
        return;
    }
    Graphics* graphics = Graphics::Instance();

    GLenum target;
    if( element ) {
        target = GL_ELEMENT_ARRAY_BUFFER;
    } else {
        target = GL_ARRAY_BUFFER;
    }

    //LOGI << "Disposing vbo: " << gl_VBO << " for target " << target << std::endl;
    LogSystem::Flush();
    graphics->UnbindVBO( target, gl_VBO );
    glDeleteBuffers( 1, &gl_VBO );
    is_valid = false;
    size_ = 0;
}

void VBOContainer::Bind() const {
    if(!is_valid){
        LOG_ASSERT(false);
        return;
    }

    if(element){
        Graphics::Instance()->BindElementVBO(gl_VBO);    
    } else {
        Graphics::Instance()->BindArrayVBO(gl_VBO);
    }
}

bool VBOContainer::valid() const {
    return is_valid;
}

unsigned VBOContainer::size() {
    return valid() ? size_ : 0;
}

uintptr_t VBOContainer::offset() const {
    return 0;
}

VBOContainer::~VBOContainer() {
    Dispose();
}