DiscussionsIssue archiveOCCT:Visualization

Archived issue #0024192

Adding support for shaders to OCCT visualization toolkit

Open CASCADEOCCT:Visualizationclosed21 public notes

Search issues

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.

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);




Public activity

21 archived notes

Participants are labeled by their role within this record.

01Commenter 2
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).
02Commenter 2
Dear kgv,

Preliminary version of shader fucntionality is ready for review (branch CR24192_2).

Could you please have a look at it?
03Commenter 3
Dear apl,

the patch CR24192 is ready for review (branch CR24192).

Could you please have a look at it?
04Commenter 4
Dear Commenter 1,

The branch CR24192 is ready for tests.

Please test.
05Commenter 5
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
06Commenter 2
Patch CR24192 has been updated.
07Commenter 7
Dear Commenter 1,

The compilation error corrected.

Please proceed.
08Commenter 8
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
09Commenter 9
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.

10Commenter 10
Could you please test also on some real-case model, like Vessel_Head?
11Commenter 11
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.
12Commenter 12
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>
13Commenter 13
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
14Commenter 14
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?
15Commenter 15
Dear Commenter 1,

The compilation error was corrected (branch CR24192).

Please proceed.
16Commenter 16
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.
17Commenter 17
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.
18Commenter 18
Dear Commenter 1,

please re-test (branch CR24192).
19Commenter 19
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.
20Commenter 20
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.
21Commenter 21
Dear dbp,

Please fill in description sections.

Related records