Fix for [#34754] "Revision 55527 provokes glitchy GLSL shadow map rendering" reported...
[blender.git] / source / gameengine / Ketsji / BL_BlenderShader.cpp
1 /** \file gameengine/Ketsji/BL_BlenderShader.cpp
2  *  \ingroup ketsji
3  */
4
5 #include "DNA_customdata_types.h"
6 #include "DNA_material_types.h"
7 #include "DNA_scene_types.h"
8
9 #include "BLI_utildefines.h"
10
11 #include "BKE_global.h"
12 #include "BKE_main.h"
13 #include "BKE_DerivedMesh.h"
14
15 #include "BL_BlenderShader.h"
16 #include "BL_Material.h"
17
18 #include "GPU_extensions.h"
19 #include "GPU_material.h"
20
21 #include "RAS_BucketManager.h"
22 #include "RAS_MeshObject.h"
23 #include "RAS_IRasterizer.h"
24  
25 BL_BlenderShader::BL_BlenderShader(KX_Scene *scene, struct Material *ma, int lightlayer)
26 :
27         mScene(scene),
28         mMat(ma),
29         mLightLayer(lightlayer),
30         mGPUMat(NULL)
31 {
32         mBlenderScene = scene->GetBlenderScene();
33         mAlphaBlend = GPU_BLEND_SOLID;
34
35         ReloadMaterial();
36 }
37
38 BL_BlenderShader::~BL_BlenderShader()
39 {
40         if (mGPUMat)
41                 GPU_material_unbind(mGPUMat);
42 }
43
44 void BL_BlenderShader::ReloadMaterial()
45 {
46         mGPUMat = (mMat) ? GPU_material_from_blender(mBlenderScene, mMat) : NULL;
47 }
48
49 void BL_BlenderShader::SetProg(bool enable, double time, RAS_IRasterizer* rasty)
50 {
51         if (VerifyShader()) {
52                 if (enable) {
53                         assert(rasty != NULL); // XXX Kinda hacky, but SetProg() should always have the rasterizer if enable is true
54
55                         float viewmat[4][4], viewinvmat[4][4];
56                         const MT_Matrix4x4& view = rasty->GetViewMatrix();
57                         const MT_Matrix4x4& viewinv = rasty->GetViewInvMatrix();
58                         view.getValue((float*)viewmat);
59                         viewinv.getValue((float*)viewinvmat);
60
61                         GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1, viewmat, viewinvmat);
62                 }
63                 else
64                         GPU_material_unbind(mGPUMat);
65         }
66 }
67
68 int BL_BlenderShader::GetAttribNum()
69 {
70         GPUVertexAttribs attribs;
71         int i, enabled = 0;
72
73         if (!VerifyShader())
74                 return enabled;
75
76         GPU_material_vertex_attributes(mGPUMat, &attribs);
77
78         for (i = 0; i < attribs.totlayer; i++)
79                 if (attribs.layer[i].glindex+1 > enabled)
80                         enabled= attribs.layer[i].glindex+1;
81         
82         if (enabled > BL_MAX_ATTRIB)
83                 enabled = BL_MAX_ATTRIB;
84
85         return enabled;
86 }
87
88 void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
89 {
90         GPUVertexAttribs attribs;
91         GPUMaterial *gpumat;
92         int i, attrib_num, uv = 0;
93
94         ras->SetAttribNum(0);
95
96         if (!VerifyShader())
97                 return;
98         
99         gpumat = mGPUMat;
100
101         if (ras->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
102                 GPU_material_vertex_attributes(gpumat, &attribs);
103                 attrib_num = GetAttribNum();
104
105                 ras->SetTexCoordNum(0);
106                 ras->SetAttribNum(attrib_num);
107                 for (i = 0; i < attrib_num; i++)
108                         ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
109
110                 for (i = 0; i < attribs.totlayer; i++) {
111                         if (attribs.layer[i].glindex > attrib_num)
112                                 continue;
113
114                         if (attribs.layer[i].type == CD_MTFACE)
115                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_UV, attribs.layer[i].glindex, uv++);
116                         else if (attribs.layer[i].type == CD_TANGENT)
117                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT, attribs.layer[i].glindex);
118                         else if (attribs.layer[i].type == CD_ORCO)
119                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_ORCO, attribs.layer[i].glindex);
120                         else if (attribs.layer[i].type == CD_NORMAL)
121                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_NORM, attribs.layer[i].glindex);
122                         else if (attribs.layer[i].type == CD_MCOL)
123                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_VCOL, attribs.layer[i].glindex);
124                         else
125                                 ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, attribs.layer[i].glindex);
126                 }
127         }
128 }
129
130 void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
131 {
132         float obmat[4][4], obcol[4];
133         GPUMaterial *gpumat;
134
135         gpumat = mGPUMat;
136
137         if (!gpumat || !GPU_material_bound(gpumat))
138                 return;
139
140         MT_Matrix4x4 model;
141         model.setValue(ms.m_OpenGLMatrix);
142
143         // note: getValue gives back column major as needed by OpenGL
144         model.getValue((float*)obmat);
145
146         if (ms.m_bObjectColor)
147                 ms.m_RGBAcolor.getValue((float *)obcol);
148         else
149                 obcol[0] = obcol[1] = obcol[2] = obcol[3] = 1.0f;
150
151         float auto_bump_scale = ms.m_pDerivedMesh!=0 ? ms.m_pDerivedMesh->auto_bump_scale : 1.0f;
152         GPU_material_bind_uniforms(gpumat, obmat, obcol, auto_bump_scale);
153
154         mAlphaBlend = GPU_material_alpha_blend(gpumat, obcol);
155 }
156
157 int BL_BlenderShader::GetAlphaBlend()
158 {
159         return mAlphaBlend;
160 }
161
162 bool BL_BlenderShader::Equals(BL_BlenderShader *blshader)
163 {
164         /* to avoid unneeded state switches */
165         return (blshader && mMat == blshader->mMat && mLightLayer == blshader->mLightLayer);
166 }
167
168 // eof