gpu_manager.h 4.62 KB
Newer Older
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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file gpu_manager.h
 * @brief Class to handle uploading vertices and indices to GPU in drawing purposes.
 */

#ifndef GPU_MANAGER_H_
#define GPU_MANAGER_H_

#include <gal/opengl/vertex_common.h>
#include <boost/scoped_array.hpp>

36
namespace KIGFX
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
{
class SHADER;
class VERTEX_CONTAINER;
class CACHED_CONTAINER;
class NONCACHED_CONTAINER;

class GPU_MANAGER
{
public:
    static GPU_MANAGER* MakeManager( VERTEX_CONTAINER* aContainer );

    virtual ~GPU_MANAGER();

    /**
     * @brief Initializes everything needed to use vertex buffer objects (should be called when
     * there is an OpenGL context available).
     */
    virtual void Initialize() = 0;

    /**
     * Function BeginDrawing()
     * Prepares the stored data to be drawn.
     */
    virtual void BeginDrawing() = 0;

    /**
     * Function DrawIndices()
     * Makes the GPU draw given range of vertices.
     * @param aOffset is the beginning of the range.
     * @param aSize is the number of vertices to be drawn.
     */
    virtual void DrawIndices( unsigned int aOffset, unsigned int aSize ) = 0;

    /**
     * Function DrawIndices()
     * Makes the GPU draw all the vertices stored in the container.
     */
    virtual void DrawAll() = 0;

    /**
     * Function EndDrawing()
     * Clears the container after drawing routines.
     */
    virtual void EndDrawing() = 0;

    /**
     * Function SetShader()
     * Allows using shaders with the stored data.
     * @param aShader is the object that allows using shaders.
     */
    virtual void SetShader( SHADER& aShader );

protected:
    GPU_MANAGER( VERTEX_CONTAINER* aContainer );

Maciej Suminski's avatar
Maciej Suminski committed
92
    ///> Drawing status flag.
93
    bool m_isDrawing;
94

Maciej Suminski's avatar
Maciej Suminski committed
95
    ///> Container that stores vertices data.
96 97
    VERTEX_CONTAINER* m_container;

Maciej Suminski's avatar
Maciej Suminski committed
98
    ///> Shader handling
99
    SHADER* m_shader;
Maciej Suminski's avatar
Maciej Suminski committed
100 101 102

    ///> Location of shader attributes (for glVertexAttribPointer)
    int m_shaderAttrib;
103 104 105 106 107 108 109 110 111
};


class GPU_CACHED_MANAGER : public GPU_MANAGER
{
public:
    GPU_CACHED_MANAGER( VERTEX_CONTAINER* aContainer );
    ~GPU_CACHED_MANAGER();

Maciej Suminski's avatar
Maciej Suminski committed
112
    ///> @copydoc GPU_MANAGER::Initialize()
113 114
    virtual void Initialize();

Maciej Suminski's avatar
Maciej Suminski committed
115
    ///> @copydoc GPU_MANAGER::BeginDrawing()
116 117
    virtual void BeginDrawing();

Maciej Suminski's avatar
Maciej Suminski committed
118
    ///> @copydoc GPU_MANAGER::DrawIndices()
119 120
    virtual void DrawIndices( unsigned int aOffset, unsigned int aSize );

Maciej Suminski's avatar
Maciej Suminski committed
121
    ///> @copydoc GPU_MANAGER::DrawAll()
122 123
    virtual void DrawAll();

Maciej Suminski's avatar
Maciej Suminski committed
124
    ///> @copydoc GPU_MANAGER::EndDrawing()
125 126 127 128 129 130 131 132 133 134
    virtual void EndDrawing();

    /**
     * Function uploadToGpu
     * Rebuilds vertex buffer object using stored VERTEX_ITEMs and sends it to the graphics card
     * memory.
     */
    virtual void uploadToGpu();

protected:
Maciej Suminski's avatar
Maciej Suminski committed
135
    ///> Buffers initialization flag
136
    bool m_buffersInitialized;
Maciej Suminski's avatar
Maciej Suminski committed
137 138

    ///> Pointer to the current indices buffer
139
    boost::scoped_array<GLuint> m_indices;
Maciej Suminski's avatar
Maciej Suminski committed
140 141

    ///> Pointer to the first free cell in the indices buffer
142
    GLuint* m_indicesPtr;
Maciej Suminski's avatar
Maciej Suminski committed
143 144

    ///> Handle to vertices buffer
145
    GLuint  m_verticesBuffer;
Maciej Suminski's avatar
Maciej Suminski committed
146 147

    ///> Number of indices stored in the indices buffer
148
    unsigned int m_indicesSize;
149 150 151 152 153 154 155 156
};


class GPU_NONCACHED_MANAGER : public GPU_MANAGER
{
public:
    GPU_NONCACHED_MANAGER( VERTEX_CONTAINER* aContainer );

Maciej Suminski's avatar
Maciej Suminski committed
157
    ///> @copydoc GPU_MANAGER::Initialize()
158 159
    virtual void Initialize();

Maciej Suminski's avatar
Maciej Suminski committed
160
    ///> @copydoc GPU_MANAGER::BeginDrawing()
161 162
    virtual void BeginDrawing();

Maciej Suminski's avatar
Maciej Suminski committed
163
    ///> @copydoc GPU_MANAGER::DrawIndices()
164 165
    virtual void DrawIndices( unsigned int aOffset, unsigned int aSize );

Maciej Suminski's avatar
Maciej Suminski committed
166
    ///> @copydoc GPU_MANAGER::DrawAll()
167 168
    virtual void DrawAll();

Maciej Suminski's avatar
Maciej Suminski committed
169
    ///> @copydoc GPU_MANAGER::EndDrawing()
170 171
    virtual void EndDrawing();
};
172
} // namespace KIGFX
173
#endif /* GPU_MANAGER_H_ */