1 /*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright 2013-2017 CERN
5 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <maciej.suminski@cern.ch>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27 #include <gal/opengl/gpu_manager.h>
28 #include <gal/opengl/cached_container_gpu.h>
29 #include <gal/opengl/cached_container_ram.h>
30 #include <gal/opengl/noncached_container.h>
31 #include <gal/opengl/shader.h>
32 #include <gal/opengl/utils.h>
33 #include <gal/opengl/vertex_item.h>
34
35 #include <profile.h>
36
37 #include <typeinfo>
38 #include <confirm.h>
39 #include <trace_helpers.h>
40
41 #ifdef KICAD_GAL_PROFILE
42 #include <profile.h>
43 #include <wx/log.h>
44 #endif /* KICAD_GAL_PROFILE */
45
46 using namespace KIGFX;
47
MakeManager(VERTEX_CONTAINER * aContainer)48 GPU_MANAGER* GPU_MANAGER::MakeManager( VERTEX_CONTAINER* aContainer )
49 {
50 if( aContainer->IsCached() )
51 return new GPU_CACHED_MANAGER( aContainer );
52 else
53 return new GPU_NONCACHED_MANAGER( aContainer );
54 }
55
56
GPU_MANAGER(VERTEX_CONTAINER * aContainer)57 GPU_MANAGER::GPU_MANAGER( VERTEX_CONTAINER* aContainer ) :
58 m_isDrawing( false ),
59 m_container( aContainer ),
60 m_shader( nullptr ),
61 m_shaderAttrib( 0 ),
62 m_enableDepthTest( true )
63 {
64 }
65
66
~GPU_MANAGER()67 GPU_MANAGER::~GPU_MANAGER()
68 {
69 }
70
71
SetShader(SHADER & aShader)72 void GPU_MANAGER::SetShader( SHADER& aShader )
73 {
74 m_shader = &aShader;
75 m_shaderAttrib = m_shader->GetAttribute( "attrShaderParams" );
76
77 if( m_shaderAttrib == -1 )
78 {
79 DisplayError( nullptr, wxT( "Could not get the shader attribute location" ) );
80 }
81 }
82
83
84 // Cached manager
GPU_CACHED_MANAGER(VERTEX_CONTAINER * aContainer)85 GPU_CACHED_MANAGER::GPU_CACHED_MANAGER( VERTEX_CONTAINER* aContainer ) :
86 GPU_MANAGER( aContainer ),
87 m_buffersInitialized( false ),
88 m_indicesCapacity( 0 ),
89 m_totalHuge( 0 ),
90 m_totalNormal( 0 ),
91 m_indexBufSize( 0 ),
92 m_indexBufMaxSize( 0 ),
93 m_curVrangeSize( 0 )
94 {
95 }
96
97
~GPU_CACHED_MANAGER()98 GPU_CACHED_MANAGER::~GPU_CACHED_MANAGER()
99 {
100 }
101
102
BeginDrawing()103 void GPU_CACHED_MANAGER::BeginDrawing()
104 {
105 wxASSERT( !m_isDrawing );
106
107 m_curVrangeSize = 0;
108 m_indexBufMaxSize = 0;
109 m_indexBufSize = 0;
110 m_vranges.clear();
111
112 m_isDrawing = true;
113 }
114
115
DrawIndices(const VERTEX_ITEM * aItem)116 void GPU_CACHED_MANAGER::DrawIndices( const VERTEX_ITEM *aItem )
117 {
118 wxASSERT( m_isDrawing );
119
120 unsigned int offset = aItem->GetOffset();
121 unsigned int size = aItem->GetSize();
122
123 if( size > 1000 )
124 {
125 m_totalHuge += size;
126 m_vranges.emplace_back( offset, offset + size - 1, true );
127 m_indexBufSize = std::max( m_curVrangeSize, m_indexBufSize );
128 m_curVrangeSize = 0;
129 }
130 else if ( size > 0 )
131 {
132 m_totalNormal += size;
133 m_vranges.emplace_back( offset, offset + size - 1, false );
134 m_curVrangeSize += size;
135 }
136 }
137
138
EndDrawing()139 void GPU_CACHED_MANAGER::EndDrawing()
140 {
141 wxASSERT( m_isDrawing );
142
143 CACHED_CONTAINER* cached = static_cast<CACHED_CONTAINER*>( m_container );
144
145 if( cached->IsMapped() )
146 cached->Unmap();
147
148 m_indexBufSize = std::max( m_curVrangeSize, m_indexBufSize );
149 m_indexBufMaxSize = std::max( 2*m_indexBufSize, m_indexBufMaxSize );
150
151 resizeIndices( m_indexBufMaxSize );
152
153 if( m_enableDepthTest )
154 glEnable( GL_DEPTH_TEST );
155 else
156 glDisable( GL_DEPTH_TEST );
157
158 // Prepare buffers
159 glEnableClientState( GL_VERTEX_ARRAY );
160 glEnableClientState( GL_COLOR_ARRAY );
161
162 // Bind vertices data buffers
163 glBindBuffer( GL_ARRAY_BUFFER, cached->GetBufferHandle() );
164 glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, (GLvoid*) COORD_OFFSET );
165 glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, (GLvoid*) COLOR_OFFSET );
166
167 if( m_shader != nullptr ) // Use shader if applicable
168 {
169 m_shader->Use();
170 glEnableVertexAttribArray( m_shaderAttrib );
171 glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE, VERTEX_SIZE,
172 (GLvoid*) SHADER_OFFSET );
173 }
174
175 PROF_TIMER cntDraw( "gl-draw-elements" );
176
177 int n_ranges = m_vranges.size();
178 int n = 0;
179 GLuint* iptr = m_indices.get();
180 GLuint icnt = 0;
181
182 int drawCalls = 0;
183
184 while( n < n_ranges )
185 {
186 VRANGE* cur = &m_vranges[n];
187
188 if( cur->m_isContinuous )
189 {
190 if( icnt > 0 )
191 {
192 glDrawElements( GL_TRIANGLES, icnt, GL_UNSIGNED_INT, m_indices.get() );
193 drawCalls++;
194 }
195
196 icnt = 0;
197 iptr = m_indices.get();
198
199 glDrawArrays( GL_TRIANGLES, cur->m_start, cur->m_end - cur->m_start + 1 );
200 drawCalls++;
201 }
202 else
203 {
204 for( GLuint i = cur->m_start; i <= cur->m_end; i++ )
205 {
206 *iptr++ = i;
207 icnt++;
208 }
209 }
210 n++;
211 }
212
213 if( icnt > 0 )
214 {
215 glDrawElements( GL_TRIANGLES, icnt, GL_UNSIGNED_INT, m_indices.get() );
216 drawCalls++;
217 }
218
219 cntDraw.Stop();
220
221 KI_TRACE( traceGalProfile,
222 "Cached manager size: VBO size %u iranges %llu max elt size %u drawcalls %u\n",
223 cached->AllItemsSize(), m_vranges.size(), m_indexBufMaxSize, drawCalls );
224 KI_TRACE( traceGalProfile, "Timing: %s\n", cntDraw.to_string() );
225
226 glBindBuffer( GL_ARRAY_BUFFER, 0 );
227 cached->ClearDirty();
228
229 // Deactivate vertex array
230 glDisableClientState( GL_COLOR_ARRAY );
231 glDisableClientState( GL_VERTEX_ARRAY );
232
233 if( m_shader != nullptr )
234 {
235 glDisableVertexAttribArray( m_shaderAttrib );
236 m_shader->Deactivate();
237 }
238
239 m_isDrawing = false;
240 }
241
242
resizeIndices(unsigned int aNewSize)243 void GPU_CACHED_MANAGER::resizeIndices( unsigned int aNewSize )
244 {
245 if( aNewSize > m_indicesCapacity )
246 {
247 m_indicesCapacity = aNewSize;
248 m_indices.reset( new GLuint[m_indicesCapacity] );
249 }
250 }
251
252
253 // Noncached manager
GPU_NONCACHED_MANAGER(VERTEX_CONTAINER * aContainer)254 GPU_NONCACHED_MANAGER::GPU_NONCACHED_MANAGER( VERTEX_CONTAINER* aContainer ) :
255 GPU_MANAGER( aContainer )
256 {
257 }
258
259
BeginDrawing()260 void GPU_NONCACHED_MANAGER::BeginDrawing()
261 {
262 // Nothing has to be prepared
263 }
264
265
DrawIndices(const VERTEX_ITEM * aItem)266 void GPU_NONCACHED_MANAGER::DrawIndices( const VERTEX_ITEM* aItem )
267 {
268 wxASSERT_MSG( false, wxT( "Not implemented yet" ) );
269 }
270
271
EndDrawing()272 void GPU_NONCACHED_MANAGER::EndDrawing()
273 {
274 #ifdef KICAD_GAL_PROFILE
275 PROF_COUNTER totalRealTime;
276 #endif /* KICAD_GAL_PROFILE */
277
278 if( m_container->GetSize() == 0 )
279 return;
280
281 VERTEX* vertices = m_container->GetAllVertices();
282 GLfloat* coordinates = (GLfloat*) ( vertices );
283 GLubyte* colors = (GLubyte*) ( vertices ) + COLOR_OFFSET;
284
285 if( m_enableDepthTest )
286 glEnable( GL_DEPTH_TEST );
287 else
288 glDisable( GL_DEPTH_TEST );
289
290 // Prepare buffers
291 glEnableClientState( GL_VERTEX_ARRAY );
292 glEnableClientState( GL_COLOR_ARRAY );
293
294 glVertexPointer( COORD_STRIDE, GL_FLOAT, VERTEX_SIZE, coordinates );
295 glColorPointer( COLOR_STRIDE, GL_UNSIGNED_BYTE, VERTEX_SIZE, colors );
296
297 if( m_shader != nullptr ) // Use shader if applicable
298 {
299 GLfloat* shaders = (GLfloat*) ( vertices ) + SHADER_OFFSET / sizeof( GLfloat );
300
301 m_shader->Use();
302 glEnableVertexAttribArray( m_shaderAttrib );
303 glVertexAttribPointer( m_shaderAttrib, SHADER_STRIDE, GL_FLOAT, GL_FALSE, VERTEX_SIZE,
304 shaders );
305 }
306
307 glDrawArrays( GL_TRIANGLES, 0, m_container->GetSize() );
308
309 #ifdef KICAD_GAL_PROFILE
310 wxLogTrace( traceGalProfile, wxT( "Noncached manager size: %d" ), m_container->GetSize() );
311 #endif /* KICAD_GAL_PROFILE */
312
313 // Deactivate vertex array
314 glDisableClientState( GL_COLOR_ARRAY );
315 glDisableClientState( GL_VERTEX_ARRAY );
316
317 if( m_shader != nullptr )
318 {
319 glDisableVertexAttribArray( m_shaderAttrib );
320 m_shader->Deactivate();
321 }
322
323 m_container->Clear();
324
325 #ifdef KICAD_GAL_PROFILE
326 totalRealTime.Stop();
327 wxLogTrace( traceGalProfile, wxT( "GPU_NONCACHED_MANAGER::EndDrawing(): %.1f ms" ),
328 totalRealTime.msecs() );
329 #endif /* KICAD_GAL_PROFILE */
330 }
331
EnableDepthTest(bool aEnabled)332 void GPU_MANAGER::EnableDepthTest( bool aEnabled )
333 {
334 m_enableDepthTest = aEnabled;
335 }
336