[Mesa-dev] [PATCH 10/29] mesa: Use bitmask/ffs to iterate enabled lights

Mathias.Froehlich at gmx.net Mathias.Froehlich at gmx.net
Tue Jun 14 04:59:53 UTC 2016


From: Mathias Fröhlich <mathias.froehlich at web.de>

Replaces loops that iterate all lights and test
which of them is enabled by a loop only iterating over
the bits set in the enabled bitmask.

v2: Use _mesa_bit_scan{,64} instead of open coding.
v3: Use u_bit_scan{,64} instead of _mesa_bit_scan{,64}.

Reviewed-by: Brian Paul <brianp at vmware.com>
Signed-off-by: Mathias Fröhlich <Mathias.Froehlich at web.de>
---
 src/mesa/main/light.c   | 44 +++++++++++++++++++++++++++++++++-----------
 src/mesa/main/rastpos.c |  9 ++++++---
 2 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/src/mesa/main/light.c b/src/mesa/main/light.c
index a52efdb..c9e2fc2 100644
--- a/src/mesa/main/light.c
+++ b/src/mesa/main/light.c
@@ -34,6 +34,7 @@
 #include "util/simple_list.h"
 #include "mtypes.h"
 #include "math/m_matrix.h"
+#include "util/bitscan.h"
 
 
 void GLAPIENTRY
@@ -612,7 +613,6 @@ _mesa_material_bitmask( struct gl_context *ctx, GLenum face, GLenum pname,
 void
 _mesa_update_material( struct gl_context *ctx, GLuint bitmask )
 {
-   struct gl_light *light, *list = &ctx->Light.EnabledList;
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
 
    if (MESA_VERBOSE & VERBOSE_MATERIAL) 
@@ -623,14 +623,20 @@ _mesa_update_material( struct gl_context *ctx, GLuint bitmask )
 
    /* update material ambience */
    if (bitmask & MAT_BIT_FRONT_AMBIENT) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
          SCALE_3V( light->_MatAmbient[0], light->Ambient, 
 		   mat[MAT_ATTRIB_FRONT_AMBIENT]);
       }
    }
 
    if (bitmask & MAT_BIT_BACK_AMBIENT) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
          SCALE_3V( light->_MatAmbient[1], light->Ambient, 
 		   mat[MAT_ATTRIB_BACK_AMBIENT]);
       }
@@ -651,14 +657,20 @@ _mesa_update_material( struct gl_context *ctx, GLuint bitmask )
 
    /* update material diffuse values */
    if (bitmask & MAT_BIT_FRONT_DIFFUSE) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
 	 SCALE_3V( light->_MatDiffuse[0], light->Diffuse, 
 		   mat[MAT_ATTRIB_FRONT_DIFFUSE] );
       }
    }
 
    if (bitmask & MAT_BIT_BACK_DIFFUSE) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
 	 SCALE_3V( light->_MatDiffuse[1], light->Diffuse, 
 		   mat[MAT_ATTRIB_BACK_DIFFUSE] );
       }
@@ -666,14 +678,20 @@ _mesa_update_material( struct gl_context *ctx, GLuint bitmask )
 
    /* update material specular values */
    if (bitmask & MAT_BIT_FRONT_SPECULAR) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
 	 SCALE_3V( light->_MatSpecular[0], light->Specular, 
 		   mat[MAT_ATTRIB_FRONT_SPECULAR]);
       }
    }
 
    if (bitmask & MAT_BIT_BACK_SPECULAR) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
 	 SCALE_3V( light->_MatSpecular[1], light->Specular,
 		   mat[MAT_ATTRIB_BACK_SPECULAR]);
       }
@@ -864,13 +882,15 @@ void
 _mesa_update_lighting( struct gl_context *ctx )
 {
    GLbitfield flags = 0;
-   struct gl_light *light;
    ctx->Light._NeedEyeCoords = GL_FALSE;
 
    if (!ctx->Light.Enabled)
       return;
 
-   foreach(light, &ctx->Light.EnabledList) {
+   GLbitfield mask = ctx->Light._EnabledLights;
+   while (mask) {
+      const int i = u_bit_scan(&mask);
+      struct gl_light *light = &ctx->Light.Light[i];
       flags |= light->_Flags;
    }
 
@@ -926,7 +946,6 @@ _mesa_update_lighting( struct gl_context *ctx )
 static void
 compute_light_positions( struct gl_context *ctx )
 {
-   struct gl_light *light;
    static const GLfloat eye_z[3] = { 0, 0, 1 };
 
    if (!ctx->Light.Enabled)
@@ -939,7 +958,10 @@ compute_light_positions( struct gl_context *ctx )
       TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelviewMatrixStack.Top->m );
    }
 
-   foreach (light, &ctx->Light.EnabledList) {
+   GLbitfield mask = ctx->Light._EnabledLights;
+   while (mask) {
+      const int i = u_bit_scan(&mask);
+      struct gl_light *light = &ctx->Light.Light[i];
 
       if (ctx->_NeedEyeCoords) {
          /* _Position is in eye coordinate space */
diff --git a/src/mesa/main/rastpos.c b/src/mesa/main/rastpos.c
index b468219..8f971f5 100644
--- a/src/mesa/main/rastpos.c
+++ b/src/mesa/main/rastpos.c
@@ -37,7 +37,7 @@
 #include "state.h"
 #include "main/dispatch.h"
 #include "main/viewport.h"
-#include "util/simple_list.h"
+#include "util/bitscan.h"
 
 
 
@@ -125,7 +125,7 @@ shade_rastpos(struct gl_context *ctx,
               GLfloat Rspec[4])
 {
    /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
-   const struct gl_light *light;
+   GLbitfield mask;
    GLfloat diffuseColor[4], specularColor[4];  /* for RGB mode only */
 
    COPY_3V(diffuseColor, base[0]);
@@ -133,7 +133,10 @@ shade_rastpos(struct gl_context *ctx,
       ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
    ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);
 
-   foreach (light, &ctx->Light.EnabledList) {
+   mask = ctx->Light._EnabledLights;
+   while (mask) {
+      const int i = u_bit_scan(&mask);
+      struct gl_light *light = &ctx->Light.Light[i];
       GLfloat attenuation = 1.0;
       GLfloat VP[3]; /* vector from vertex to light pos */
       GLfloat n_dot_VP;
-- 
2.5.5



More information about the mesa-dev mailing list