# How to get shader source for an actor

**URL:** https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408
**Category:** Support
**Created:** [May 1, 2022, 9:24pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408 "2022-05-01T21:24:21Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![BlueKnight7](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@BlueKnight7](https://discourse.vtk.org/u/BlueKnight7)
#### Post date: [May 1, 2022, 9:24pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/1 "2022-05-01T21:24:21Z")

</div>

I know from the nightly build documentation for 9.1 that I can use the GetSource function of vtkShader to retrieve the source (as a string) for the shader. Or, at least I hope that I can.

I also know from the examples that I can get a vtkActor’s vtkShaderProperty with the GetShaderProperty function.

What I can’t figure out is how to connect the two so that I can view the shader’s source in its entirety for the actor that I’m using.

Can someone point out a way to do this?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 2, 2022, 7:51pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/2 "2022-05-02T19:51:39Z")

</div>

Hi,

`vtkShaderProperty` has several methods called `Get*SourceCode()`, reflecting the fact that you don’t have _the shader_, but several ones that serve to customize a given procedure (e.g. geometry) in the rendering workflow in the graphics card.

take care,

Paulo

---

<div class="post-metadata">

### Author: ![BlueKnight7](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@BlueKnight7](https://discourse.vtk.org/u/BlueKnight7)
#### Post date: [May 2, 2022, 8:20pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/3 "2022-05-02T20:20:22Z")

</div>

Hi Paul,

More context would be helpful here.

I don’t see Get\*Source routines listed in the vtkShaderProperty class reference page, but there are several Get\*ShaderCode function. Not trying to be picky, but this might make it clearer for others reading this. I will assume that you meant Get\*ShaderCode.

I saw these functions before, but was uncertain as to how to handle the fact that the return type is a “virtual char \*”; that is why I was trying to work with the vtkShader function that returned a string.

Could you educate us less knowledgeable C++ programmers; how do you use a function that returns a virtual pointer? Do I just ignore the virtual part and assign it to a char \* in this case?

Is there some vkt test code / case that uses this function that one could study?

I appreciate the help.

BK

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 2, 2022, 8:54pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/4 "2022-05-02T20:54:35Z")

</div>

Hello, Roger,

> [@BlueKnight7](#):
>
> I don’t see Get_Source routines listed in the vtkShaderProperty class reference page, but there are several Get_ShaderCode function. Not trying to be picky, but this might make it clearer for others reading this. I will assume that you meant Get\*ShaderCode.

Fixed.

> [@BlueKnight7](#):
>
> I saw these functions before, but was uncertain as to how to handle the fact that the return type is a “virtual char \*”; that is why I was trying to work with the vtkShader function that returned a string.
> 
> Could you educate us less knowledgeable C++ programmers; how do you use a function that returns a virtual pointer? Do I just ignore the virtual part and assign it to a char \* in this case?

The `virtual` keyword only means that the function’s behavior may change depending on the object’s class. It doesn’t have to do with the returned data type, which is `char *`. Suppose the `Animal` class that has a `virtual string talk(){return "";}`. You can have a `Dog` and a `Cat` classes that extend `Animal`. These subclasses may override `Animal::talk()` to implement behavior especific to them: `virtual string Dog::talk(){ return "woof woof!";}` and `virtual string Cat::talk(){ return "meow!";}`. So if you declare an `Animal` object like this: `Animal* a = new Dog();` and if you call its `talk()` method, it’ll return “woof woof!”. Without the `virtual` keyword, it would return an empty string, since `a` belongs to the `Animal` class. The `virtual` keyword is one way in C++ to implement polymorphism, that is, variable behavior for seemingly the same thing.

`char` means a single character. `*` means that it is a pointer to the given data type. So `char *` is a variable that has the memory address to a singe character. Normally, a `char *` denotes the pointer to the first character of a string, which ends with the first of the following characters that has the null character (`\0`) or the 8-bit integer value of zero. These are the so-called "C-string"s. In practice, C++ programmers use some high level string class such as `std::string`. You can just create a `std::string` object from a `char *` by doing, for example, `std::string myString( shaderProperty->GetFragmentShaderCode() );`.

Please, take a look at some examples of shader usage here: [Any example or tips to render sea surface(shader+FFT)? - #2 by Paulo\_Carvalho](https://discourse.vtk.org/t/any-example-or-tips-to-render-sea-surface-shader-fft/7432/2) .

take care,

Paulo

---

<div class="post-metadata">

### Author: ![BlueKnight7](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@BlueKnight7](https://discourse.vtk.org/u/BlueKnight7)
#### Post date: [May 2, 2022, 10:05pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/5 "2022-05-02T22:05:37Z")

</div>

Hi Paulo,

Your reply filled in a number of gaps.

When I tried what you suggested – std::string myString( sp-\>GetFragmentShaderCode() ); – , the code crashed with this error:

terminate called after throwing an instance of ‘std::logic\_error’  
what(): basic\_string::\_S\_construct null not valid

When I looked at this in the debugger, a null was truly returned by GetFragmentShaderCode.

So, what am I missing here?

I created an actor from vtkPolyDataMapper – zActor1-\>SetMapper( zMapper );. Isn’t the default shader assigned by the time the actor is created and mapper assigned?

Thanks.

BK

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 3, 2022, 3:21pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/7 "2022-05-03T15:21:46Z")

</div>

Hello,

> [@BlueKnight7](#):
>
> When I tried what you suggested – std::string myString( sp-\>GetFragmentShaderCode() ); – , the code crashed with this error:
> 
> terminate called after throwing an instance of ‘std::logic\_error’  
> what(): basic\_string::\_S\_construct null not valid
> 
> When I looked at this in the debugger, a null was truly returned by GetFragmentShaderCode.
> 
> So, what am I missing here?

Pointers are just integer numbers. In C/C++ standard, a null pointer is a pointer with zero value since the memory address zero is not accessible to a user program. So, you have to test for that before doing something with the pointer. For example, a simple

```cpp
 char* szCode = sp->GetFragmentShaderCode();
 if( code ) { 
     std::string strCode( szCode );
     //do something with strCode
 } else { 
     printf('WARNING: fragment shader code not set.'); 
 }

```

does it.

> [@BlueKnight7](#):
>
> I created an actor from vtkPolyDataMapper – zActor1-\>SetMapper( zMapper );. Isn’t the default shader assigned by the time the actor is created and mapper assigned?

From what I know, you need set the shader code somewhere beforehand. You won’t get whatever the mapper uses by default by calling `Get*ShaderCode()` on `vtkShaderProperty`. But maybe someone out there with more experience in using shaders in VTK knows a hack.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![BlueKnight7](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@BlueKnight7](https://discourse.vtk.org/u/BlueKnight7)
#### Post date: [May 3, 2022, 3:35pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/8 "2022-05-03T15:35:52Z")

</div>

Paulo,

Most of the VTK shader examples talk about changing the shader code. But, I need to see the code that is being used in order to know how to change it! Can’t change what I don’t know and need to avoid conflicts. That and one needs to know what the variables are named that the other parts of VTK are passing into the shader (e.g. normals, colors, vertices, etc.)

Thanks for you help so far.

BK

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 3, 2022, 6:14pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/9 "2022-05-03T18:14:31Z")

</div>

Hello,

If your mapper is a `vtkOpenGLPolyDataMapper`, then you can call `Get[Fragment|Geometry|Vertex]ShaderCode()` on it:

```cpp
vtkOpenGLPolyDataMapper* mapperGL = std::dynamic_cast<vtkOpenGLPolyDataMapper*>( zMapper );
if( mapperGL ){
   char* szFragShaderCode = mapperGL->GetFragmentShaderCode();
   if( szFragShaderCode ){
      printf( "Fragment shader code:\n%s\n", szFragShaderCode);
   } else {
      printf( "Fragment shader code not set.\n" );
   }
} else {
   printf( "Mapper is not a vtkOpenGLPolyDataMapper.\n" );
}

```

take care,

Paulo

---

<div class="post-metadata">

### Author: ![simonesneault](https://discourse.vtk.org/user_avatar/discourse.vtk.org/simonesneault/32/67_2.png) [@simonesneault](https://discourse.vtk.org/u/simonesneault)
#### Post date: [May 3, 2022, 7:26pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/10 "2022-05-03T19:26:36Z")

</div>

Hello Roger,

In order to get access to the real shader, you can use a trick to make it crash. Then the shader will be printed in the console !  
For exemple you can call

```auto
mapperGL->AddShaderReplacement( vtkShader::Fragment, "//VTK::", true, "crap_text", false );              

```

It will replace the first token “//VTK::” by another text and won’t be able to compile.  
Just some ugly trick, but it should work.

Simon

---

<div class="post-metadata">

### Author: ![pieper](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pieper/32/17_2.png) [@pieper](https://discourse.vtk.org/u/pieper)
#### Post date: [May 3, 2022, 7:59pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/11 "2022-05-03T19:59:13Z")

</div>

That’s a cool trick @simonesneault 👍

You can also get a lot of insight using [renderdoc](https://renderdoc.org/). Spoiler alert: it’s hard to work on the vtk shaders because handle a lot of different options through code generation in C++.

---

<div class="post-metadata">

### Author: ![BlueKnight7](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@BlueKnight7](https://discourse.vtk.org/u/BlueKnight7)
#### Post date: [May 4, 2022, 1:10am UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/12 "2022-05-04T01:10:17Z")

</div>

Hi All,

Pardon this for its length. There are a few things to address.

First, thanks to all the responders.

I couldn’t get the dynamic\_cast to work in Paulo’s suggesting because I had declared the mapper as: vtkNew\<vtkOpenGLPolyDataMapper\> zMapper  
But, I was able to work around it by using  
char \*szFragShaderCode = zMapper-\>GetFragmentShaderCode();  
I’ll attribute this to the old way of dong things vs the new way (please somebody correct me if this is wrong).

Simon’s tip was kind of cool. Got to see the code!

Will explore renderdoc when I get a minute. Thanks Steve.

I can appreciate that VTK is doing a lot with shaders under the hood, setting things with C++ code, etc. The reason I started looking at the shaders is that I have them working the way I wanted in some other OpenGL code that I wrote. But, I ran into some problems with trying to get VTK to do what I consider to be a very simple thing. I posted this as a bug on VTK discourse development. I wanted to do the backfaces with a different color and it was taking the outside of a sphere as the backfaces and not the inside. However, if I use culling on the sphere, the backfaces ARE the inside. See: [Backface bug in vtkActor](https://discourse.vtk.org/t/backface-bug-in-vtkactor/8376)  
Am totally confused on this and would appreciate any help.

BK

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [May 4, 2022, 1:46pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/13 "2022-05-04T13:46:27Z")

</div>

Hi, Roger,

> [@BlueKnight7](#):
>
> I couldn’t get the dynamic\_cast to work in Paulo’s suggesting because I had declared the mapper as: vtkNew zMapper

I didn’t know `zMapper` was a smartpointer to a `vtkOpenGLPolyDataMapper`. That’s why it’s important to post the code you’re using.

> [@BlueKnight7](#):
>
> Simon’s tip was kind of cool. Got to see the code!

It’s kind of cool but do be careful where you do that. Intentionally crashing the graphics driver/cards in a hired cloud server, for example, will likely put you in hot water.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![BlueKnight7](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@BlueKnight7](https://discourse.vtk.org/u/BlueKnight7)
#### Post date: [May 4, 2022, 1:50pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/14 "2022-05-04T13:50:47Z")

</div>

HI Paulo,

Both points noted.  
Thanks.

BK

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [May 6, 2022, 2:20pm UTC](https://discourse.vtk.org/t/how-to-get-shader-source-for-an-actor/8408/15 "2022-05-06T14:20:03Z")

</div>

You can also print the shaders by adding a print at the very bottom of `vtkOpenGLPolyDataMapper::BuildShaders` or `vtkOpenGLGPUVolumeRayCastMapper::BuildShader`.

That’s where all the dynamic shader code is resolved before compiling the shader program.

Something like

```auto
std::cout << shaders[vtkShader::Vertex]->GetSource() << std::endl;
std::cout << shaders[vtkShader::Fragment]->GetSource() << std::endl;
std::cout << shaders[vtkShader::Geometry]->GetSource() << std::endl;

```
