Archived issue #0024192
Adding support for shaders to OCCT visualization toolkit
Description
The purpose of this feature is implementing the following functionality:
1. Support of both modern (3.0 and higher) and old versions of OpenGL and GLSL. Interoperability between 'old' and 'new' shader sources - by implementing custom shader programming interface.
2. Setting individual shader programs for different types of primitives.
3. Possibility of setting shader program 'by default', for presentation and for individual group inside presentation.
4. Possibility of manipulating shaders from application level, including adding and removing shader programs and setting non-standard shader variables. Thus, application developer can implement custom rendering techniques.
1. Support of both modern (3.0 and higher) and old versions of OpenGL and GLSL. Interoperability between 'old' and 'new' shader sources - by implementing custom shader programming interface.
2. Setting individual shader programs for different types of primitives.
3. Possibility of setting shader program 'by default', for presentation and for individual group inside presentation.
4. Possibility of manipulating shaders from application level, including adding and removing shader programs and setting non-standard shader variables. Thus, application developer can implement custom rendering techniques.
Additional information
The purpose of this feature is adding support of GLSL shaders to OCCT visualization core. Shaders allow to implement different rendering effects on graphics hardware with a high degree of flexibility, such as per-pixel lighting, custom shading models, bump mapping, procedural texturing and others. Currently OCCT supports only vertex and fragment GLSL shader, but this functionality will be extended in future releases.
To set custom vertex and fragment GLSL shaders in DRAW the following command is used:
vshaderprog <ShapeName> <Path to Vertex Shader> <Path to Fragment Shader>
To detach shader program from specific shape, use the following command:
vshaderprog <ShapeName> off
Note, that to execute shader programs the OpenGL 2.0+ GPU is required.
To enable custom shader for specific AISShape in your application, the following API functions are used:
// Create shader program
Handle(Graphic3d_ShaderProgram) aProgram = new Graphic3d_ShaderProgram();
// Attach vertex shader
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile(
Graphic3d_TOS_VERTEX, "<Path to VS>"));
// Attach fragment shader
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile(
Graphic3d_TOS_FRAGMENT, "<Path to FS>"));
// Set values for custom uniform variables (if they are)
aProgram->PushVariable ("MyColor", Graphic3d_Vec3(0.f, 1.f, 0.f));
// Set aspect property for specific AISShape
AISShape->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
To set custom vertex and fragment GLSL shaders in DRAW the following command is used:
vshaderprog <ShapeName> <Path to Vertex Shader> <Path to Fragment Shader>
To detach shader program from specific shape, use the following command:
vshaderprog <ShapeName> off
Note, that to execute shader programs the OpenGL 2.0+ GPU is required.
To enable custom shader for specific AISShape in your application, the following API functions are used:
// Create shader program
Handle(Graphic3d_ShaderProgram) aProgram = new Graphic3d_ShaderProgram();
// Attach vertex shader
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile(
Graphic3d_TOS_VERTEX, "<Path to VS>"));
// Attach fragment shader
aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile(
Graphic3d_TOS_FRAGMENT, "<Path to FS>"));
// Set values for custom uniform variables (if they are)
aProgram->PushVariable ("MyColor", Graphic3d_Vec3(0.f, 1.f, 0.f));
// Set aspect property for specific AISShape
AISShape->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
Public activity
21 archived notes
Participants are labeled by their role within this record.
Dear dbp,
please post/attach the following info as soon as patch will be available:
- Image comparison on several test cases (with/without shaders).
- Performance comparison (including CPU times) on several test cases (including big number of simple objects with different presentation mode / interleaved presentation mode).
please post/attach the following info as soon as patch will be available:
- Image comparison on several test cases (with/without shaders).
- Performance comparison (including CPU times) on several test cases (including big number of simple objects with different presentation mode / interleaved presentation mode).
Dear kgv,
Preliminary version of shader fucntionality is ready for review (branch CR24192_2).
Could you please have a look at it?
Preliminary version of shader fucntionality is ready for review (branch CR24192_2).
Could you please have a look at it?
Dear apl,
the patch CR24192 is ready for review (branch CR24192).
Could you please have a look at it?
the patch CR24192 is ready for review (branch CR24192).
Could you please have a look at it?
Dear Commenter 1,
The branch CR24192 is ready for tests.
Please test.
The branch CR24192 is ready for tests.
Please test.
Dear Commenter 1,
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms.
There is compilation error on Linux platform:
http://jenkins-test-02.nnov.opencascade.com:8080/user/mnt/my-views/view/CR24192/job/mnt-CR24192-master_build_occt_linux/1/parsed_console/?
In file included from ../../../../inc/Graphic3d_ShaderVariable.hxx:179,
from ../../../../inc/Graphic3d_ShaderProgram.hxx:25,
from ../../../../src/Graphic3d/Graphic3d_Group_8.cxx:49:
../../../../inc/Graphic3d_ShaderVariable_Impl.lxx:61: error: cannot declare member function ‘static Graphic3d_ShaderVariable* Graphic3d_ShaderVariable::Create(const TCollection_AsciiString&, const T&)’ to have static linkage
make[2]: *** [Graphic3d_Group_8.lo] Error 1
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms.
There is compilation error on Linux platform:
http://jenkins-test-02.nnov.opencascade.com:8080/user/mnt/my-views/view/CR24192/job/mnt-CR24192-master_build_occt_linux/1/parsed_console/?
In file included from ../../../../inc/Graphic3d_ShaderVariable.hxx:179,
from ../../../../inc/Graphic3d_ShaderProgram.hxx:25,
from ../../../../src/Graphic3d/Graphic3d_Group_8.cxx:49:
../../../../inc/Graphic3d_ShaderVariable_Impl.lxx:61: error: cannot declare member function ‘static Graphic3d_ShaderVariable* Graphic3d_ShaderVariable::Create(const TCollection_AsciiString&, const T&)’ to have static linkage
make[2]: *** [Graphic3d_Group_8.lo] Error 1
Patch CR24192 has been updated.
Dear Commenter 1,
The compilation error corrected.
Please proceed.
The compilation error corrected.
Please proceed.
According to frame rate comparison, there is no a performance regression when the feature is not used (no shader programs are used). The results shown for 6400 small boxes.
Before the patch:
----------------------------------
FPS: 56.800739991127884
CPU: 17.78411399999996 msec
After the patch:
----------------------------------
FPS: 58.451109685153412
CPU: 17.31611099999995 msec
For performance estimation the following script was used:
pload ALL
vinit w=1024 h=768
vsetdispmode 1
set N 1
for {set X 1} {$X <= 80} {incr X} { for {set Y 1} {$Y <= 80} {incr Y} { box b${N} [expr $X * 20] [expr $Y * 20] [expr 50 * ( sin($X) + sin($Y) )] 10 10 25; vdisplay b${N}; incr N; } }
vfit
vfps
Before the patch:
----------------------------------
FPS: 56.800739991127884
CPU: 17.78411399999996 msec
After the patch:
----------------------------------
FPS: 58.451109685153412
CPU: 17.31611099999995 msec
For performance estimation the following script was used:
pload ALL
vinit w=1024 h=768
vsetdispmode 1
set N 1
for {set X 1} {$X <= 80} {incr X} { for {set Y 1} {$Y <= 80} {incr Y} { box b${N} [expr $X * 20] [expr $Y * 20] [expr 50 * ( sin($X) + sin($Y) )] 10 10 25; vdisplay b${N}; incr N; } }
vfit
vfps
Then using individual 'Phong Shading' shader (per-pixel lighting) for each box, the performance regression for previous test is about 50% (6400 boxes):
FPS: 28.340777629084517
CPU: 35.256225999999913 msec
Then using the same 'Phong Shading' shader program (per-pixel lighting) for all boxes, the performance regression is about 33%:
FPS: 37.848268270758574
CPU: 26.208168000000001 msec
Then using the same 'Gouraud Shading' shader program (per-vertex lighting like FFP) for all boxes, the performance regression is about 14%:
FPS: 48.030730269723115
CPU: 20.904133999999885 msec
So, it seems that shader functionality does not increase the CPU load significantly. Simple profiling showed, that performance decrease is largely due to switching graphics pipeline state (calling of glUseProgram).
All tests were performed with Windows 7 PC: Core i5 3.1 GHz, AMD Radeon 7870.
FPS: 28.340777629084517
CPU: 35.256225999999913 msec
Then using the same 'Phong Shading' shader program (per-pixel lighting) for all boxes, the performance regression is about 33%:
FPS: 37.848268270758574
CPU: 26.208168000000001 msec
Then using the same 'Gouraud Shading' shader program (per-vertex lighting like FFP) for all boxes, the performance regression is about 14%:
FPS: 48.030730269723115
CPU: 20.904133999999885 msec
So, it seems that shader functionality does not increase the CPU load significantly. Simple profiling showed, that performance decrease is largely due to switching graphics pipeline state (calling of glUseProgram).
All tests were performed with Windows 7 PC: Core i5 3.1 GHz, AMD Radeon 7870.
Could you please test also on some real-case model, like Vessel_Head?
For real-life shape with fine tesselation (6M triangles) the results are the following.
Before the patch
----------------------------------
FPS: 137.43942063408832
CPU: 5.6160359999999798 msec
After the patch (NO shaders)
----------------------------------
FPS: 145.7065051774959
CPU: 5.9280379999999866 msec
After the patch + 'Gouraud Shading' shader
-------------------------------------------
FPS: 143.91671800136763
CPU: 5.6160359999999798 msec
After the patch + 'Phong Shading' shader
-------------------------------------------
FPS: 143.1793642049785
CPU: 6.2400399999999934 msec
So, then using custom shaders for single object (or small number of objects) thre is no performance regression.
Before the patch
----------------------------------
FPS: 137.43942063408832
CPU: 5.6160359999999798 msec
After the patch (NO shaders)
----------------------------------
FPS: 145.7065051774959
CPU: 5.9280379999999866 msec
After the patch + 'Gouraud Shading' shader
-------------------------------------------
FPS: 143.91671800136763
CPU: 5.6160359999999798 msec
After the patch + 'Phong Shading' shader
-------------------------------------------
FPS: 143.1793642049785
CPU: 6.2400399999999934 msec
So, then using custom shaders for single object (or small number of objects) thre is no performance regression.
For testing shader functionality the simplest 'Normal' shaders can be used (attached to this issue).
To set up custom shader program for AIS interactive object, the following DRAW script can be used:
pload ALL
vinit
vsetdispmode 1
restore <PATH TO SHAPE> SHAPE
vdisplay SHAPE
vshaderprog SHAPE <PATH TO VERTEX SHADER> <PATH TO FRAGMENT SHADER>
To set up custom shader program for AIS interactive object, the following DRAW script can be used:
pload ALL
vinit
vsetdispmode 1
restore <PATH TO SHAPE> SHAPE
vdisplay SHAPE
vshaderprog SHAPE <PATH TO VERTEX SHADER> <PATH TO FRAGMENT SHADER>
Dear Commenter 1,
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms.
There is compilation error on Linux platform:
http://jenkins-test-02.nnov.opencascade.com:8080/user/mnt/my-views/view/CR24192/job/mnt-CR24192-master_build_occt_linux/1/parsed_console/?
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:30: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:31: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:32: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:33: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:34: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:35: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:36: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:37: error: expected unqualified-id before ‘;’ token
make[2]: *** [Graphic3d_ShaderVariable.lo] Error 1
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms.
There is compilation error on Linux platform:
http://jenkins-test-02.nnov.opencascade.com:8080/user/mnt/my-views/view/CR24192/job/mnt-CR24192-master_build_occt_linux/1/parsed_console/?
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:30: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:31: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:32: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:33: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:34: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:35: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:36: error: expected unqualified-id before ‘;’ token
../../../../src/Graphic3d/Graphic3d_ShaderVariable.cxx:37: error: expected unqualified-id before ‘;’ token
make[2]: *** [Graphic3d_ShaderVariable.lo] Error 1
Dear dbp,
The solution for proper initialization of resources is implemented in branch CR24192. The branch is based on CR24228 (the original version of the patch for resources).
Could you please review it?
The solution for proper initialization of resources is implemented in branch CR24192. The branch is based on CR24228 (the original version of the patch for resources).
Could you please review it?
Dear Commenter 1,
The compilation error was corrected (branch CR24192).
Please proceed.
The compilation error was corrected (branch CR24192).
Please proceed.
Dear Commenter 1,
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms and tested.
[revision removed]
Number of compiler warnings:
occt component :
Linux: 365 (368 on master)
Windows: 6 (6 on master)
products component :
Linux: 189 (190 on master)
Windows: 287 (287 on master)
Regressions/Differences:
http://occt-tests/CR24192-master-occt/Windows-32-VC9/bugs/vis/bug23654_MarkersRecompute.html
bugs vis(004) bug23654_MarkersRecompute: FAILED (exception)
Testing cases:
Not needed
Testing on Linux:
Total MEMORY difference: 355386952 / 353811788
Total CPU difference: 41218.42000000063 / 42952.64000000077
Testing on Windows:
Total MEMORY difference: 407624752 / 406995556
Total CPU difference: 25346.65625 / 37404.296875
There are not differences in images found by testdiff.
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms and tested.
[revision removed]
Number of compiler warnings:
occt component :
Linux: 365 (368 on master)
Windows: 6 (6 on master)
products component :
Linux: 189 (190 on master)
Windows: 287 (287 on master)
Regressions/Differences:
http://occt-tests/CR24192-master-occt/Windows-32-VC9/bugs/vis/bug23654_MarkersRecompute.html
bugs vis(004) bug23654_MarkersRecompute: FAILED (exception)
Testing cases:
Not needed
Testing on Linux:
Total MEMORY difference: 355386952 / 353811788
Total CPU difference: 41218.42000000063 / 42952.64000000077
Testing on Windows:
Total MEMORY difference: 407624752 / 406995556
Total CPU difference: 25346.65625 / 37404.296875
There are not differences in images found by testdiff.
Dear dbp,
The branch CR24192 is rebased for the current state of CR24228. CR24228 improves approach to managing resources and contains changes necessary for CR24192. The reported problem is already fixed in its current state.
Please review.
The branch CR24192 is rebased for the current state of CR24228. CR24228 improves approach to managing resources and contains changes necessary for CR24192. The reported problem is already fixed in its current state.
Please review.
Dear Commenter 1,
please re-test (branch CR24192).
please re-test (branch CR24192).
Dear mkv,
The crash in DrawElements function when using shaders is produced by Mesa's software OpenGL implementation. Patch is ok, and testing my be continued.
The crash in DrawElements function when using shaders is produced by Mesa's software OpenGL implementation. Patch is ok, and testing my be continued.
Dear Commenter 1,
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms and tested.
[revision removed]
Number of compiler warnings:
occt component :
Linux: 362 (368 on master)
Windows: 6 (6 on master)
products component :
Linux: 189 (190 on master)
Windows: 287 (287 on master)
Regressions/Differences:
No regressions/differences
Testing cases:
Not needed
Testing on Linux:
Total MEMORY difference: 355499732 / 353799020
Total CPU difference: 43739.12000000061 / 42953.02000000077
Testing on Windows:
Total MEMORY difference: 408358948 / 407073520
Total CPU difference: 31213.734375 / 37415.03125
There are not differences in images found by testdiff.
Branch CR24192 (and products from GIT master) was compiled on Linux and Windows platforms and tested.
[revision removed]
Number of compiler warnings:
occt component :
Linux: 362 (368 on master)
Windows: 6 (6 on master)
products component :
Linux: 189 (190 on master)
Windows: 287 (287 on master)
Regressions/Differences:
No regressions/differences
Testing cases:
Not needed
Testing on Linux:
Total MEMORY difference: 355499732 / 353799020
Total CPU difference: 43739.12000000061 / 42953.02000000077
Testing on Windows:
Total MEMORY difference: 408358948 / 407073520
Total CPU difference: 31213.734375 / 37415.03125
There are not differences in images found by testdiff.
Dear dbp,
Please fill in description sections.
Please fill in description sections.
Related records
- #0024241 · parent of · closedAdding shaders directory to OCCT resources
- #0030126 · parent of · assignedVisualization, Graphic3d_ShaderManager - define standard Lighting & Clipping within custom GLSL programs as template functions
- #0031715 · parent of · closedVisualization, OpenGl_ShaderProgram - add access to proxy shader program
- #0032289 · parent of · closedVisualization - add NCollection_Mat3 for 3x3 matrix similar to NCollection_Mat4
- #0024228 · related to · closedTKOpenGL - destroy GL context at view close
- #0026275 · related to · closedVisualization, TKOpenGl - add public constructor for OpenGl_ShaderProgram