Fix for [#34754] "Revision 55527 provokes glitchy GLSL shadow map rendering" reported...
authorMitchell Stokes <mogurijin@gmail.com>
Thu, 4 Apr 2013 03:55:43 +0000 (03:55 +0000)
committerMitchell Stokes <mogurijin@gmail.com>
Thu, 4 Apr 2013 03:55:43 +0000 (03:55 +0000)
source/blender/gpu/GPU_material.h
source/blender/gpu/intern/gpu_draw.c
source/blender/gpu/intern/gpu_material.c
source/gameengine/Ketsji/BL_BlenderShader.cpp
source/gameengine/Ketsji/BL_BlenderShader.h
source/gameengine/Ketsji/KX_BlenderMaterial.cpp

index 17d3ce3cd73532049d07d7943875934b1dc6d553..29da72a00fe60bb6649ff0d74278db7bc8234084 100644 (file)
@@ -129,8 +129,8 @@ void GPU_material_free(struct Material *ma);
 
 void GPU_materials_free(void);
 
-void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap);
-void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float viewmat[4][4], float viewinv[4][4], float obcol[4], float autobumpscale);
+void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap, float viewmat[4][4], float viewinv[4][4]);
+void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float obcol[4], float autobumpscale);
 void GPU_material_unbind(GPUMaterial *material);
 int GPU_material_bound(GPUMaterial *material);
 
index b772507e0cc881514c195cf5b24a2643c83adc97..c78961e6308059946b0ae324d72d2518c456e166 100644 (file)
@@ -1457,10 +1457,10 @@ int GPU_enable_material(int nr, void *attribs)
 
                        gpumat = GPU_material_from_blender(GMS.gscene, mat);
                        GPU_material_vertex_attributes(gpumat, gattribs);
-                       GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT));
+                       GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT), GMS.gviewmat, GMS.gviewinv);
 
                        auto_bump_scale = GMS.gob->derivedFinal != NULL ? GMS.gob->derivedFinal->auto_bump_scale : 1.0f;
-                       GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gviewmat, GMS.gviewinv, GMS.gob->col, auto_bump_scale);
+                       GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gob->col, auto_bump_scale);
                        GMS.gboundmat= mat;
 
                        /* for glsl use alpha blend mode, unless it's set to solid and
index 999e3b5c20ea2a1ddf75d2d590ceb8d2bdfcf23e..075a670e06efdb047ed8b36debff93cba4dfb516 100644 (file)
@@ -257,11 +257,12 @@ void GPU_material_free(Material *ma)
        BLI_freelistN(&ma->gpumaterial);
 }
 
-void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap)
+void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap, float viewmat[4][4], float viewinv[4][4])
 {
        if (material->pass) {
                LinkData *nlink;
                GPULamp *lamp;
+               GPUShader *shader = GPU_pass_shader(material->pass);
 
                /* handle layer lamps */
                for (nlink=material->lamps.first; nlink; nlink=nlink->next) {
@@ -275,29 +276,52 @@ void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double tim
                                lamp->dynenergy = 0.0f;
                                lamp->dyncol[0]= lamp->dyncol[1]= lamp->dyncol[2] = 0.0f;
                        }
+
+                       if (material->dynproperty & DYN_LAMP_VEC) {
+                               copy_v3_v3(lamp->dynvec, lamp->vec);
+                               normalize_v3(lamp->dynvec);
+                               negate_v3(lamp->dynvec);
+                               mul_mat3_m4_v3(viewmat, lamp->dynvec);
+                       }
+
+                       if (material->dynproperty & DYN_LAMP_CO) {
+                               copy_v3_v3(lamp->dynco, lamp->co);
+                               mul_m4_v3(viewmat, lamp->dynco);
+                       }
+
+                       if (material->dynproperty & DYN_LAMP_IMAT) {
+                               mult_m4_m4m4(lamp->dynimat, lamp->imat, viewinv);
+                       }
+
+                       if (material->dynproperty & DYN_LAMP_PERSMAT) {
+                               if (!GPU_lamp_has_shadow_buffer(lamp)) /* The lamp matrices are already updated if we're using shadow buffers */
+                                       GPU_lamp_update_buffer_mats(lamp);
+                               mult_m4_m4m4(lamp->dynpersmat, lamp->persmat, viewinv);
+                       }
+               }
+
+               /* handle per material built-ins */
+               if (material->builtins & GPU_VIEW_MATRIX) {
+                       GPU_shader_uniform_vector(shader, material->viewmatloc, 16, 1, (float*)viewmat);
+               }
+               if (material->builtins & GPU_INVERSE_VIEW_MATRIX) {
+                       GPU_shader_uniform_vector(shader, material->invviewmatloc, 16, 1, (float*)viewinv);
                }
 
                GPU_pass_bind(material->pass, time, mipmap);
                GPU_pass_update_uniforms(material->pass);
+
                material->bound = 1;
        }
 }
 
-void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float viewmat[4][4], float viewinv[4][4], float obcol[4], float autobumpscale)
+void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float obcol[4], float autobumpscale)
 {
        if (material->pass) {
                GPUShader *shader = GPU_pass_shader(material->pass);
-               LinkData *nlink;
-               GPULamp *lamp;
                float invmat[4][4], col[4];
 
-               /* handle builtins */
-               if (material->builtins & GPU_VIEW_MATRIX) {
-                       GPU_shader_uniform_vector(shader, material->viewmatloc, 16, 1, (float*)viewmat);
-               }
-               if (material->builtins & GPU_INVERSE_VIEW_MATRIX) {
-                       GPU_shader_uniform_vector(shader, material->invviewmatloc, 16, 1, (float*)viewinv);
-               }
+               /* handle per object builtins */
                if (material->builtins & GPU_OBJECT_MATRIX) {
                        GPU_shader_uniform_vector(shader, material->obmatloc, 16, 1, (float*)obmat);
                }
@@ -313,32 +337,6 @@ void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[4][4], float
                if (material->builtins & GPU_AUTO_BUMPSCALE) {
                        GPU_shader_uniform_vector(shader, material->obautobumpscaleloc, 1, 1, &autobumpscale);
                }
-               /* update lamps */
-               for (nlink=material->lamps.first; nlink; nlink=nlink->next) {
-                       lamp= nlink->data;
-
-                       if (material->dynproperty & DYN_LAMP_VEC) {
-                               copy_v3_v3(lamp->dynvec, lamp->vec);
-                               normalize_v3(lamp->dynvec);
-                               negate_v3(lamp->dynvec);
-                               mul_mat3_m4_v3(viewmat, lamp->dynvec);
-                       }
-
-                       if (material->dynproperty & DYN_LAMP_CO) {
-                               copy_v3_v3(lamp->dynco, lamp->co);
-                               mul_m4_v3(viewmat, lamp->dynco);
-                       }
-
-                       if (material->dynproperty & DYN_LAMP_IMAT) {
-                               mult_m4_m4m4(lamp->dynimat, lamp->imat, viewinv);
-                       }
-
-                       if (material->dynproperty & DYN_LAMP_PERSMAT) {
-                               if (!GPU_lamp_has_shadow_buffer(lamp)) /* The lamp matrices are already updated if we're using shadow buffers */
-                                       GPU_lamp_update_buffer_mats(lamp);
-                               mult_m4_m4m4(lamp->dynpersmat, lamp->persmat, viewinv);
-                       }
-               }
        }
 }
 
index f5f9b344b87dd8b641f831c0c914b0a78334f086..3f2a9a6df6a548069fa885038c6a4d0732248fa8 100644 (file)
@@ -46,11 +46,20 @@ void BL_BlenderShader::ReloadMaterial()
        mGPUMat = (mMat) ? GPU_material_from_blender(mBlenderScene, mMat) : NULL;
 }
 
-void BL_BlenderShader::SetProg(bool enable, double time)
+void BL_BlenderShader::SetProg(bool enable, double time, RAS_IRasterizer* rasty)
 {
        if (VerifyShader()) {
-               if (enable)
-                       GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1);
+               if (enable) {
+                       assert(rasty != NULL); // XXX Kinda hacky, but SetProg() should always have the rasterizer if enable is true
+
+                       float viewmat[4][4], viewinvmat[4][4];
+                       const MT_Matrix4x4& view = rasty->GetViewMatrix();
+                       const MT_Matrix4x4& viewinv = rasty->GetViewInvMatrix();
+                       view.getValue((float*)viewmat);
+                       viewinv.getValue((float*)viewinvmat);
+
+                       GPU_material_bind(mGPUMat, mLightLayer, mBlenderScene->lay, time, 1, viewmat, viewinvmat);
+               }
                else
                        GPU_material_unbind(mGPUMat);
        }
@@ -120,7 +129,7 @@ void BL_BlenderShader::SetAttribs(RAS_IRasterizer* ras, const BL_Material *mat)
 
 void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
 {
-       float obmat[4][4], viewmat[4][4], viewinvmat[4][4], obcol[4];
+       float obmat[4][4], obcol[4];
        GPUMaterial *gpumat;
 
        gpumat = mGPUMat;
@@ -130,13 +139,9 @@ void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
 
        MT_Matrix4x4 model;
        model.setValue(ms.m_OpenGLMatrix);
-       const MT_Matrix4x4& view = rasty->GetViewMatrix();
-       const MT_Matrix4x4& viewinv = rasty->GetViewInvMatrix();
 
        // note: getValue gives back column major as needed by OpenGL
        model.getValue((float*)obmat);
-       view.getValue((float*)viewmat);
-       viewinv.getValue((float*)viewinvmat);
 
        if (ms.m_bObjectColor)
                ms.m_RGBAcolor.getValue((float *)obcol);
@@ -144,7 +149,7 @@ void BL_BlenderShader::Update(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty )
                obcol[0] = obcol[1] = obcol[2] = obcol[3] = 1.0f;
 
        float auto_bump_scale = ms.m_pDerivedMesh!=0 ? ms.m_pDerivedMesh->auto_bump_scale : 1.0f;
-       GPU_material_bind_uniforms(gpumat, obmat, viewmat, viewinvmat, obcol, auto_bump_scale);
+       GPU_material_bind_uniforms(gpumat, obmat, obcol, auto_bump_scale);
 
        mAlphaBlend = GPU_material_alpha_blend(gpumat, obcol);
 }
index 626c65baadd675b88fd5b7ec15d71343bc7f41fb..71f66c2a49dab843aa5d128b7e30d92f399aa4c4 100644 (file)
@@ -78,7 +78,7 @@ public:
                // same as VerifyShared
                return (NULL != mGPUMat);
        }
-       void                            SetProg(bool enable, double time=0.0);
+       void                            SetProg(bool enable, double time=0.0, RAS_IRasterizer* rasty=NULL);
 
        int GetAttribNum();
        void SetAttribs(class RAS_IRasterizer* ras, const BL_Material *mat);
index 231ec27030db90ad5110aa163d045f464a06f158..853cfa53da216b11b614294dea5169b7e3e709a7 100644 (file)
@@ -314,7 +314,7 @@ void KX_BlenderMaterial::setBlenderShaderData( bool enable, RAS_IRasterizer *ras
                else
                        BL_Texture::DisableAllTextures();
 
-               mBlenderShader->SetProg(true, ras->GetTime());
+               mBlenderShader->SetProg(true, ras->GetTime(), ras);
                mLastBlenderShader= mBlenderShader;
        }
 }