st/wgl: refactor stw_pixelformat_init()
[mesa.git] / src / gallium / state_trackers / wgl / stw_pixelformat.c
index e606477e975fb6d02c698273a1a4ec89ea3f4dd0..b74b71b1ba69467f4896854eaaedb49f4e4ff433 100644 (file)
  * 
  **************************************************************************/
 
-#include "main/mtypes.h"
-#include "main/context.h"
-
 #include "pipe/p_format.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_screen.h"
 
 #include "util/u_format.h"
 #include "util/u_debug.h"
+#include "util/u_memory.h"
 
 #include "stw_icd.h"
 #include "stw_device.h"
@@ -87,6 +85,10 @@ stw_pf_color[] = {
    { PIPE_FORMAT_B4G4R4A4_UNORM,    { 4,  4,  4,  4}, {16,  4,  0, 12} }
 };
 
+static const struct stw_pf_color_info
+stw_pf_color_extended[] = {
+    { PIPE_FORMAT_R32G32B32A32_FLOAT, { 32,  32, 32,  32}, { 0,  32, 64, 96} }
+};
 
 static const struct stw_pf_depth_info 
 stw_pf_depth_stencil[] = {
@@ -96,8 +98,8 @@ stw_pf_depth_stencil[] = {
    { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
    { PIPE_FORMAT_Z16_UNORM,   {16, 0} },
    /* combined depth-stencil */
-   { PIPE_FORMAT_Z24_UNORM_S8_USCALED, {24, 8} },
-   { PIPE_FORMAT_S8_USCALED_Z24_UNORM, {24, 8} }
+   { PIPE_FORMAT_Z24_UNORM_S8_UINT, {24, 8} },
+   { PIPE_FORMAT_S8_UINT_Z24_UNORM, {24, 8} }
 };
 
 
@@ -118,13 +120,13 @@ stw_pf_multisample[] = {
 static void
 stw_pixelformat_add(
    struct stw_device *stw_dev,
+   boolean extended,
    const struct stw_pf_color_info *color,
    const struct stw_pf_depth_info *depth,
    unsigned accum,
    boolean doublebuffer,
    unsigned samples )
 {
-   boolean extended = FALSE;
    struct stw_pixelformat_info *pfi;
    
    assert(stw_dev->pixelformat_extended_count < STW_MAX_PIXELFORMATS);
@@ -148,7 +150,9 @@ stw_pixelformat_add(
    pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
    
    /* TODO: also support non-native pixel formats */
-   pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
+   if (!extended) {
+      pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
+   }
 
    /* See http://www.opengl.org/pipeline/article/vol003_7/ */
    pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
@@ -187,7 +191,7 @@ stw_pixelformat_add(
     */
    pfi->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
    if (doublebuffer)
-      pfi->stvis.buffer_mask = ST_ATTACHMENT_BACK_LEFT_MASK;
+      pfi->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
 
    pfi->stvis.color_format = color->format;
    pfi->stvis.depth_stencil_format = depth->format;
@@ -206,47 +210,74 @@ stw_pixelformat_add(
    }
 }
 
-void
-stw_pixelformat_init( void )
+
+/**
+ * Add the depth/stencil/accum/ms variants for a particular color format.
+ */
+static void
+add_color_format_variants(const struct stw_pf_color_info *color,
+                          boolean extended)
 {
    struct pipe_screen *screen = stw_dev->screen;
-   unsigned i, j, k, l;
-   
-   assert( !stw_dev->pixelformat_count );
-   assert( !stw_dev->pixelformat_extended_count );
+   unsigned ms, db, ds, acc;
+   unsigned bind_flags = PIPE_BIND_RENDER_TARGET;
+
+   if (!extended) {
+      bind_flags |= PIPE_BIND_DISPLAY_TARGET;
+   }
+
+   if (!screen->is_format_supported(screen, color->format,
+                                    PIPE_TEXTURE_2D, 0, bind_flags)) {
+      return;
+   }
+
+   for (ms = 0; ms < Elements(stw_pf_multisample); ms++) {
+      unsigned samples = stw_pf_multisample[ms];
 
-   for(i = 0; i < Elements(stw_pf_multisample); ++i) {
-      unsigned samples = stw_pf_multisample[i];
-      
       /* FIXME: re-enabled MSAA when we can query it */
-      if(samples)
+      if (samples)
          continue;
 
-      for(j = 0; j < Elements(stw_pf_color); ++j) {
-         const struct stw_pf_color_info *color = &stw_pf_color[j];
-         
-         if(!screen->is_format_supported(screen, color->format, PIPE_TEXTURE_2D,
-                                         0, PIPE_BIND_RENDER_TARGET |
-                                         PIPE_BIND_DISPLAY_TARGET, 0))
-            continue;
-         
-         for(k = 0; k < Elements(stw_pf_doublebuffer); ++k) {
-            unsigned doublebuffer = stw_pf_doublebuffer[k];
-            
-            for(l = 0; l < Elements(stw_pf_depth_stencil); ++l) {
-               const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[l];
-               
-               if(!screen->is_format_supported(screen, depth->format, PIPE_TEXTURE_2D, 
-                                               0, PIPE_BIND_DEPTH_STENCIL, 0))
-                  continue;
-
-               stw_pixelformat_add( stw_dev, color, depth,  0, doublebuffer, samples );
-               stw_pixelformat_add( stw_dev, color, depth, 16, doublebuffer, samples );
+      for (db = 0; db < Elements(stw_pf_doublebuffer); db++) {
+         unsigned doublebuffer = stw_pf_doublebuffer[db];
+
+         for (ds = 0; ds < Elements(stw_pf_depth_stencil); ds++) {
+            const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[ds];
+
+            if (!screen->is_format_supported(screen, depth->format,
+                                             PIPE_TEXTURE_2D, 0,
+                                             PIPE_BIND_DEPTH_STENCIL)) {
+               continue;
+            }
+
+            for (acc = 0; acc < 2; acc++) {
+               stw_pixelformat_add(stw_dev, extended, color, depth,
+                                   acc * 16, doublebuffer, samples);
             }
          }
       }
    }
-   
+}
+
+
+void
+stw_pixelformat_init( void )
+{
+   unsigned i;
+
+   assert( !stw_dev->pixelformat_count );
+   assert( !stw_dev->pixelformat_extended_count );
+
+   /* normal, displayable formats */
+   for (i = 0; i < Elements(stw_pf_color); i++) {
+      add_color_format_variants(&stw_pf_color[i], FALSE);
+   }
+
+   /* extended, pbuffer-only formats */
+   for (i = 0; i < Elements(stw_pf_color_extended); i++) {
+      add_color_format_variants(&stw_pf_color_extended[i], TRUE);
+   }
+
    assert( stw_dev->pixelformat_count <= stw_dev->pixelformat_extended_count );
    assert( stw_dev->pixelformat_extended_count <= STW_MAX_PIXELFORMATS );
 }
@@ -264,9 +295,18 @@ stw_pixelformat_get_extended_count( void )
 }
 
 const struct stw_pixelformat_info *
-stw_pixelformat_get_info( uint index )
+stw_pixelformat_get_info( int iPixelFormat )
 {
-   assert( index < stw_dev->pixelformat_extended_count );
+   int index;
+
+   if (iPixelFormat <= 0) {
+      return NULL;
+   }
+
+   index = iPixelFormat - 1;
+   if (index >= stw_dev->pixelformat_extended_count) {
+      return NULL;
+   }
 
    return &stw_dev->pixelformats[index];
 }
@@ -280,7 +320,6 @@ DrvDescribePixelFormat(
    PIXELFORMATDESCRIPTOR *ppfd )
 {
    uint count;
-   uint index;
    const struct stw_pixelformat_info *pfi;
 
    (void) hdc;
@@ -288,15 +327,17 @@ DrvDescribePixelFormat(
    if (!stw_dev)
       return 0;
 
-   count = stw_pixelformat_get_extended_count();
-   index = (uint) iPixelFormat - 1;
+   count = stw_pixelformat_get_count();
 
    if (ppfd == NULL)
       return count;
-   if (index >= count || cjpfd != sizeof( PIXELFORMATDESCRIPTOR ))
+   if (cjpfd != sizeof( PIXELFORMATDESCRIPTOR ))
       return 0;
 
-   pfi = stw_pixelformat_get_info( index );
+   pfi = stw_pixelformat_get_info( iPixelFormat );
+   if (!pfi) {
+      return 0;
+   }
    
    memcpy(ppfd, &pfi->pfd, sizeof( PIXELFORMATDESCRIPTOR ));
 
@@ -362,11 +403,11 @@ int stw_pixelformat_choose( HDC hdc,
 
    (void) hdc;
 
-   count = stw_pixelformat_get_count();
-   bestindex = count;
+   count = stw_pixelformat_get_extended_count();
+   bestindex = 0;
    bestdelta = ~0U;
 
-   for (index = 0; index < count; index++) {
+   for (index = 1; index <= count; index++) {
       uint delta = 0;
       const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info( index );
 
@@ -396,8 +437,5 @@ int stw_pixelformat_choose( HDC hdc,
       }
    }
 
-   if (bestindex == count)
-      return 0;
-
-   return bestindex + 1;
+   return bestindex;
 }