KiCad PCB EDA Suite
Loading...
Searching...
No Matches
render_3d_raytrace_gl.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2020 Mario Luzeiro <[email protected]>
5 * Copyright (C) 2024 Alex Shvartzkop <[email protected]>
6 * Copyright (C) 2015-2024 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <gal/opengl/kiglew.h> // Must be included first
27
28#include <algorithm>
29#include <atomic>
30#include <chrono>
31#include <thread>
32
34#include "../common_ogl/ogl_utils.h"
35#include <core/profile.h> // To use GetRunningMicroSecs or another profiling utility
36#include <wx/log.h>
37
38
40 RENDER_3D_RAYTRACE_BASE( aAdapter, aCamera )
41{
42 wxLogTrace( m_logTrace, wxT( "RENDER_3D_RAYTRACE_GL::RENDER_3D_RAYTRACE_GL" ) );
43
45 m_pboId = GL_NONE;
46 m_pboDataSize = 0;
47}
48
49
51{
52 deletePbo();
53}
54
55
57{
58 // Delete PBO if it was created
60 {
61 if( glIsBufferARB( m_pboId ) )
62 glDeleteBuffers( 1, &m_pboId );
63
64 m_pboId = GL_NONE;
65 }
66}
67
68
69void RENDER_3D_RAYTRACE_GL::SetCurWindowSize( const wxSize& aSize )
70{
71 if( m_windowSize != aSize )
72 {
73 m_windowSize = aSize;
74 glViewport( 0, 0, m_windowSize.x, m_windowSize.y );
75
76 initPbo();
77 }
78}
79
80
81bool RENDER_3D_RAYTRACE_GL::Redraw( bool aIsMoving, REPORTER* aStatusReporter,
82 REPORTER* aWarningReporter )
83{
84 bool requestRedraw = false;
85
86 // Initialize openGL if need
88 {
90
91 //aIsMoving = true;
92 requestRedraw = true;
93
94 // It will assign the first time the windows size, so it will now
95 // revert to preview mode the first time the Redraw is called
98 }
99
100 std::unique_ptr<BUSY_INDICATOR> busy = CreateBusyIndicator();
101
102 // Reload board if it was requested
104 {
105 if( aStatusReporter )
106 aStatusReporter->Report( _( "Loading..." ) );
107
108 //aIsMoving = true;
109 requestRedraw = true;
110 Reload( aStatusReporter, aWarningReporter, false );
111 }
112
113
114 // Recalculate constants if windows size was changed
116 {
118 aIsMoving = true;
119 requestRedraw = true;
120
122 }
123
124 // Clear buffers
125 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
126 glClearDepth( 1.0f );
127 glClearStencil( 0x00 );
128 glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
129
130 // 4-byte pixel alignment
131 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
132
133 glDisable( GL_STENCIL_TEST );
134 glDisable( GL_LIGHTING );
135 glDisable( GL_COLOR_MATERIAL );
136 glDisable( GL_DEPTH_TEST );
137 glDisable( GL_TEXTURE_2D );
138 glDisable( GL_BLEND );
139 glDisable( GL_MULTISAMPLE );
140
141 const bool was_camera_changed = m_camera.ParametersChanged();
142
143 if( requestRedraw || aIsMoving || was_camera_changed )
144 m_renderState = RT_RENDER_STATE_MAX; // Set to an invalid state,
145 // so it will restart again latter
146
147 // This will only render if need, otherwise it will redraw the PBO on the screen again
148 if( aIsMoving || was_camera_changed )
149 {
150 // Set head light (camera view light) with the opposite direction of the camera
151 if( m_cameraLight )
153
156
157 // Bind PBO
158 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboId );
159
160 // Get the PBO pixel pointer to write the data
161 uint8_t* ptrPBO = (uint8_t *)glMapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
162 GL_WRITE_ONLY_ARB );
163
164 if( ptrPBO )
165 {
166 renderPreview( ptrPBO );
167
168 // release pointer to mapping buffer, this initialize the coping to PBO
169 glUnmapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB );
170 }
171
172 glWindowPos2i( m_xoffset, m_yoffset );
173 }
174 else
175 {
176 // Bind PBO
177 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboId );
178
180 {
181 // Get the PBO pixel pointer to write the data
182 uint8_t* ptrPBO = (uint8_t *)glMapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB,
183 GL_WRITE_ONLY_ARB );
184
185 if( ptrPBO )
186 {
187 render( ptrPBO, aStatusReporter );
188
190 requestRedraw = true;
191
192 // release pointer to mapping buffer, this initialize the coping to PBO
193 glUnmapBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB );
194 }
195 }
196
198 {
199 glClear( GL_COLOR_BUFFER_BIT );
200 }
201
202 glWindowPos2i( m_xoffset, m_yoffset );
203 }
204
205 // This way it will blend the progress rendering with the last buffer. eg:
206 // if it was called after a openGL.
207 glEnable( GL_BLEND );
208 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
209 glEnable( GL_ALPHA_TEST );
210 glDrawPixels( m_realBufferSize.x, m_realBufferSize.y, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
211 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
212
213 return requestRedraw;
214}
215
216
218{
219 if( GLEW_ARB_pixel_buffer_object )
220 {
222
223 // Try to delete vbo if it was already initialized
224 deletePbo();
225
226 // Learn about Pixel buffer objects at:
227 // http://www.songho.ca/opengl/gl_pbo.html
228 // http://web.eecs.umich.edu/~sugih/courses/eecs487/lectures/25-PBO+Mipmapping.pdf
229 // "create 2 pixel buffer objects, you need to delete them when program exits.
230 // glBufferDataARB with NULL pointer reserves only memory space."
231
232 // This sets the number of RGBA pixels
234
235 glGenBuffersARB( 1, &m_pboId );
236 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboId );
237 glBufferDataARB( GL_PIXEL_UNPACK_BUFFER_ARB, m_pboDataSize, 0, GL_STREAM_DRAW_ARB );
238 glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
239
240 wxLogTrace( m_logTrace,
241 wxT( "RENDER_3D_RAYTRACE_GL:: GLEW_ARB_pixel_buffer_object is supported" ) );
242 }
243}
Helper class to handle information needed to display 3D board.
Definition: board_adapter.h:73
SFVEC4F m_BgColorTop
background top color
SFVEC4F m_BgColorBot
background bottom color
A class used to derive camera objects from.
Definition: camera.h:103
const SFVEC3F & GetDir() const
Definition: camera.h:135
bool ParametersChanged()
Definition: camera.cpp:729
void SetDirection(const SFVEC3F &aDir)
Set directional light orientation.
Definition: light.h:131
Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:51
CAMERA & m_camera
Flag if the canvas specific for this render was already initialized.
std::unique_ptr< BUSY_INDICATOR > CreateBusyIndicator() const
Return a created busy indicator, if a factory has been set, else a null pointer.
bool m_reloadRequested
The window size that this camera is working.
BOARD_ADAPTER & m_boardAdapter
static SFVEC4F premultiplyAlpha(const SFVEC4F &aInput)
void Reload(REPORTER *aStatusReporter, REPORTER *aWarningReporter, bool aOnlyLoadCopperAndShapes)
void render(uint8_t *ptrPBO, REPORTER *aStatusReporter)
DIRECTIONAL_LIGHT * m_cameraLight
RT_RENDER_STATE m_renderState
State used on quality render.
wxSize m_oldWindowsSize
Encode Morton code positions.
void renderPreview(uint8_t *ptrPBO)
RENDER_3D_RAYTRACE_GL(EDA_3D_CANVAS *aCanvas, BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
bool Redraw(bool aIsMoving, REPORTER *aStatusReporter, REPORTER *aWarningReporter) override
Redraw the view.
void SetCurWindowSize(const wxSize &aSize) override
Before each render, the canvas will tell the render what is the size of its windows,...
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
#define _(s)
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
void OglDrawBackground(const SFVEC4F &aTopColor, const SFVEC4F &aBotColor)
Definition: ogl_utils.cpp:185
@ RT_RENDER_STATE_FINISH
@ RT_RENDER_STATE_MAX