KiCad PCB EDA Suite
Loading...
Searching...
No Matches
render_3d_raytrace_base.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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <algorithm>
26#include <atomic>
27#include <chrono>
28#include <thread>
29
31#include "mortoncodes.h"
32#include "../color_rgba.h"
33#include "3d_fastmath.h"
34#include "3d_math.h"
35#include <thread_pool.h>
36#include <core/profile.h> // To use GetRunningMicroSecs or another profiling utility
37#include <wx/log.h>
38
39
40#ifdef USE_SRGB_SPACE
41
43#define SRGB_GAMA 2.4f
44
45
46// This function implements the conversion from linear RGB to sRGB
47// https://github.com/g-truc/glm/blob/master/glm/gtc/color_space.inl#L12
48static SFVEC3F convertLinearToSRGB( const SFVEC3F& aRGBcolor )
49{
50 const float gammaCorrection = 1.0f / SRGB_GAMA;
51 const SFVEC3F clampedColor = glm::clamp( aRGBcolor, SFVEC3F( 0.0f ), SFVEC3F( 1.0f ) );
52
53 return glm::mix( glm::pow( clampedColor, SFVEC3F(gammaCorrection) ) * 1.055f - 0.055f,
54 clampedColor * 12.92f,
55 glm::lessThan( clampedColor, SFVEC3F(0.0031308f) ) );
56}
57
58
59static SFVEC4F convertLinearToSRGBA( const SFVEC4F& aRGBAcolor )
60{
61 return SFVEC4F( convertLinearToSRGB( SFVEC3F( aRGBAcolor ) ), aRGBAcolor.a );
62}
63
64
65// This function implements the conversion from sRGB to linear RGB
66// https://github.com/g-truc/glm/blob/master/glm/gtc/color_space.inl#L35
68{
69 const float gammaCorrection = SRGB_GAMA;
70
71 return glm::mix( glm::pow( ( aSRGBcolor + SFVEC3F( 0.055f ) )
72 * SFVEC3F( 0.94786729857819905213270142180095f ),
73 SFVEC3F( gammaCorrection ) ),
74 aSRGBcolor * SFVEC3F( 0.07739938080495356037151702786378f ),
75 glm::lessThanEqual( aSRGBcolor, SFVEC3F( 0.04045f ) ) );
76}
77
78
80{
81 return SFVEC4F( ConvertSRGBToLinear( SFVEC3F( aSRGBAcolor ) ), aSRGBAcolor.a );
82}
83
84#endif
85
86
88 RENDER_3D_BASE( aAdapter, aCamera ),
89 m_postShaderSsao( aCamera )
90{
91 wxLogTrace( m_logTrace, wxT( "RENDER_3D_RAYTRACE_BASE::RENDER_3D_RAYTRACE_BASE" ) );
92
93 //m_pboId = GL_NONE;
94 //m_pboDataSize = 0;
95 m_accelerator = nullptr;
98 m_oldWindowsSize.x = 0;
99 m_oldWindowsSize.y = 0;
100 m_outlineBoard2dObjects = nullptr;
102 m_firstHitinfo = nullptr;
103 m_shaderBuffer = nullptr;
104 m_cameraLight = nullptr;
105
106 m_xoffset = 0;
107 m_yoffset = 0;
108
110 m_isPreview = false;
111 m_renderState = RT_RENDER_STATE_MAX; // Set to an initial invalid state
114}
115
116
118{
119 wxLogTrace( m_logTrace, wxT( "RENDER_3D_RAYTRACE_BASE::~RENDER_3D_RAYTRACE_BASE" ) );
120
121 delete m_accelerator;
122 m_accelerator = nullptr;
123
125 m_outlineBoard2dObjects = nullptr;
126
129
130 delete[] m_shaderBuffer;
131 m_shaderBuffer = nullptr;
132}
133
134
136{
137 return 200; // ms
138}
139
140
142{
144
147
148 m_postShaderSsao.InitFrame();
149
151
152 // Mark the blocks not processed yet
153 std::fill( m_blockPositionsWasProcessed.begin(), m_blockPositionsWasProcessed.end(), 0 );
154}
155
156
157static inline void SetPixel( uint8_t* p, const COLOR_RGBA& v )
158{
159 p[0] = v.c[0];
160 p[1] = v.c[1];
161 p[2] = v.c[2];
162 p[3] = v.c[3];
163}
164
165
166static void SetPixelSRGBA( uint8_t* p, const COLOR_RGBA& v )
167{
168 SFVEC4F color = v;
169
170#ifdef USE_SRGB_SPACE
171 color = convertLinearToSRGB( color );
172#endif
173
174 COLOR_RGBA rgba( color );
175 SetPixel( p, rgba );
176}
177
178
180{
181 return SFVEC4F( aInput.r * aInput.a, aInput.g * aInput.a, aInput.b * aInput.a, aInput.a );
182}
183
184
185void RENDER_3D_RAYTRACE_BASE::render( uint8_t* ptrPBO, REPORTER* aStatusReporter )
186{
188 {
190
191 if( m_cameraLight )
192 m_cameraLight->SetDirection( -m_camera.GetDir() );
193
194 if( m_boardAdapter.m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
195 {
196 // Set all pixels of PBO transparent (Alpha to 0)
197 // This way it will draw the full buffer but only shows the updated (
198 // already calculated) squares
199 unsigned int nPixels = m_realBufferSize.x * m_realBufferSize.y;
200 uint8_t* tmp_ptrPBO = ptrPBO + 3; // PBO is RGBA
201
202 for( unsigned int i = 0; i < nPixels; ++i )
203 {
204 *tmp_ptrPBO = 0;
205 tmp_ptrPBO += 4; // PBO is RGBA
206 }
207 }
208
213 }
214
215 switch( m_renderState )
216 {
218 renderTracing( ptrPBO, aStatusReporter );
219 break;
220
222 postProcessShading( ptrPBO, aStatusReporter );
223 break;
224
226 postProcessBlurFinish( ptrPBO, aStatusReporter );
227 break;
228
229 default:
230 wxASSERT_MSG( false, wxT( "Invalid state on m_renderState" ) );
232 break;
233 }
234
235 if( aStatusReporter && ( m_renderState == RT_RENDER_STATE_FINISH ) )
236 {
237 // Calculation time in seconds
238 const double elapsed_time = (double) ( GetRunningMicroSecs() - m_renderStartTime ) / 1e6;
239
240 aStatusReporter->Report( wxString::Format( _( "Rendering time %.3f s" ), elapsed_time ) );
241 }
242}
243
244
245void RENDER_3D_RAYTRACE_BASE::renderTracing( uint8_t* ptrPBO, REPORTER* aStatusReporter )
246{
247 m_isPreview = false;
248
249 auto startTime = std::chrono::steady_clock::now();
250 std::atomic<size_t> numBlocksRendered( 0 );
251 std::atomic<size_t> currentBlock( 0 );
252
254 const int timeLimit = m_blockPositions.size() > 40000 ? 750 : 400;
255
256 auto processBlocks =
257 [&]()
258 {
259 for( size_t iBlock = currentBlock.fetch_add( 1 );
260 iBlock < m_blockPositions.size();
261 iBlock = currentBlock.fetch_add( 1 ) )
262 {
263 if( !m_blockPositionsWasProcessed[iBlock] )
264 {
265 renderBlockTracing( ptrPBO, iBlock );
267 numBlocksRendered++;
268 }
269
270 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(
271 std::chrono::steady_clock::now() - startTime );
272
273 if( diff.count() > timeLimit )
274 break;
275 }
276 };
277
278 BS::multi_future<void> futures;
279
280 for( size_t i = 0; i < tp.get_thread_count(); ++i )
281 futures.push_back( tp.submit_task( processBlocks ) );
282
283 futures.wait();
284
285 m_blockRenderProgressCount += numBlocksRendered;
286
287 if( aStatusReporter )
288 aStatusReporter->Report( wxString::Format( _( "Rendering: %.0f %%" ),
289 (float) ( m_blockRenderProgressCount * 100 )
290 / (float) m_blockPositions.size() ) );
291
292 // Check if it finish the rendering and if should continue to a post processing
293 // or mark it as finished
295 {
296 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
298 else
300 }
301}
302
303
304void RENDER_3D_RAYTRACE_BASE::renderFinalColor( uint8_t* ptrPBO, const SFVEC4F& rgbColor,
305 bool applyColorSpaceConversion )
306{
307 SFVEC4F color = rgbColor;
308
309#ifdef USE_SRGB_SPACE
311 // if( applyColorSpaceConversion )
312 // rgbColor = glm::convertLinearToSRGB( rgbColor );
313
314 if( applyColorSpaceConversion )
315 color = convertLinearToSRGB( rgbColor );
316#endif
317
318 ptrPBO[0] = (unsigned int) glm::clamp( (int) ( color.r * 255 ), 0, 255 );
319 ptrPBO[1] = (unsigned int) glm::clamp( (int) ( color.g * 255 ), 0, 255 );
320 ptrPBO[2] = (unsigned int) glm::clamp( (int) ( color.b * 255 ), 0, 255 );
321 ptrPBO[3] = (unsigned int) glm::clamp( (int) ( color.a * 255 ), 0, 255 );
322}
323
324
325static void HITINFO_PACKET_init( HITINFO_PACKET* aHitPacket )
326{
327 // Initialize hitPacket with a "not hit" information
328 for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
329 {
330 aHitPacket[i].m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
331 aHitPacket[i].m_HitInfo.m_acc_node_info = 0;
332 aHitPacket[i].m_hitresult = false;
333 aHitPacket[i].m_HitInfo.m_HitNormal = SFVEC3F( 0.0f );
334 aHitPacket[i].m_HitInfo.m_ShadowFactor = 1.0f;
335 }
336}
337
338
339void RENDER_3D_RAYTRACE_BASE::renderRayPackets( const SFVEC4F* bgColorY, const RAY* aRayPkt,
340 HITINFO_PACKET* aHitPacket, bool is_testShadow,
341 SFVEC4F* aOutHitColor )
342{
343 for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
344 {
345 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
346 {
347 if( aHitPacket[i].m_hitresult == true )
348 {
349 aOutHitColor[i] = shadeHit( bgColorY[y], aRayPkt[i], aHitPacket[i].m_HitInfo,
350 false, 0, is_testShadow );
351 }
352 else
353 {
354 aOutHitColor[i] = bgColorY[y];
355 }
356 }
357 }
358}
359
360
362 const HITINFO_PACKET* aHitPck_X0Y0,
363 const HITINFO_PACKET* aHitPck_AA_X1Y1,
364 const RAY* aRayPck, SFVEC4F* aOutHitColor )
365{
366 const bool is_testShadow = m_boardAdapter.m_Cfg->m_Render.raytrace_shadows;
367
368 for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
369 {
370 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
371 {
372 const RAY& rayAA = aRayPck[i];
373
374 HITINFO hitAA;
375 hitAA.m_tHit = std::numeric_limits<float>::infinity();
376 hitAA.m_acc_node_info = 0;
377
378 bool hitted = false;
379
380 const unsigned int idx0y1 = ( x + 0 ) + RAYPACKET_DIM * ( y + 1 );
381 const unsigned int idx1y1 = ( x + 1 ) + RAYPACKET_DIM * ( y + 1 );
382
383 // Gets the node info from the hit.
384 const unsigned int nodex0y0 = aHitPck_X0Y0[ i ].m_HitInfo.m_acc_node_info;
385 const unsigned int node_AA_x0y0 = aHitPck_AA_X1Y1[ i ].m_HitInfo.m_acc_node_info;
386
387 unsigned int nodex1y0 = 0;
388
389 if( x < ( RAYPACKET_DIM - 1 ) )
390 nodex1y0 = aHitPck_X0Y0[i + 1].m_HitInfo.m_acc_node_info;
391
392 unsigned int nodex0y1 = 0;
393
394 if( y < ( RAYPACKET_DIM - 1 ) && idx0y1 < RAYPACKET_RAYS_PER_PACKET )
395 nodex0y1 = aHitPck_X0Y0[idx0y1].m_HitInfo.m_acc_node_info;
396
397 unsigned int nodex1y1 = 0;
398
399 if( ( x < ( RAYPACKET_DIM - 1 ) )
400 && ( y < ( RAYPACKET_DIM - 1 ) )
401 && idx1y1 < RAYPACKET_RAYS_PER_PACKET )
402 {
403 nodex1y1 = aHitPck_X0Y0[idx1y1].m_HitInfo.m_acc_node_info;
404 }
405
406 // If all nodes are equal we assume there was no change on the object hits.
407 if( ( nodex0y0 != nodex1y0 && nodex1y0 != 0 )
408 || ( nodex0y0 != nodex0y1 && nodex0y1 != 0 )
409 || ( nodex0y0 != nodex1y1 && nodex1y1 != 0 )
410 || ( nodex0y0 != node_AA_x0y0 ) )
411 {
412 // Try to intersect the different nodes
413 // It tests the possible combination of hitted or not hitted points
414 // This will try to get the best hit for this ray
415
416 if( nodex0y0 != 0 )
417 hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex0y0 );
418
419 if( nodex1y0 != 0
420 && nodex0y0 != nodex1y0 )
421 {
422 hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex1y0 );
423 }
424
425 if( nodex0y1 != 0
426 && nodex0y0 != nodex0y1
427 && nodex1y0 != nodex0y1 )
428 {
429 hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex0y1 );
430 }
431
432 if( nodex1y1 != 0
433 && nodex0y0 != nodex1y1
434 && nodex0y1 != nodex1y1
435 && nodex1y0 != nodex1y1 )
436 {
437 hitted |= m_accelerator->Intersect( rayAA, hitAA, nodex1y1 );
438 }
439
440 if( node_AA_x0y0 != 0
441 && nodex0y0 != node_AA_x0y0
442 && nodex0y1 != node_AA_x0y0
443 && nodex1y0 != node_AA_x0y0
444 && nodex1y1 != node_AA_x0y0 )
445 {
446 hitted |= m_accelerator->Intersect( rayAA, hitAA, node_AA_x0y0 );
447 }
448
449 if( hitted )
450 {
451 // If we got any result, shade it
452 aOutHitColor[i] = shadeHit( aBgColorY[y], rayAA, hitAA, false, 0, is_testShadow );
453 }
454 else
455 {
456 // Note: There are very few cases that will end on this situation
457 // so it is not so expensive to trace a single ray from the beginning
458
459 // It was missed the 'last nodes' so, trace a ray from the beginning
460 if( m_accelerator->Intersect( rayAA, hitAA ) )
461 aOutHitColor[i] = shadeHit( aBgColorY[y], rayAA, hitAA, false, 0, is_testShadow );
462 }
463 }
464 }
465 }
466}
467
468
469#define DISP_FACTOR 0.075f
470
471
472void RENDER_3D_RAYTRACE_BASE::renderBlockTracing( uint8_t* ptrPBO, signed int iBlock )
473{
474 // Initialize ray packets
475 const SFVEC2UI& blockPos = m_blockPositions[iBlock];
476 const SFVEC2I blockPosI = SFVEC2I( blockPos.x + m_xoffset, blockPos.y + m_yoffset );
477 const SFVEC2F randDisp = ( m_camera.GetProjection() == PROJECTION_TYPE::ORTHO ) ?
478 SFVEC2F( 0.0f, 0.0f ) :
480
481 RAYPACKET blockPacket( m_camera, (SFVEC2F) blockPosI + randDisp,
482 randDisp /* Displacement random factor */ );
483
484
486
487 HITINFO_PACKET_init( hitPacket_X0Y0 );
488
489 // Calculate background gradient color
490 SFVEC4F bgColor[RAYPACKET_DIM];// Store a vertical gradient color
491
492 for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
493 {
494 const float posYfactor = (float) ( blockPosI.y + y ) / (float) m_windowSize.y;
495
496 bgColor[y] = m_backgroundColorTop * SFVEC4F(posYfactor) +
497 m_backgroundColorBottom * ( SFVEC4F(1.0f) - SFVEC4F(posYfactor) );
498 }
499
500 // Intersect ray packets (calculate the intersection with rays and objects)
501 if( !m_accelerator->Intersect( blockPacket, hitPacket_X0Y0 ) )
502 {
503 // If block is empty then set shades and continue
504 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
505 {
506 for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
507 {
508 const SFVEC4F& outColor = bgColor[y];
509
510 const unsigned int yBlockPos = blockPos.y + y;
511
512 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x )
513 {
514 m_postShaderSsao.SetPixelData( blockPos.x + x, yBlockPos,
515 SFVEC3F( 0.0f ), outColor,
516 SFVEC3F( 0.0f ), 0, 1.0f );
517 }
518 }
519 }
520
521 // This will set the output color to be displayed
522 // If post processing is enabled, it will not reflect the final result (as the final
523 // color will be computed on post processing) but it is used for report progress
524 const bool isFinalColor = !m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing;
525
526 for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
527 {
528 const SFVEC4F& outColor = bgColor[y];
529
530 const unsigned int yConst = blockPos.x + ( ( y + blockPos.y ) * m_realBufferSize.x );
531
532 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x )
533 {
534 uint8_t* ptr = &ptrPBO[( yConst + x ) * 4];
535
536 renderFinalColor( ptr, outColor, isFinalColor );
537 }
538 }
539
540 // There is nothing more here to do.. there are no hits ..
541 // just background so continue
542 return;
543 }
544
545 SFVEC4F hitColor_X0Y0[RAYPACKET_RAYS_PER_PACKET];
546
547 // Shade original (0, 0) hits ("paint" the intersected objects)
548 renderRayPackets( bgColor, blockPacket.m_ray, hitPacket_X0Y0,
549 m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_X0Y0 );
550
551 if( m_boardAdapter.m_Cfg->m_Render.raytrace_anti_aliasing )
552 {
553 SFVEC4F hitColor_AA_X1Y1[RAYPACKET_RAYS_PER_PACKET];
554
555 // Intersect one blockPosI + (0.5, 0.5) used for anti aliasing calculation
556 HITINFO_PACKET hitPacket_AA_X1Y1[RAYPACKET_RAYS_PER_PACKET];
557 HITINFO_PACKET_init( hitPacket_AA_X1Y1 );
558
559 RAYPACKET blockPacket_AA_X1Y1( m_camera, (SFVEC2F) blockPosI + SFVEC2F( 0.5f, 0.5f ),
560 randDisp );
561
562 if( !m_accelerator->Intersect( blockPacket_AA_X1Y1, hitPacket_AA_X1Y1 ) )
563 {
564 // Missed all the package
565 for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
566 {
567 const SFVEC4F& outColor = bgColor[y];
568
569 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
570 hitColor_AA_X1Y1[i] = outColor;
571 }
572 }
573 else
574 {
575 renderRayPackets( bgColor, blockPacket_AA_X1Y1.m_ray, hitPacket_AA_X1Y1,
576 m_boardAdapter.m_Cfg->m_Render.raytrace_shadows, hitColor_AA_X1Y1 );
577 }
578
579 SFVEC4F hitColor_AA_X1Y0[RAYPACKET_RAYS_PER_PACKET];
580 SFVEC4F hitColor_AA_X0Y1[RAYPACKET_RAYS_PER_PACKET];
581 SFVEC4F hitColor_AA_X0Y1_half[RAYPACKET_RAYS_PER_PACKET];
582
583 for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
584 {
585 SFVEC4F color_average = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] ) * SFVEC4F( 0.5f );
586
587 hitColor_AA_X1Y0[i] = color_average;
588 hitColor_AA_X0Y1[i] = color_average;
589 hitColor_AA_X0Y1_half[i] = color_average;
590 }
591
592 RAY blockRayPck_AA_X1Y0[RAYPACKET_RAYS_PER_PACKET];
593 RAY blockRayPck_AA_X0Y1[RAYPACKET_RAYS_PER_PACKET];
594 RAY blockRayPck_AA_X1Y1_half[RAYPACKET_RAYS_PER_PACKET];
595
597 m_camera, (SFVEC2F) blockPosI + SFVEC2F( 0.5f - randDisp.x, randDisp.y ),
598 randDisp, blockRayPck_AA_X1Y0 );
599
601 m_camera, (SFVEC2F) blockPosI + SFVEC2F( randDisp.x, 0.5f - randDisp.y ),
602 randDisp, blockRayPck_AA_X0Y1 );
603
605 m_camera, (SFVEC2F) blockPosI + SFVEC2F( 0.25f - randDisp.x, 0.25f - randDisp.y ),
606 randDisp, blockRayPck_AA_X1Y1_half );
607
608 renderAntiAliasPackets( bgColor, hitPacket_X0Y0, hitPacket_AA_X1Y1, blockRayPck_AA_X1Y0,
609 hitColor_AA_X1Y0 );
610
611 renderAntiAliasPackets( bgColor, hitPacket_X0Y0, hitPacket_AA_X1Y1, blockRayPck_AA_X0Y1,
612 hitColor_AA_X0Y1 );
613
614 renderAntiAliasPackets( bgColor, hitPacket_X0Y0, hitPacket_AA_X1Y1,
615 blockRayPck_AA_X1Y1_half, hitColor_AA_X0Y1_half );
616
617 // Average the result
618 for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
619 {
620 hitColor_X0Y0[i] = ( hitColor_X0Y0[i] + hitColor_AA_X1Y1[i] + hitColor_AA_X1Y0[i] +
621 hitColor_AA_X0Y1[i] + hitColor_AA_X0Y1_half[i] ) *
622 SFVEC4F( 1.0f / 5.0f );
623 }
624 }
625
626 // Copy results to the next stage
627 uint8_t* ptr = &ptrPBO[( blockPos.x + ( blockPos.y * m_realBufferSize.x ) ) * 4];
628
629 const uint32_t ptrInc = ( m_realBufferSize.x - RAYPACKET_DIM ) * 4;
630
631 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
632 {
633 SFVEC2I bPos;
634 bPos.y = blockPos.y;
635
636 for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
637 {
638 bPos.x = blockPos.x;
639
640 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
641 {
642 const SFVEC4F& hColor = hitColor_X0Y0[i];
643
644 if( hitPacket_X0Y0[i].m_hitresult == true )
645 {
646 m_postShaderSsao.SetPixelData( bPos.x, bPos.y,
647 hitPacket_X0Y0[i].m_HitInfo.m_HitNormal,
648 hColor,
649 blockPacket.m_ray[i].at(
650 hitPacket_X0Y0[i].m_HitInfo.m_tHit ),
651 hitPacket_X0Y0[i].m_HitInfo.m_tHit,
652 hitPacket_X0Y0[i].m_HitInfo.m_ShadowFactor );
653 }
654 else
655 {
656 m_postShaderSsao.SetPixelData( bPos.x, bPos.y, SFVEC3F( 0.0f ), hColor,
657 SFVEC3F( 0.0f ), 0, 1.0f );
658 }
659
660 renderFinalColor( ptr, hColor, false );
661
662 bPos.x++;
663 ptr += 4;
664 }
665
666 ptr += ptrInc;
667 bPos.y++;
668 }
669 }
670 else
671 {
672 for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
673 {
674 for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
675 {
676 renderFinalColor( ptr, hitColor_X0Y0[i], true );
677 ptr += 4;
678 }
679
680 ptr += ptrInc;
681 }
682 }
683}
684
685
686void RENDER_3D_RAYTRACE_BASE::postProcessShading( uint8_t* /* ptrPBO */, REPORTER* aStatusReporter )
687{
688 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
689 {
690 if( aStatusReporter )
691 aStatusReporter->Report( _( "Rendering: Post processing shader" ) );
692
693 m_postShaderSsao.SetShadowsEnabled( m_boardAdapter.m_Cfg->m_Render.raytrace_shadows );
694
695 std::atomic<size_t> nextBlock( 0 );
696 std::atomic<size_t> threadsFinished( 0 );
697
698 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
699
700 for( size_t ii = 0; ii < parallelThreadCount; ++ii )
701 {
702 std::thread t = std::thread( [&]()
703 {
704 for( size_t y = nextBlock.fetch_add( 1 ); y < m_realBufferSize.y;
705 y = nextBlock.fetch_add( 1 ) )
706 {
707 SFVEC3F* ptr = &m_shaderBuffer[ y * m_realBufferSize.x ];
708
709 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
710 {
711 *ptr = m_postShaderSsao.Shade( SFVEC2I( x, y ) );
712 ptr++;
713 }
714 }
715
716 threadsFinished++;
717 } );
718
719 t.detach();
720 }
721
722 while( threadsFinished < parallelThreadCount )
723 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
724
725 m_postShaderSsao.SetShadedBuffer( m_shaderBuffer );
726
727 // Set next state
729 }
730 else
731 {
732 // As this was an invalid state, set to finish
734 }
735}
736
737
739 REPORTER* /* aStatusReporter */ )
740{
741 if( m_boardAdapter.m_Cfg->m_Render.raytrace_post_processing )
742 {
743 // Now blurs the shader result and compute the final color
744 std::atomic<size_t> nextBlock( 0 );
745 std::atomic<size_t> threadsFinished( 0 );
746
747 size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
748
749 for( size_t ii = 0; ii < parallelThreadCount; ++ii )
750 {
751 std::thread t = std::thread( [&]()
752 {
753 for( size_t y = nextBlock.fetch_add( 1 ); y < m_realBufferSize.y;
754 y = nextBlock.fetch_add( 1 ) )
755 {
756 uint8_t* ptr = &ptrPBO[ y * m_realBufferSize.x * 4 ];
757
758 for( signed int x = 0; x < (int)m_realBufferSize.x; ++x )
759 {
760 const SFVEC3F bluredShadeColor = m_postShaderSsao.Blur( SFVEC2I( x, y ) );
761
762#ifdef USE_SRGB_SPACE
763 const SFVEC4F originColor = convertLinearToSRGBA(
764 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) ) );
765#else
766 const SFVEC4F originColor =
767 m_postShaderSsao.GetColorAtNotProtected( SFVEC2I( x, y ) );
768#endif
769 const SFVEC4F shadedColor = m_postShaderSsao.ApplyShadeColor(
770 SFVEC2I( x, y ), originColor, bluredShadeColor );
771
772 renderFinalColor( ptr, shadedColor, false );
773
774 ptr += 4;
775 }
776 }
777
778 threadsFinished++;
779 } );
780
781 t.detach();
782 }
783
784 while( threadsFinished < parallelThreadCount )
785 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
786
787 // Debug code
788 //m_postShaderSsao.DebugBuffersOutputAsImages();
789 }
790
791 // End rendering
793}
794
795
797{
798 m_isPreview = true;
799
803
804 std::atomic<size_t> nextBlock( 0 );
805 std::atomic<size_t> threadsFinished( 0 );
806
807 size_t parallelThreadCount = std::min<size_t>(
808 std::max<size_t>( std::thread::hardware_concurrency(), 2 ),
809 m_blockPositions.size() );
810
811 for( size_t ii = 0; ii < parallelThreadCount; ++ii )
812 {
813 std::thread t = std::thread( [&]()
814 {
815 for( size_t iBlock = nextBlock.fetch_add( 1 ); iBlock < m_blockPositionsFast.size();
816 iBlock = nextBlock.fetch_add( 1 ) )
817 {
818 const SFVEC2UI& windowPosUI = m_blockPositionsFast[ iBlock ];
819 const SFVEC2I windowsPos = SFVEC2I( windowPosUI.x + m_xoffset,
820 windowPosUI.y + m_yoffset );
821
822 RAYPACKET blockPacket( m_camera, windowsPos, 4 );
823
825
826 // Initialize hitPacket with a "not hit" information
827 for( HITINFO_PACKET& packet : hitPacket )
828 {
829 packet.m_HitInfo.m_tHit = std::numeric_limits<float>::infinity();
830 packet.m_HitInfo.m_acc_node_info = 0;
831 packet.m_hitresult = false;
832 }
833
834 // Intersect packet block
835 m_accelerator->Intersect( blockPacket, hitPacket );
836
837 // Calculate background gradient color
838 SFVEC4F bgColor[RAYPACKET_DIM];
839
840 SFVEC4F bgTopColor = m_backgroundColorTop;
841 SFVEC4F bgBotColor = m_backgroundColorBottom;
842
843 for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
844 {
845 const float posYfactor =
846 (float) ( windowsPos.y + y * 4.0f ) / (float) m_windowSize.y;
847
848 bgColor[y] = bgTopColor * SFVEC4F( posYfactor )
849 + bgBotColor * ( SFVEC4F( 1.0f ) - SFVEC4F( posYfactor ) );
850 }
851
852 COLOR_RGBA hitColorShading[RAYPACKET_RAYS_PER_PACKET];
853
854 for( unsigned int i = 0; i < RAYPACKET_RAYS_PER_PACKET; ++i )
855 {
856 const SFVEC4F bhColorY = bgColor[i / RAYPACKET_DIM];
857
858 if( hitPacket[i].m_hitresult == true )
859 {
860 const SFVEC4F hitColor = shadeHit( bhColorY, blockPacket.m_ray[i],
861 hitPacket[i].m_HitInfo, false,
862 0, false );
863
864 hitColorShading[i] = COLOR_RGBA( hitColor );
865 }
866 else
867 hitColorShading[i] = bhColorY;
868 }
869
870 COLOR_RGBA cLRB_old[( RAYPACKET_DIM - 1 )];
871
872 for( unsigned int y = 0; y < ( RAYPACKET_DIM - 1 ); ++y )
873 {
874 const SFVEC4F bgColorY = bgColor[y];
875 const COLOR_RGBA bgColorYRGB = COLOR_RGBA( bgColorY );
876
877 // This stores cRTB from the last block to be reused next time in a cLTB pixel
878 COLOR_RGBA cRTB_old;
879
880 //RAY cRTB_ray;
881 //HITINFO cRTB_hitInfo;
882
883 for( unsigned int x = 0; x < ( RAYPACKET_DIM - 1 ); ++x )
884 {
885 // pxl 0 pxl 1 pxl 2 pxl 3 pxl 4
886 // x0 x1 ...
887 // .---------------------------.
888 // y0 | cLT | cxxx | cLRT | cxxx | cRT |
889 // | cxxx | cLTC | cxxx | cRTC | cxxx |
890 // | cLTB | cxxx | cC | cxxx | cRTB |
891 // | cxxx | cLBC | cxxx | cRBC | cxxx |
892 // '---------------------------'
893 // y1 | cLB | cxxx | cLRB | cxxx | cRB |
894
895 const unsigned int iLT = ( ( x + 0 ) + RAYPACKET_DIM * ( y + 0 ) );
896 const unsigned int iRT = ( ( x + 1 ) + RAYPACKET_DIM * ( y + 0 ) );
897 const unsigned int iLB = ( ( x + 0 ) + RAYPACKET_DIM * ( y + 1 ) );
898 const unsigned int iRB = ( ( x + 1 ) + RAYPACKET_DIM * ( y + 1 ) );
899
900 // !TODO: skip when there are no hits
901 const COLOR_RGBA& cLT = hitColorShading[ iLT ];
902 const COLOR_RGBA& cRT = hitColorShading[ iRT ];
903 const COLOR_RGBA& cLB = hitColorShading[ iLB ];
904 const COLOR_RGBA& cRB = hitColorShading[ iRB ];
905
906 // Trace and shade cC
907 COLOR_RGBA cC = bgColorYRGB;
908
909 const SFVEC3F& oriLT = blockPacket.m_ray[ iLT ].m_Origin;
910 const SFVEC3F& oriRB = blockPacket.m_ray[ iRB ].m_Origin;
911
912 const SFVEC3F& dirLT = blockPacket.m_ray[ iLT ].m_Dir;
913 const SFVEC3F& dirRB = blockPacket.m_ray[ iRB ].m_Dir;
914
915 SFVEC3F oriC;
916 SFVEC3F dirC;
917
918 HITINFO centerHitInfo;
919 centerHitInfo.m_tHit = std::numeric_limits<float>::infinity();
920
921 bool hittedC = false;
922
923 if( ( hitPacket[iLT].m_hitresult == true )
924 || ( hitPacket[iRT].m_hitresult == true )
925 || ( hitPacket[iLB].m_hitresult == true )
926 || ( hitPacket[iRB].m_hitresult == true ) )
927 {
928 oriC = ( oriLT + oriRB ) * 0.5f;
929 dirC = glm::normalize( ( dirLT + dirRB ) * 0.5f );
930
931 // Trace the center ray
932 RAY centerRay;
933 centerRay.Init( oriC, dirC );
934
935 const unsigned int nodeLT = hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
936 const unsigned int nodeRT = hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
937 const unsigned int nodeLB = hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
938 const unsigned int nodeRB = hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
939
940 if( nodeLT != 0 )
941 hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
942 nodeLT );
943
944 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
945 hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
946 nodeRT );
947
948 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) && ( nodeLB != nodeRT ) )
949 hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
950 nodeLB );
951
952 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) && ( nodeRB != nodeLT )
953 && ( nodeRB != nodeRT ) )
954 hittedC |= m_accelerator->Intersect( centerRay, centerHitInfo,
955 nodeRB );
956
957 if( hittedC )
958 {
959 cC = COLOR_RGBA( shadeHit( bgColorY, centerRay, centerHitInfo,
960 false, 0, false ) );
961 }
962 else
963 {
964 centerHitInfo.m_tHit = std::numeric_limits<float>::infinity();
965 hittedC = m_accelerator->Intersect( centerRay, centerHitInfo );
966
967 if( hittedC )
968 cC = COLOR_RGBA( shadeHit( bgColorY, centerRay, centerHitInfo,
969 false, 0, false ) );
970 }
971 }
972
973 // Trace and shade cLRT
974 COLOR_RGBA cLRT = bgColorYRGB;
975
976 const SFVEC3F& oriRT = blockPacket.m_ray[ iRT ].m_Origin;
977 const SFVEC3F& dirRT = blockPacket.m_ray[ iRT ].m_Dir;
978
979 if( y == 0 )
980 {
981 // Trace the center ray
982 RAY rayLRT;
983 rayLRT.Init( ( oriLT + oriRT ) * 0.5f,
984 glm::normalize( ( dirLT + dirRT ) * 0.5f ) );
985
986 HITINFO hitInfoLRT;
987 hitInfoLRT.m_tHit = std::numeric_limits<float>::infinity();
988
989 if( hitPacket[iLT].m_hitresult && hitPacket[iRT].m_hitresult
990 && ( hitPacket[iLT].m_HitInfo.pHitObject
991 == hitPacket[iRT].m_HitInfo.pHitObject ) )
992 {
993 hitInfoLRT.pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
994 hitInfoLRT.m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
995 hitPacket[ iRT ].m_HitInfo.m_tHit ) * 0.5f;
996 hitInfoLRT.m_HitNormal =
997 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
998 + hitPacket[iRT].m_HitInfo.m_HitNormal )
999 * 0.5f );
1000
1001 cLRT = COLOR_RGBA( shadeHit( bgColorY, rayLRT, hitInfoLRT, false,
1002 0, false ) );
1003 cLRT = BlendColor( cLRT, BlendColor( cLT, cRT ) );
1004 }
1005 else
1006 {
1007 // If any hits
1008 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iRT ].m_hitresult )
1009 {
1010 const unsigned int nodeLT =
1011 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1012 const unsigned int nodeRT =
1013 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1014
1015 bool hittedLRT = false;
1016
1017 if( nodeLT != 0 )
1018 hittedLRT |= m_accelerator->Intersect( rayLRT, hitInfoLRT,
1019 nodeLT );
1020
1021 if( ( nodeRT != 0 ) && ( nodeRT != nodeLT ) )
1022 hittedLRT |= m_accelerator->Intersect( rayLRT, hitInfoLRT,
1023 nodeRT );
1024
1025 if( hittedLRT )
1026 cLRT = COLOR_RGBA( shadeHit( bgColorY, rayLRT, hitInfoLRT,
1027 false, 0, false ) );
1028 else
1029 {
1030 hitInfoLRT.m_tHit = std::numeric_limits<float>::infinity();
1031
1032 if( m_accelerator->Intersect( rayLRT,hitInfoLRT ) )
1033 cLRT = COLOR_RGBA( shadeHit( bgColorY, rayLRT,
1034 hitInfoLRT, false,
1035 0, false ) );
1036 }
1037 }
1038 }
1039 }
1040 else
1041 {
1042 cLRT = cLRB_old[x];
1043 }
1044
1045 // Trace and shade cLTB
1046 COLOR_RGBA cLTB = bgColorYRGB;
1047
1048 if( x == 0 )
1049 {
1050 const SFVEC3F &oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1051 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1052
1053 // Trace the center ray
1054 RAY rayLTB;
1055 rayLTB.Init( ( oriLT + oriLB ) * 0.5f,
1056 glm::normalize( ( dirLT + dirLB ) * 0.5f ) );
1057
1058 HITINFO hitInfoLTB;
1059 hitInfoLTB.m_tHit = std::numeric_limits<float>::infinity();
1060
1061 if( hitPacket[ iLT ].m_hitresult && hitPacket[ iLB ].m_hitresult
1062 && ( hitPacket[ iLT ].m_HitInfo.pHitObject ==
1063 hitPacket[ iLB ].m_HitInfo.pHitObject ) )
1064 {
1065 hitInfoLTB.pHitObject = hitPacket[ iLT ].m_HitInfo.pHitObject;
1066 hitInfoLTB.m_tHit = ( hitPacket[ iLT ].m_HitInfo.m_tHit +
1067 hitPacket[ iLB ].m_HitInfo.m_tHit ) * 0.5f;
1068 hitInfoLTB.m_HitNormal =
1069 glm::normalize( ( hitPacket[iLT].m_HitInfo.m_HitNormal
1070 + hitPacket[iLB].m_HitInfo.m_HitNormal )
1071 * 0.5f );
1072 cLTB = COLOR_RGBA(
1073 shadeHit( bgColorY, rayLTB, hitInfoLTB, false, 0, false ) );
1074 cLTB = BlendColor( cLTB, BlendColor( cLT, cLB) );
1075 }
1076 else
1077 {
1078 // If any hits
1079 if( hitPacket[ iLT ].m_hitresult || hitPacket[ iLB ].m_hitresult )
1080 {
1081 const unsigned int nodeLT =
1082 hitPacket[ iLT ].m_HitInfo.m_acc_node_info;
1083 const unsigned int nodeLB =
1084 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1085
1086 bool hittedLTB = false;
1087
1088 if( nodeLT != 0 )
1089 hittedLTB |= m_accelerator->Intersect( rayLTB, hitInfoLTB,
1090 nodeLT );
1091
1092 if( ( nodeLB != 0 ) && ( nodeLB != nodeLT ) )
1093 hittedLTB |= m_accelerator->Intersect( rayLTB, hitInfoLTB,
1094 nodeLB );
1095
1096 if( hittedLTB )
1097 cLTB = COLOR_RGBA( shadeHit( bgColorY, rayLTB, hitInfoLTB,
1098 false, 0, false ) );
1099 else
1100 {
1101 hitInfoLTB.m_tHit = std::numeric_limits<float>::infinity();
1102
1103 if( m_accelerator->Intersect( rayLTB, hitInfoLTB ) )
1104 cLTB = COLOR_RGBA( shadeHit( bgColorY, rayLTB,
1105 hitInfoLTB, false,
1106 0, false ) );
1107 }
1108 }
1109 }
1110 }
1111 else
1112 {
1113 cLTB = cRTB_old;
1114 }
1115
1116 // Trace and shade cRTB
1117 COLOR_RGBA cRTB = bgColorYRGB;
1118
1119 // Trace the center ray
1120 RAY rayRTB;
1121 rayRTB.Init( ( oriRT + oriRB ) * 0.5f,
1122 glm::normalize( ( dirRT + dirRB ) * 0.5f ) );
1123
1124 HITINFO hitInfoRTB;
1125 hitInfoRTB.m_tHit = std::numeric_limits<float>::infinity();
1126
1127 if( hitPacket[ iRT ].m_hitresult && hitPacket[ iRB ].m_hitresult
1128 && ( hitPacket[ iRT ].m_HitInfo.pHitObject ==
1129 hitPacket[ iRB ].m_HitInfo.pHitObject ) )
1130 {
1131 hitInfoRTB.pHitObject = hitPacket[ iRT ].m_HitInfo.pHitObject;
1132
1133 hitInfoRTB.m_tHit = ( hitPacket[ iRT ].m_HitInfo.m_tHit +
1134 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1135
1136 hitInfoRTB.m_HitNormal =
1137 glm::normalize( ( hitPacket[iRT].m_HitInfo.m_HitNormal
1138 + hitPacket[iRB].m_HitInfo.m_HitNormal )
1139 * 0.5f );
1140
1141 cRTB = COLOR_RGBA( shadeHit( bgColorY, rayRTB, hitInfoRTB, false, 0,
1142 false ) );
1143 cRTB = BlendColor( cRTB, BlendColor( cRT, cRB ) );
1144 }
1145 else
1146 {
1147 // If any hits
1148 if( hitPacket[ iRT ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1149 {
1150 const unsigned int nodeRT =
1151 hitPacket[ iRT ].m_HitInfo.m_acc_node_info;
1152 const unsigned int nodeRB =
1153 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1154
1155 bool hittedRTB = false;
1156
1157 if( nodeRT != 0 )
1158 hittedRTB |= m_accelerator->Intersect( rayRTB, hitInfoRTB,
1159 nodeRT );
1160
1161 if( ( nodeRB != 0 ) && ( nodeRB != nodeRT ) )
1162 hittedRTB |= m_accelerator->Intersect( rayRTB, hitInfoRTB,
1163 nodeRB );
1164
1165 if( hittedRTB )
1166 {
1167 cRTB = COLOR_RGBA( shadeHit( bgColorY, rayRTB, hitInfoRTB,
1168 false, 0, false) );
1169 }
1170 else
1171 {
1172 hitInfoRTB.m_tHit = std::numeric_limits<float>::infinity();
1173
1174 if( m_accelerator->Intersect( rayRTB, hitInfoRTB ) )
1175 cRTB = COLOR_RGBA( shadeHit( bgColorY, rayRTB, hitInfoRTB,
1176 false, 0, false ) );
1177 }
1178 }
1179 }
1180
1181 cRTB_old = cRTB;
1182
1183 // Trace and shade cLRB
1184 COLOR_RGBA cLRB = bgColorYRGB;
1185
1186 const SFVEC3F& oriLB = blockPacket.m_ray[ iLB ].m_Origin;
1187 const SFVEC3F& dirLB = blockPacket.m_ray[ iLB ].m_Dir;
1188
1189 // Trace the center ray
1190 RAY rayLRB;
1191 rayLRB.Init( ( oriLB + oriRB ) * 0.5f,
1192 glm::normalize( ( dirLB + dirRB ) * 0.5f ) );
1193
1194 HITINFO hitInfoLRB;
1195 hitInfoLRB.m_tHit = std::numeric_limits<float>::infinity();
1196
1197 if( hitPacket[iLB].m_hitresult && hitPacket[iRB].m_hitresult
1198 && ( hitPacket[iLB].m_HitInfo.pHitObject ==
1199 hitPacket[iRB].m_HitInfo.pHitObject ) )
1200 {
1201 hitInfoLRB.pHitObject = hitPacket[ iLB ].m_HitInfo.pHitObject;
1202
1203 hitInfoLRB.m_tHit = ( hitPacket[ iLB ].m_HitInfo.m_tHit +
1204 hitPacket[ iRB ].m_HitInfo.m_tHit ) * 0.5f;
1205
1206 hitInfoLRB.m_HitNormal =
1207 glm::normalize( ( hitPacket[iLB].m_HitInfo.m_HitNormal
1208 + hitPacket[iRB].m_HitInfo.m_HitNormal )
1209 * 0.5f );
1210
1211 cLRB = COLOR_RGBA( shadeHit( bgColorY, rayLRB, hitInfoLRB, false, 0,
1212 false ) );
1213 cLRB = BlendColor( cLRB, BlendColor( cLB, cRB ) );
1214 }
1215 else
1216 {
1217 // If any hits
1218 if( hitPacket[ iLB ].m_hitresult || hitPacket[ iRB ].m_hitresult )
1219 {
1220 const unsigned int nodeLB =
1221 hitPacket[ iLB ].m_HitInfo.m_acc_node_info;
1222 const unsigned int nodeRB =
1223 hitPacket[ iRB ].m_HitInfo.m_acc_node_info;
1224
1225 bool hittedLRB = false;
1226
1227 if( nodeLB != 0 )
1228 hittedLRB |= m_accelerator->Intersect( rayLRB, hitInfoLRB,
1229 nodeLB );
1230
1231 if( ( nodeRB != 0 ) && ( nodeRB != nodeLB ) )
1232 hittedLRB |= m_accelerator->Intersect( rayLRB, hitInfoLRB,
1233 nodeRB );
1234
1235 if( hittedLRB )
1236 {
1237 cLRB = COLOR_RGBA( shadeHit( bgColorY, rayLRB, hitInfoLRB,
1238 false, 0, false ) );
1239 }
1240 else
1241 {
1242 hitInfoLRB.m_tHit = std::numeric_limits<float>::infinity();
1243
1244 if( m_accelerator->Intersect( rayLRB, hitInfoLRB ) )
1245 cLRB = COLOR_RGBA( shadeHit( bgColorY, rayLRB, hitInfoLRB,
1246 false, 0, false ) );
1247 }
1248 }
1249 }
1250
1251 cLRB_old[x] = cLRB;
1252
1253 // Trace and shade cLTC
1254 COLOR_RGBA cLTC = BlendColor( cLT , cC );
1255
1256 if( hitPacket[ iLT ].m_hitresult || hittedC )
1257 {
1258 // Trace the center ray
1259 RAY rayLTC;
1260 rayLTC.Init( ( oriLT + oriC ) * 0.5f,
1261 glm::normalize( ( dirLT + dirC ) * 0.5f ) );
1262
1263 HITINFO hitInfoLTC;
1264 hitInfoLTC.m_tHit = std::numeric_limits<float>::infinity();
1265
1266 bool hitted = false;
1267
1268 if( hittedC )
1269 hitted = centerHitInfo.pHitObject->Intersect( rayLTC, hitInfoLTC );
1270 else if( hitPacket[ iLT ].m_hitresult )
1271 hitted = hitPacket[ iLT ].m_HitInfo.pHitObject->Intersect(
1272 rayLTC,
1273 hitInfoLTC );
1274
1275 if( hitted )
1276 cLTC = COLOR_RGBA( shadeHit( bgColorY, rayLTC, hitInfoLTC, false,
1277 0, false ) );
1278 }
1279
1280 // Trace and shade cRTC
1281 COLOR_RGBA cRTC = BlendColor( cRT , cC );
1282
1283 if( hitPacket[ iRT ].m_hitresult || hittedC )
1284 {
1285 // Trace the center ray
1286 RAY rayRTC;
1287 rayRTC.Init( ( oriRT + oriC ) * 0.5f,
1288 glm::normalize( ( dirRT + dirC ) * 0.5f ) );
1289
1290 HITINFO hitInfoRTC;
1291 hitInfoRTC.m_tHit = std::numeric_limits<float>::infinity();
1292
1293 bool hitted = false;
1294
1295 if( hittedC )
1296 hitted = centerHitInfo.pHitObject->Intersect( rayRTC, hitInfoRTC );
1297 else if( hitPacket[ iRT ].m_hitresult )
1298 hitted = hitPacket[iRT].m_HitInfo.pHitObject->Intersect(
1299 rayRTC, hitInfoRTC );
1300
1301 if( hitted )
1302 cRTC = COLOR_RGBA( shadeHit( bgColorY, rayRTC, hitInfoRTC, false,
1303 0, false ) );
1304 }
1305
1306 // Trace and shade cLBC
1307 COLOR_RGBA cLBC = BlendColor( cLB , cC );
1308
1309 if( hitPacket[ iLB ].m_hitresult || hittedC )
1310 {
1311 // Trace the center ray
1312 RAY rayLBC;
1313 rayLBC.Init( ( oriLB + oriC ) * 0.5f,
1314 glm::normalize( ( dirLB + dirC ) * 0.5f ) );
1315
1316 HITINFO hitInfoLBC;
1317 hitInfoLBC.m_tHit = std::numeric_limits<float>::infinity();
1318
1319 bool hitted = false;
1320
1321 if( hittedC )
1322 hitted = centerHitInfo.pHitObject->Intersect( rayLBC, hitInfoLBC );
1323 else if( hitPacket[ iLB ].m_hitresult )
1324 hitted = hitPacket[iLB].m_HitInfo.pHitObject->Intersect(
1325 rayLBC, hitInfoLBC );
1326
1327 if( hitted )
1328 cLBC = COLOR_RGBA( shadeHit( bgColorY, rayLBC, hitInfoLBC, false,
1329 0, false ) );
1330 }
1331
1332 // Trace and shade cRBC
1333 COLOR_RGBA cRBC = BlendColor( cRB , cC );
1334
1335 if( hitPacket[ iRB ].m_hitresult || hittedC )
1336 {
1337 // Trace the center ray
1338 RAY rayRBC;
1339 rayRBC.Init( ( oriRB + oriC ) * 0.5f,
1340 glm::normalize( ( dirRB + dirC ) * 0.5f ) );
1341
1342 HITINFO hitInfoRBC;
1343 hitInfoRBC.m_tHit = std::numeric_limits<float>::infinity();
1344
1345 bool hitted = false;
1346
1347 if( hittedC )
1348 hitted = centerHitInfo.pHitObject->Intersect( rayRBC, hitInfoRBC );
1349 else if( hitPacket[ iRB ].m_hitresult )
1350 hitted = hitPacket[iRB].m_HitInfo.pHitObject->Intersect(
1351 rayRBC, hitInfoRBC );
1352
1353 if( hitted )
1354 cRBC = COLOR_RGBA( shadeHit( bgColorY, rayRBC, hitInfoRBC, false,
1355 0, false ) );
1356 }
1357
1358 // Set pixel colors
1359 uint8_t* ptr =
1360 &ptrPBO[( 4 * x + m_blockPositionsFast[iBlock].x
1362 * ( m_blockPositionsFast[iBlock].y + 4 * y ) ) * 4];
1363
1364 SetPixelSRGBA( ptr + 0, cLT );
1365 SetPixelSRGBA( ptr + 4, BlendColor( cLT, cLRT, cLTC ) );
1366 SetPixelSRGBA( ptr + 8, cLRT );
1367 SetPixelSRGBA( ptr + 12, BlendColor( cLRT, cRT, cRTC ) );
1368
1369 ptr += m_realBufferSize.x * 4;
1370 SetPixelSRGBA( ptr + 0, BlendColor( cLT , cLTB, cLTC ) );
1371 SetPixelSRGBA( ptr + 4, BlendColor( cLTC, BlendColor( cLT , cC ) ) );
1372 SetPixelSRGBA( ptr + 8, BlendColor( cC, BlendColor( cLRT, cLTC, cRTC ) ) );
1373 SetPixelSRGBA( ptr + 12, BlendColor( cRTC, BlendColor( cRT , cC ) ) );
1374
1375 ptr += m_realBufferSize.x * 4;
1376 SetPixelSRGBA( ptr + 0, cLTB );
1377 SetPixelSRGBA( ptr + 4, BlendColor( cC, BlendColor( cLTB, cLTC, cLBC ) ) );
1378 SetPixelSRGBA( ptr + 8, cC );
1379 SetPixelSRGBA( ptr + 12, BlendColor( cC, BlendColor( cRTB, cRTC, cRBC ) ) );
1380
1381 ptr += m_realBufferSize.x * 4;
1382 SetPixelSRGBA( ptr + 0, BlendColor( cLB , cLTB, cLBC ) );
1383 SetPixelSRGBA( ptr + 4, BlendColor( cLBC, BlendColor( cLB , cC ) ) );
1384 SetPixelSRGBA( ptr + 8, BlendColor( cC, BlendColor( cLRB, cLBC, cRBC ) ) );
1385 SetPixelSRGBA( ptr + 12, BlendColor( cRBC, BlendColor( cRB , cC ) ) );
1386 }
1387 }
1388 }
1389
1390 threadsFinished++;
1391 } );
1392
1393 t.detach();
1394 }
1395
1396 while( threadsFinished < parallelThreadCount )
1397 std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
1398}
1399
1400
1401#define USE_EXPERIMENTAL_SOFT_SHADOWS 1
1402
1404 HITINFO& aHitInfo, bool aIsInsideObject,
1405 unsigned int aRecursiveLevel, bool is_testShadow ) const
1406{
1407 const MATERIAL* objMaterial = aHitInfo.pHitObject->GetMaterial();
1408 wxASSERT( objMaterial != nullptr );
1409
1410 SFVEC4F outColor =
1411 SFVEC4F( objMaterial->GetEmissiveColor() + objMaterial->GetAmbientColor(), 1.0f );
1412
1413 if( aRecursiveLevel > 7 )
1414 return outColor;
1415
1416 SFVEC3F hitPoint = aHitInfo.m_HitPoint;
1417
1418 hitPoint += aHitInfo.m_HitNormal * m_boardAdapter.GetNonCopperLayerThickness() * 0.6f;
1419
1420 const SFVEC4F diffuseColorObj =
1421 SFVEC4F( aHitInfo.pHitObject->GetDiffuseColor( aHitInfo ), 1.0f );
1422
1423 bool is_aa_enabled = m_boardAdapter.m_Cfg->m_Render.raytrace_anti_aliasing && !m_isPreview;
1424 float shadow_att_factor_sum = 0.0f;
1425
1426 unsigned int nr_lights_that_can_cast_shadows = 0;
1427
1428 for( const LIGHT* light : m_lights )
1429 {
1430 SFVEC3F vectorToLight;
1431 SFVEC3F colorOfLight;
1432 float distToLight;
1433
1434 light->GetLightParameters( hitPoint, vectorToLight, colorOfLight, distToLight );
1435
1436 const float NdotL = glm::dot( aHitInfo.m_HitNormal, vectorToLight );
1437
1438 // Only calc shade if the normal is facing the direction of light,
1439 // otherwise it is in the shadow
1440 if( NdotL >= FLT_EPSILON )
1441 {
1442 float shadow_att_factor_light = 1.0f;
1443
1444 if( is_testShadow && light->GetCastShadows() )
1445 {
1446 nr_lights_that_can_cast_shadows++;
1447
1448 // For rays that are recursive, just calculate one hit shadow
1449 if( aRecursiveLevel > 0 )
1450 {
1451 RAY rayToLight;
1452 rayToLight.Init( hitPoint, vectorToLight );
1453
1454 // Test if point is not in the shadow.
1455 // Test for any hit from the point in the direction of light
1456 if( m_accelerator->IntersectP( rayToLight, distToLight ) )
1457 shadow_att_factor_light = 0.0f;
1458
1459 }
1460 else // Experimental softshadow calculation
1461 {
1462 const unsigned int shadow_number_of_samples =
1463 m_boardAdapter.m_Cfg->m_Render.raytrace_nrsamples_shadows;
1464 const float shadow_inc_factor = 1.0f / (float) ( shadow_number_of_samples );
1465
1466 for( unsigned int i = 0; i < shadow_number_of_samples; ++i )
1467 {
1468 RAY rayToLight;
1469
1470 if( i == 0 )
1471 {
1472 rayToLight.Init( hitPoint, vectorToLight );
1473 }
1474 else
1475 {
1476 const SFVEC3F unifVector = UniformRandomHemisphereDirection();
1477 const SFVEC3F disturbed_vector_to_light =
1478 glm::normalize( vectorToLight + unifVector *
1479 m_boardAdapter.m_Cfg->m_Render.raytrace_spread_shadows );
1480
1481 rayToLight.Init( hitPoint, disturbed_vector_to_light );
1482 }
1483
1484 if( m_accelerator->IntersectP( rayToLight, distToLight ) )
1485 shadow_att_factor_light -= shadow_inc_factor;
1486 }
1487 }
1488
1489 shadow_att_factor_sum += shadow_att_factor_light;
1490 }
1491
1492 outColor += SFVEC4F( objMaterial->Shade( aRay, aHitInfo, NdotL, diffuseColorObj,
1493 vectorToLight, colorOfLight,
1494 shadow_att_factor_light ),
1495 1.0 );
1496 }
1497 }
1498
1499 // Improvement: this is not taking in account the lightcolor
1500 if( nr_lights_that_can_cast_shadows > 0 )
1501 {
1502 aHitInfo.m_ShadowFactor = glm::max(
1503 shadow_att_factor_sum / (float) ( nr_lights_that_can_cast_shadows * 1.0f ), 0.0f );
1504 }
1505 else
1506 {
1507 aHitInfo.m_ShadowFactor = 1.0f;
1508 }
1509
1510 // Clamp color to not be brighter than 1.0f
1511 outColor = glm::min( outColor, SFVEC4F( 1.0f ) );
1512
1513 if( !m_isPreview )
1514 {
1515 // Reflections
1516 if( ( objMaterial->GetReflection() > 0.0f )
1517 && m_boardAdapter.m_Cfg->m_Render.raytrace_reflections
1518 && ( aRecursiveLevel < objMaterial->GetReflectionRecursionCount() ) )
1519 {
1520 const unsigned int reflection_number_of_samples =
1521 objMaterial->GetReflectionRayCount();
1522
1523 SFVEC4F sum_color = SFVEC4F( 0.0f );
1524
1525 const SFVEC3F reflectVector = aRay.m_Dir - 2.0f *
1526 glm::dot( aRay.m_Dir, aHitInfo.m_HitNormal ) * aHitInfo.m_HitNormal;
1527
1528 for( unsigned int i = 0; i < reflection_number_of_samples; ++i )
1529 {
1530 RAY reflectedRay;
1531
1532 if( i == 0 )
1533 {
1534 reflectedRay.Init( hitPoint, reflectVector );
1535 }
1536 else
1537 {
1538 // Apply some randomize to the reflected vector
1539 const SFVEC3F random_reflectVector =
1540 glm::normalize( reflectVector
1542 * m_boardAdapter.m_Cfg->m_Render
1543 .raytrace_spread_reflections );
1544
1545 reflectedRay.Init( hitPoint, random_reflectVector );
1546 }
1547
1548 HITINFO reflectedHit;
1549 reflectedHit.m_tHit = std::numeric_limits<float>::infinity();
1550
1551 if( m_accelerator->Intersect( reflectedRay, reflectedHit ) )
1552 {
1553 SFVEC4F add = ( diffuseColorObj + SFVEC4F( objMaterial->GetSpecularColor(),
1554 1.0f ) ) *
1555 shadeHit( aBgColor, reflectedRay, reflectedHit, false,
1556 aRecursiveLevel + 1, is_testShadow ) *
1557 SFVEC4F( objMaterial->GetReflection() *
1558 // Falloff factor
1559 (1.0f / ( 1.0f + 0.75f * reflectedHit.m_tHit *
1560 reflectedHit.m_tHit) ) );
1561
1562 sum_color += add;
1563 }
1564 }
1565
1566 outColor += (sum_color / SFVEC4F( (float)reflection_number_of_samples) );
1567 }
1568
1569 // Refraction
1570 const float objTransparency = aHitInfo.pHitObject->GetModelTransparency();
1571
1572 if( ( objTransparency > 0.0f ) && m_boardAdapter.m_Cfg->m_Render.raytrace_refractions
1573 && ( aRecursiveLevel < objMaterial->GetRefractionRecursionCount() ) )
1574 {
1575 const float airIndex = 1.000293f;
1576 const float glassIndex = 1.49f;
1577 const float air_over_glass = airIndex / glassIndex;
1578 const float glass_over_air = glassIndex / airIndex;
1579
1580 const float refractionRatio = aIsInsideObject?glass_over_air:air_over_glass;
1581
1582 SFVEC3F refractedVector;
1583
1584 if( Refract( aRay.m_Dir, aHitInfo.m_HitNormal, refractionRatio, refractedVector ) )
1585 {
1586 // This increase the start point by a "fixed" factor so it will work the
1587 // same for all distances
1588 const SFVEC3F startPoint =
1589 aRay.at( aHitInfo.m_tHit + m_boardAdapter.GetNonCopperLayerThickness() *
1590 0.25f );
1591
1592 const unsigned int refractions_number_of_samples =
1593 objMaterial->GetRefractionRayCount();
1594
1595 SFVEC4F sum_color = SFVEC4F( 0.0f );
1596
1597 for( unsigned int i = 0; i < refractions_number_of_samples; ++i )
1598 {
1599 RAY refractedRay;
1600
1601 if( i == 0 )
1602 {
1603 refractedRay.Init( startPoint, refractedVector );
1604 }
1605 else
1606 {
1607 // apply some randomize to the refracted vector
1608 const SFVEC3F randomizeRefractedVector =
1609 glm::normalize( refractedVector +
1611 m_boardAdapter.m_Cfg->m_Render.raytrace_spread_refractions );
1612
1613 refractedRay.Init( startPoint, randomizeRefractedVector );
1614 }
1615
1616 HITINFO refractedHit;
1617 refractedHit.m_tHit = std::numeric_limits<float>::infinity();
1618
1619 SFVEC4F refractedColor = aBgColor;
1620
1621 if( m_accelerator->Intersect( refractedRay, refractedHit ) )
1622 {
1623 refractedColor = shadeHit( aBgColor, refractedRay, refractedHit,
1624 !aIsInsideObject, aRecursiveLevel + 1, false );
1625
1626 const SFVEC4F absorbance = ( SFVEC4F(1.0f) - diffuseColorObj ) *
1627 (1.0f - objTransparency ) *
1628 objMaterial->GetAbsorvance() *
1629 refractedHit.m_tHit;
1630
1631 const SFVEC4F transparency = 1.0f / ( absorbance + 1.0f );
1632
1633 sum_color += refractedColor * transparency;
1634 }
1635 else
1636 {
1637 sum_color += refractedColor;
1638 }
1639 }
1640
1641 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * sum_color
1642 / SFVEC4F( (float) refractions_number_of_samples );
1643 }
1644 else
1645 {
1646 outColor = outColor * ( 1.0f - objTransparency ) + objTransparency * aBgColor;
1647 }
1648 }
1649 }
1650
1651 return outColor;
1652}
1653
1654
1655static float distance( const SFVEC2UI& a, const SFVEC2UI& b )
1656{
1657 const float dx = (float) a.x - (float) b.x;
1658 const float dy = (float) a.y - (float) b.y;
1659 return hypotf( dx, dy );
1660}
1661
1662
1664{
1666
1667 // Calc block positions for fast preview mode
1668 m_blockPositionsFast.clear();
1669
1670 unsigned int i = 0;
1671
1672 while(1)
1673 {
1674 const unsigned int mX = DecodeMorton2X(i);
1675 const unsigned int mY = DecodeMorton2Y(i);
1676
1677 i++;
1678
1679 const SFVEC2UI blockPos( mX * 4 * RAYPACKET_DIM - mX * 4,
1680 mY * 4 * RAYPACKET_DIM - mY * 4 );
1681
1682 if( ( blockPos.x >= ( (unsigned int)m_windowSize.x - ( 4 * RAYPACKET_DIM + 4 ) ) ) &&
1683 ( blockPos.y >= ( (unsigned int)m_windowSize.y - ( 4 * RAYPACKET_DIM + 4 ) ) ) )
1684 break;
1685
1686 if( ( blockPos.x < ( (unsigned int)m_windowSize.x - ( 4 * RAYPACKET_DIM + 4 ) ) ) &&
1687 ( blockPos.y < ( (unsigned int)m_windowSize.y - ( 4 * RAYPACKET_DIM + 4 ) ) ) )
1688 {
1689 m_blockPositionsFast.push_back( blockPos );
1690
1691 if( blockPos.x > m_realBufferSize.x )
1692 m_realBufferSize.x = blockPos.x;
1693
1694 if( blockPos.y > m_realBufferSize.y )
1695 m_realBufferSize.y = blockPos.y;
1696 }
1697 }
1698
1700
1703
1704 m_xoffset = ( m_windowSize.x - m_realBufferSize.x ) / 2;
1705 m_yoffset = ( m_windowSize.y - m_realBufferSize.y ) / 2;
1706
1707 m_postShaderSsao.UpdateSize( m_realBufferSize );
1708
1709 // Calc block positions for regular rendering. Choose an 'inside out' style of rendering.
1710 m_blockPositions.clear();
1711 const int blocks_x = m_realBufferSize.x / RAYPACKET_DIM;
1712 const int blocks_y = m_realBufferSize.y / RAYPACKET_DIM;
1713 m_blockPositions.reserve( blocks_x * blocks_y );
1714
1715 // Hilbert curve position calculation
1716 // modified from Matters Computational, Springer 2011
1717 // GPLv3, Copyright Joerg Arndt
1718 constexpr auto hilbert_get_pos =
1719 []( size_t aT, size_t& aX, size_t& aY )
1720 {
1721 static const size_t htab[] = { 0b0010, 0b0100, 0b1100, 0b1001, 0b1111, 0b0101,
1722 0b0001, 0b1000, 0b0000, 0b1010, 0b1110, 0b0111,
1723 0b1101, 0b1011, 0b0011, 0b0110 };
1724 static const size_t size = sizeof( size_t ) * 8;
1725 size_t xv = 0;
1726 size_t yv = 0;
1727 size_t c01 = 0;
1728
1729 for( size_t i = 0; i < ( size / 2 ); ++i )
1730 {
1731 size_t abi = aT >> ( size - 2 );
1732 aT <<= 2;
1733
1734 size_t st = htab[( c01 << 2 ) | abi];
1735 c01 = st & 3;
1736
1737 yv = ( yv << 1 ) | ( ( st >> 2 ) & 1 );
1738 xv = ( xv << 1 ) | ( st >> 3 );
1739 }
1740
1741 aX = xv;
1742 aY = yv;
1743 };
1744
1745 size_t total_blocks = blocks_x * blocks_y;
1746 size_t pos = 0;
1747 size_t x = 0;
1748 size_t y = 0;
1749
1750 while( m_blockPositions.size() < total_blocks )
1751 {
1752 hilbert_get_pos( pos++, x, y );
1753
1754 if( x < blocks_x && y < blocks_y )
1755 m_blockPositions.emplace_back( x * RAYPACKET_DIM, y * RAYPACKET_DIM );
1756 }
1757
1758 // Create m_shader buffer
1759 delete[] m_shaderBuffer;
1761
1762 initPbo();
1763}
1764
1765
1767{
1768 HITINFO hitInfo;
1769 hitInfo.m_tHit = std::numeric_limits<float>::infinity();
1770
1771 if( m_accelerator )
1772 {
1773 if( m_accelerator->Intersect( aRay, hitInfo ) )
1774 {
1775 if( hitInfo.pHitObject )
1776 return hitInfo.pHitObject->GetBoardItem();
1777 }
1778 }
1779
1780 return nullptr;
1781}
Defines math related functions.
Defines math related functions.
bool Refract(const SFVEC3F &aInVector, const SFVEC3F &aNormal, float aRin_over_Rout, SFVEC3F &aOutVector)
Based on: https://github.com/mmp/pbrt-v3/blob/master/src/core/reflection.h See also: http://www....
Definition 3d_math.h:112
SFVEC3F UniformRandomHemisphereDirection()
Definition 3d_math.h:55
Helper class to handle information needed to display 3D board.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
A class used to derive camera objects from.
Definition camera.h:103
A base light class to derive to implement other light classes.
Definition light.h:41
Base material class that can be used to derive other material implementations.
Definition material.h:240
float GetAbsorvance() const
Definition material.h:275
virtual SFVEC3F Shade(const RAY &aRay, const HITINFO &aHitInfo, float NdotL, const SFVEC3F &aDiffuseObjColor, const SFVEC3F &aDirToLight, const SFVEC3F &aLightColor, float aShadowAttenuationFactor) const =0
Shade an intersection point.
const SFVEC3F & GetAmbientColor() const
Definition material.h:268
unsigned int GetRefractionRayCount() const
Definition material.h:276
const SFVEC3F & GetSpecularColor() const
Definition material.h:270
const SFVEC3F & GetEmissiveColor() const
Definition material.h:269
float GetReflection() const
Definition material.h:274
unsigned int GetReflectionRayCount() const
Definition material.h:277
virtual SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const =0
BOARD_ITEM * GetBoardItem() const
Definition object_3d.h:56
virtual bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const =0
float GetModelTransparency() const
Definition object_3d.h:65
const MATERIAL * GetMaterial() const
Definition object_3d.h:64
RENDER_3D_BASE(BOARD_ADAPTER &aBoardAdapter, CAMERA &aCamera)
wxSize m_windowSize
The window size that this camera is working.
BOARD_ADAPTER & m_boardAdapter
Settings reference in use for this render.
void renderBlockTracing(uint8_t *ptrPBO, signed int iBlock)
static SFVEC4F premultiplyAlpha(const SFVEC4F &aInput)
RENDER_3D_RAYTRACE_BASE(BOARD_ADAPTER &aAdapter, CAMERA &aCamera)
void renderFinalColor(uint8_t *ptrPBO, const SFVEC4F &rgbColor, bool applyColorSpaceConversion)
void renderTracing(uint8_t *ptrPBO, REPORTER *aStatusReporter)
void render(uint8_t *ptrPBO, REPORTER *aStatusReporter)
void postProcessBlurFinish(uint8_t *ptrPBO, REPORTER *aStatusReporter)
void renderAntiAliasPackets(const SFVEC4F *aBgColorY, const HITINFO_PACKET *aHitPck_X0Y0, const HITINFO_PACKET *aHitPck_AA_X1Y1, const RAY *aRayPck, SFVEC4F *aOutHitColor)
void postProcessShading(uint8_t *ptrPBO, REPORTER *aStatusReporter)
std::vector< int > m_blockPositionsWasProcessed
Flag if a position was already processed (cleared each new render).
BOARD_ITEM * IntersectBoardItem(const RAY &aRay)
RT_RENDER_STATE m_renderState
State used on quality render.
void renderRayPackets(const SFVEC4F *bgColorY, const RAY *aRayPkt, HITINFO_PACKET *aHitPacket, bool is_testShadow, SFVEC4F *aOutHitColor)
int GetWaitForEditingTimeOut() override
Give the interface the time (in ms) that it should wait for editing or movements before (this works f...
std::vector< SFVEC2UI > m_blockPositionsFast
Encode the Morton code positions (on fast preview mode).
SFVEC4F shadeHit(const SFVEC4F &aBgColor, const RAY &aRay, HITINFO &aHitInfo, bool aIsInsideObject, unsigned int aRecursiveLevel, bool is_testShadow) const
BVH_CONTAINER_2D * m_antioutlineBoard2dObjects
wxSize m_oldWindowsSize
Used to see if the windows size changed.
void renderPreview(uint8_t *ptrPBO)
virtual void initPbo()=0
size_t m_blockRenderProgressCount
Save the number of blocks progress of the render.
int64_t m_renderStartTime
Time that the render starts.
std::vector< SFVEC2UI > m_blockPositions
Encode Morton code positions.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
COLOR_RGBA BlendColor(const COLOR_RGBA &aC1, const COLOR_RGBA &aC2)
#define _(s)
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
uint32_t DecodeMorton2Y(uint32_t code)
uint32_t DecodeMorton2X(uint32_t code)
Implements Morton Codes https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ http://www....
int64_t GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.
void RAYPACKET_InitRays_with2DDisplacement(const CAMERA &aCamera, const SFVEC2F &aWindowsPosition, const SFVEC2F &a2DWindowsPosDisplacementFactor, RAY *aRayPck)
#define RAYPACKET_INVMASK
Definition raypacket.h:34
#define RAYPACKET_RAYS_PER_PACKET
Definition raypacket.h:35
#define RAYPACKET_DIM
Definition raypacket.h:32
#define SRGB_GAMA
static SFVEC4F convertLinearToSRGBA(const SFVEC4F &aRGBAcolor)
SFVEC3F ConvertSRGBToLinear(const SFVEC3F &aSRGBcolor)
static void SetPixel(uint8_t *p, const COLOR_RGBA &v)
static void HITINFO_PACKET_init(HITINFO_PACKET *aHitPacket)
static SFVEC3F convertLinearToSRGB(const SFVEC3F &aRGBcolor)
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
static void SetPixelSRGBA(uint8_t *p, const COLOR_RGBA &v)
#define DISP_FACTOR
SFVEC4F ConvertSRGBAToLinear(const SFVEC4F &aSRGBAcolor)
@ RT_RENDER_STATE_POST_PROCESS_SHADE
@ RT_RENDER_STATE_POST_PROCESS_BLUR_AND_FINISH
@ RT_RENDER_STATE_FINISH
@ RT_RENDER_STATE_TRACING
@ RT_RENDER_STATE_MAX
SFVEC4F ConvertSRGBAToLinear(const SFVEC4F &aSRGBAcolor)
bool m_hitresult
Definition hitinfo.h:57
HITINFO m_HitInfo
Definition hitinfo.h:58
Stores the hit information of a ray with a point on the surface of a object.
Definition hitinfo.h:36
unsigned int m_acc_node_info
( 4) The acc stores here the node that it hits
Definition hitinfo.h:42
float m_tHit
( 4) distance
Definition hitinfo.h:38
const OBJECT_3D * pHitObject
( 4) Object that was hitted
Definition hitinfo.h:40
float m_ShadowFactor
( 4) Shadow attenuation (1.0 no shadow, 0.0f darkness)
Definition hitinfo.h:45
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition hitinfo.h:37
SFVEC3F m_HitPoint
(12) hit position
Definition hitinfo.h:44
RAY m_ray[RAYPACKET_RAYS_PER_PACKET]
Definition raypacket.h:54
Definition ray.h:63
SFVEC3F m_Dir
Definition ray.h:67
void Init(const SFVEC3F &o, const SFVEC3F &d)
Definition ray.cpp:35
SFVEC3F m_Origin
Definition ray.h:64
SFVEC3F at(float t) const
Definition ray.h:84
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:31
unsigned char c[4]
Definition color_rgba.h:36
glm::ivec2 SFVEC2I
Definition xv3d_types.h:39
glm::vec2 SFVEC2F
Definition xv3d_types.h:42
glm::vec3 SFVEC3F
Definition xv3d_types.h:44
glm::uvec2 SFVEC2UI
Definition xv3d_types.h:38
glm::vec4 SFVEC4F
Definition xv3d_types.h:46