[AD] d3d filtering flags

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Only really tested on ex_filter and ex_haiku.  Can someone review it?

Peter
commit ed22c8bf864abb2848063899ab9f38a053fe3f8d
Author: Peter Wang <novalazy@xxxxxxxxxx>
Date:   Wed Nov 10 23:54:49 2010 +1100

    Implement min/mag filter and mipmap flags for Direct3D.
    
    Removed old support for config settings for filtering.

diff --git a/src/win/d3d_bmp.cpp b/src/win/d3d_bmp.cpp
index 03ee2eb..af87c43 100644
--- a/src/win/d3d_bmp.cpp
+++ b/src/win/d3d_bmp.cpp
@@ -165,10 +165,10 @@ static void d3d_sync_bitmap_memory(ALLEGRO_BITMAP *bitmap)
       texture = d3d_bmp->video_texture;
 
    if (texture->LockRect(0, &locked_rect, NULL, 0) == D3D_OK) {
-   _al_convert_bitmap_data(locked_rect.pBits, bitmap->format, locked_rect.Pitch,
-      bitmap->memory, bitmap->format, al_get_pixel_size(bitmap->format)*bitmap->w,
-      0, 0, 0, 0, bitmap->w, bitmap->h);
-   texture->UnlockRect(0);
+      _al_convert_bitmap_data(locked_rect.pBits, bitmap->format, locked_rect.Pitch,
+         bitmap->memory, bitmap->format, al_get_pixel_size(bitmap->format)*bitmap->w,
+         0, 0, 0, 0, bitmap->w, bitmap->h);
+      texture->UnlockRect(0);
    }
    else {
       ALLEGRO_ERROR("d3d_sync_bitmap_memory: Couldn't lock texture.\n");
@@ -300,26 +300,45 @@ void _al_d3d_release_default_pool_textures(void)
 }
 
 static bool d3d_create_textures(ALLEGRO_DISPLAY_D3D *disp, int w, int h,
+   int flags,
    LPDIRECT3DTEXTURE9 *video_texture, LPDIRECT3DTEXTURE9 *system_texture,
    int format)
 {
+   int levels;
+   int autogenmipmap;
+   int err;
+
+   if (flags & ALLEGRO_MIPMAP) {
+      /* "0" for all possible levels, required for auto mipmap generation. */
+      levels = 0;
+      autogenmipmap = D3DUSAGE_AUTOGENMIPMAP;
+   }
+   else {
+      levels = 1;
+      autogenmipmap = 0;
+   }
+
    if (_al_d3d_render_to_texture_supported()) {
       if (video_texture) {
-         if (disp->device->CreateTexture(w, h, 1,
-               D3DUSAGE_RENDERTARGET, (D3DFORMAT)_al_format_to_d3d(format), D3DPOOL_DEFAULT,
-               video_texture, NULL) != D3D_OK) {
+         err = disp->device->CreateTexture(w, h, levels,
+            D3DUSAGE_RENDERTARGET | autogenmipmap,
+            (D3DFORMAT)_al_format_to_d3d(format), D3DPOOL_DEFAULT,
+            video_texture, NULL);
+         if (err != D3D_OK && err != D3DOK_NOAUTOGEN) {
             ALLEGRO_ERROR("d3d_create_textures: Unable to create video texture.\n");
             return false;
          }
       }
 
       if (system_texture) {
-         if (disp->device->CreateTexture(w, h, 1,
-               0, (D3DFORMAT)_al_format_to_d3d(format), D3DPOOL_SYSTEMMEM,
-               system_texture, NULL) != D3D_OK) {
+         err = disp->device->CreateTexture(w, h, 1,
+            0, (D3DFORMAT)_al_format_to_d3d(format), D3DPOOL_SYSTEMMEM,
+            system_texture, NULL);
+         if (err != D3D_OK) {
             ALLEGRO_ERROR("d3d_create_textures: Unable to create system texture.\n");
             if (video_texture && (*video_texture)) {
                (*video_texture)->Release();
+               *video_texture = NULL;
             }
             return false;
          }
@@ -329,9 +348,10 @@ static bool d3d_create_textures(ALLEGRO_DISPLAY_D3D *disp, int w, int h,
    }
    else {
       if (video_texture) {
-         if (disp->device->CreateTexture(w, h, 1,
-               0, (D3DFORMAT)_al_format_to_d3d(format), D3DPOOL_MANAGED,
-               video_texture, NULL) != D3D_OK) {
+         err = disp->device->CreateTexture(w, h, 1,
+            0, (D3DFORMAT)_al_format_to_d3d(format), D3DPOOL_MANAGED,
+            video_texture, NULL);
+         if (err != D3D_OK) {
             ALLEGRO_ERROR("d3d_create_textures: Unable to create video texture (no render-to-texture).\n");
             return false;
          }
@@ -455,9 +475,11 @@ bool _al_d3d_recreate_bitmap_textures(ALLEGRO_DISPLAY_D3D *disp)
       ALLEGRO_BITMAP_D3D **bptr = (ALLEGRO_BITMAP_D3D **)_al_vector_ref(&created_bitmaps, i);
       ALLEGRO_BITMAP_D3D *bmp = *bptr;
       ALLEGRO_BITMAP *al_bmp = (ALLEGRO_BITMAP *)bmp;
+
       if (bmp->display == disp) {
 	      if (!d3d_create_textures(disp, bmp->texture_w,
 		    bmp->texture_h,
+		    al_bmp->flags,
 		    &bmp->video_texture,
 		    &bmp->system_texture,
 		    al_bmp->format))
@@ -482,7 +504,9 @@ void _al_d3d_refresh_texture_memory(void)
       ALLEGRO_BITMAP_D3D *bmp = *bptr;
       ALLEGRO_BITMAP *al_bmp = (ALLEGRO_BITMAP *)bmp;
       ALLEGRO_DISPLAY_D3D *bmps_display = (ALLEGRO_DISPLAY_D3D *)al_bmp->display;
+
       d3d_create_textures(bmps_display, bmp->texture_w, bmp->texture_h,
+         al_bmp->flags,
 	 &bmp->video_texture, /*&bmp->system_texture*/0, al_bmp->format);
       d3d_sync_bitmap_texture(al_bmp,
 	 0, 0, al_bmp->w, al_bmp->h);
@@ -526,6 +550,7 @@ static bool d3d_upload_bitmap(ALLEGRO_BITMAP *bitmap)
       if (d3d_bmp->video_texture == 0)
          if (!d3d_create_textures(d3d_bmp->display, d3d_bmp->texture_w,
                d3d_bmp->texture_h,
+               d3d_bmp->bitmap.flags,
                &d3d_bmp->video_texture,
                &d3d_bmp->system_texture,
                bitmap->format)) {
diff --git a/src/win/d3d_disp.cpp b/src/win/d3d_disp.cpp
index fbad61e..9083ca7 100644
--- a/src/win/d3d_disp.cpp
+++ b/src/win/d3d_disp.cpp
@@ -69,9 +69,6 @@ static bool is_vista = false;
 static int num_faux_fullscreen_windows = 0;
 static bool already_fullscreen = false; /* real fullscreen */
 
-static DWORD d3d_min_filter = D3DTEXF_POINT;
-static DWORD d3d_mag_filter = D3DTEXF_POINT;
-
 static ALLEGRO_MUTEX *present_mutex;
 ALLEGRO_MUTEX *_al_d3d_lost_device_mutex;
 
@@ -367,16 +364,6 @@ static int d3d_al_color_to_d3d(ALLEGRO_COLOR color)
    return result;
 }
 
-static DWORD d3d_get_filter(const char *s)
-{
-   if (!stricmp(s, "LINEAR"))
-      return D3DTEXF_LINEAR;
-   if (!stricmp(s, "ANISOTROPIC"))
-      return D3DTEXF_ANISOTROPIC;
-   return D3DTEXF_POINT;
-}
-
-
 static void d3d_reset_state(ALLEGRO_DISPLAY_D3D *disp)
 {
    if (disp->device_lost)
@@ -393,12 +380,6 @@ static void d3d_reset_state(ALLEGRO_DISPLAY_D3D *disp)
       ALLEGRO_ERROR("SetSamplerState failed\n");
    if (disp->device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP) != D3D_OK)
       ALLEGRO_ERROR("SetSamplerState failed\n");
-
-   /* Set up filtering */
-   if (disp->device->SetSamplerState(0, D3DSAMP_MINFILTER, d3d_min_filter) != D3D_OK)
-      ALLEGRO_ERROR("SetSamplerState failed\n");
-   if (disp->device->SetSamplerState(0, D3DSAMP_MAGFILTER, d3d_mag_filter) != D3D_OK)
-      ALLEGRO_ERROR("SetSamplerState failed\n");
 }
 
 void _al_d3d_get_current_ortho_projection_parameters(float *w, float *h)
@@ -1783,9 +1764,6 @@ static bool d3d_create_display_internals(ALLEGRO_DISPLAY_D3D *d3d_display)
    D3D_DISPLAY_PARAMETERS params;
    ALLEGRO_DISPLAY_WIN *win_display = &d3d_display->win_display;
    ALLEGRO_DISPLAY *al_display = &win_display->display;
-   static bool cfg_read = false;
-   ALLEGRO_SYSTEM *sys;
-   const char *s;
    ALLEGRO_EXTRA_DISPLAY_SETTINGS *ref =  _al_get_new_display_settings();
    int num_modes;
    int i;
@@ -1872,21 +1850,6 @@ static bool d3d_create_display_internals(ALLEGRO_DISPLAY_D3D *d3d_display)
       return false;
    }
 
-   if (!cfg_read) {
-      cfg_read = true;
-
-      sys = al_get_system_driver();
-
-      if (sys->config) {
-         s = al_get_config_value(sys->config, "graphics", "min_filter");
-         if (s)
-            d3d_min_filter = d3d_get_filter(s);
-         s = al_get_config_value(sys->config, "graphics", "mag_filter");
-         if (s)
-            d3d_mag_filter = d3d_get_filter(s);
-      }
-   }
-
    d3d_reset_state(d3d_display);
 
    //d3d_display->backbuffer_bmp.render_target = d3d_display->render_target;
@@ -2639,13 +2602,34 @@ static void d3d_flush_vertex_cache(ALLEGRO_DISPLAY* disp)
       return;
 
    ALLEGRO_DISPLAY_D3D* d3d_disp = (ALLEGRO_DISPLAY_D3D*)disp;
+   ALLEGRO_BITMAP* cache_bmp = (ALLEGRO_BITMAP*)disp->cache_texture;
+   ALLEGRO_BITMAP_D3D* d3d_bmp = (ALLEGRO_BITMAP_D3D*)cache_bmp;
 
    if (d3d_disp->device->SetTexture(0,
-         (IDirect3DBaseTexture9 *)((ALLEGRO_BITMAP_D3D*)disp->cache_texture)->video_texture) != D3D_OK) {
+         (IDirect3DBaseTexture9 *)d3d_bmp->video_texture) != D3D_OK) {
       ALLEGRO_ERROR("d3d_flush_vertex_cache: SetTexture failed.\n");
       return;
    }
 
+   if (cache_bmp->flags & ALLEGRO_MIN_LINEAR) {
+      d3d_disp->device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
+   }
+   else {
+      d3d_disp->device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
+   }
+   if (cache_bmp->flags & ALLEGRO_MAG_LINEAR) {
+      d3d_disp->device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
+   }
+   else {
+      d3d_disp->device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
+   }
+   if (cache_bmp->flags & ALLEGRO_MIPMAP) {
+      d3d_disp->device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
+   }
+   else {
+      d3d_disp->device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
+   }
+
    d3d_disp->device->SetFVF(D3DFVF_TL_VERTEX);
 
    if (d3d_disp->device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, disp->num_cache_vertices / 3,


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/