r300: Make sure to drop current hardware state reference to texture objects.
[mesa.git] / src / mesa / drivers / dri / r300 / r300_context.c
index 12308af45fd0f1458c9d6b6873392e1336207266..8f0effd83e2726ca2383f8f359ee15941f22462e 100644 (file)
@@ -27,20 +27,24 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 **************************************************************************/
 
-/*
- * Authors:
- *   Keith Whitwell <keith@tungstengraphics.com>
- *   Nicolai Haehnle <prefect_@gmx.net>
+/**
+ * \file
+ *
+ * \author Keith Whitwell <keith@tungstengraphics.com>
+ *
+ * \author Nicolai Haehnle <prefect_@gmx.net>
  */
-#include "glheader.h"
-#include "api_arrayelt.h"
-#include "context.h"
-#include "simple_list.h"
-#include "imports.h"
-#include "matrix.h"
-#include "extensions.h"
-#include "state.h"
-#include "bufferobj.h"
+
+#include "main/glheader.h"
+#include "main/api_arrayelt.h"
+#include "main/context.h"
+#include "main/simple_list.h"
+#include "main/imports.h"
+#include "main/matrix.h"
+#include "main/extensions.h"
+#include "main/state.h"
+#include "main/texobj.h"
+#include "main/bufferobj.h"
 
 #include "swrast/swrast.h"
 #include "swrast_setup/swrast_setup.h"
@@ -59,10 +63,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "r300_state.h"
 #include "r300_ioctl.h"
 #include "r300_tex.h"
-#include "r300_maos.h"
+#include "r300_emit.h"
+#include "r300_swtcl.h"
 
 #ifdef USER_BUFFERS
-#include "radeon_mm.h"
+#include "r300_mem.h"
 #endif
 
 #include "vblank.h"
@@ -73,42 +78,44 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 int future_hw_tcl_on = 1;
 int hw_tcl_on = 1;
 
-#define need_GL_EXT_stencil_two_side
-#define need_GL_ARB_multisample
-#define need_GL_ARB_texture_compression
-#define need_GL_ARB_vertex_buffer_object
+#define need_GL_VERSION_2_0
+#define need_GL_ARB_point_parameters
 #define need_GL_ARB_vertex_program
-#define need_GL_EXT_blend_minmax
-//#define need_GL_EXT_fog_coord
-#define need_GL_EXT_secondary_color
 #define need_GL_EXT_blend_equation_separate
 #define need_GL_EXT_blend_func_separate
+#define need_GL_EXT_blend_minmax
+#define need_GL_EXT_fog_coord
 #define need_GL_EXT_gpu_program_parameters
+#define need_GL_EXT_secondary_color
+#define need_GL_EXT_stencil_two_side
+#define need_GL_ATI_separate_stencil
 #define need_GL_NV_vertex_program
 #include "extension_helper.h"
 
 const struct dri_extension card_extensions[] = {
   /* *INDENT-OFF* */
-  {"GL_ARB_multisample",               GL_ARB_multisample_functions},
+  {"GL_ARB_depth_texture",             NULL},
+  {"GL_ARB_fragment_program",          NULL},
   {"GL_ARB_multitexture",              NULL},
+  {"GL_ARB_point_parameters",          GL_ARB_point_parameters_functions},
+  {"GL_ARB_shadow",                    NULL},
+  {"GL_ARB_shadow_ambient",            NULL},
   {"GL_ARB_texture_border_clamp",      NULL},
-  {"GL_ARB_texture_compression",       GL_ARB_texture_compression_functions},
   {"GL_ARB_texture_cube_map",          NULL},
   {"GL_ARB_texture_env_add",           NULL},
   {"GL_ARB_texture_env_combine",       NULL},
   {"GL_ARB_texture_env_crossbar",      NULL},
   {"GL_ARB_texture_env_dot3",          NULL},
   {"GL_ARB_texture_mirrored_repeat",   NULL},
-  {"GL_ARB_vertex_buffer_object",      GL_ARB_vertex_buffer_object_functions},
   {"GL_ARB_vertex_program",            GL_ARB_vertex_program_functions},
-  {"GL_ARB_fragment_program",          NULL},
   {"GL_EXT_blend_equation_separate",   GL_EXT_blend_equation_separate_functions},
   {"GL_EXT_blend_func_separate",       GL_EXT_blend_func_separate_functions},
   {"GL_EXT_blend_minmax",              GL_EXT_blend_minmax_functions},
   {"GL_EXT_blend_subtract",            NULL},
-//  {"GL_EXT_fog_coord",                       GL_EXT_fog_coord_functions },
+  {"GL_EXT_fog_coord",                 GL_EXT_fog_coord_functions },
   {"GL_EXT_gpu_program_parameters",     GL_EXT_gpu_program_parameters_functions},
   {"GL_EXT_secondary_color",           GL_EXT_secondary_color_functions},
+  {"GL_EXT_shadow_funcs",              NULL},
   {"GL_EXT_stencil_two_side",          GL_EXT_stencil_two_side_functions},
   {"GL_EXT_stencil_wrap",              NULL},
   {"GL_EXT_texture_edge_clamp",                NULL},
@@ -118,6 +125,7 @@ const struct dri_extension card_extensions[] = {
   {"GL_EXT_texture_lod_bias",          NULL},
   {"GL_EXT_texture_mirror_clamp",      NULL},
   {"GL_EXT_texture_rectangle",         NULL},
+  {"GL_ATI_separate_stencil",          GL_ATI_separate_stencil_functions},
   {"GL_ATI_texture_env_combine3",      NULL},
   {"GL_ATI_texture_mirror_once",       NULL},
   {"GL_MESA_pack_invert",              NULL},
@@ -130,6 +138,16 @@ const struct dri_extension card_extensions[] = {
   /* *INDENT-ON* */
 };
 
+
+/**
+ * The GL 2.0 functions are needed to make display lists work with
+ * functions added by GL_ATI_separate_stencil.
+ */
+const struct dri_extension gl_20_extension[] = {
+  {"GL_VERSION_2_0",                   GL_VERSION_2_0_functions },
+};
+
+
 extern struct tnl_pipeline_stage _r300_render_stage;
 extern const struct tnl_pipeline_stage _r300_tcl_stage;
 
@@ -200,8 +218,6 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        r300->initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache,
                                                     "def_max_anisotropy");
 
-       //r300->texmicrotile = GL_TRUE;
-
        /* Init default driver functions then plug in our R300-specific functions
         * (the texture functions are especially important)
         */
@@ -212,7 +228,7 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        r300InitShaderFuncs(&functions);
 
 #ifdef USER_BUFFERS
-       radeon_mm_init(r300);
+       r300_mem_init(r300);
 #endif
 
        if (!radeonInitContext(&r300->radeon, &functions,
@@ -271,6 +287,25 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
            MIN2(ctx->Const.MaxTextureImageUnits,
                 ctx->Const.MaxTextureCoordUnits);
        ctx->Const.MaxTextureMaxAnisotropy = 16.0;
+       ctx->Const.MaxTextureLodBias = 16.0;
+
+       if (screen->chip_family >= CHIP_FAMILY_RV515)
+           ctx->Const.MaxTextureLevels = 13;
+       else
+           ctx->Const.MaxTextureLevels = 12;
+
+        driCalculateMaxTextureLevels( r300->texture_heaps,
+                                      r300->nr_heaps,
+                                      & ctx->Const,
+                                      4,
+                                      ctx->Const.MaxTextureLevels - 1,
+                                      MIN2(ctx->Const.MaxTextureLevels,
+                                           MAX_3D_TEXTURE_LEVELS) - 1,
+                                      ctx->Const.MaxTextureLevels - 1,
+                                      ctx->Const.MaxTextureLevels - 1,
+                                      ctx->Const.MaxTextureLevels - 1,
+                                      GL_FALSE,
+                                      2 );
 
        ctx->Const.MinPointSize = 1.0;
        ctx->Const.MinPointSizeAA = 1.0;
@@ -290,6 +325,8 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
 #endif
 #endif
 
+       ctx->Const.MaxDrawBuffers = 1;
+
        /* Initialize the software rasterizer and helper modules.
         */
        _swrast_CreateContext(ctx);
@@ -316,15 +353,17 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        _tnl_allow_vertex_fog(ctx, GL_TRUE);
 
        /* currently bogus data */
-       ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
-       ctx->Const.VertexProgram.MaxNativeInstructions =
-           VSF_MAX_FRAGMENT_LENGTH / 4;
-       ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
-       ctx->Const.VertexProgram.MaxTemps = 32;
-       ctx->Const.VertexProgram.MaxNativeTemps =
-           /*VSF_MAX_FRAGMENT_TEMPS */ 32;
-       ctx->Const.VertexProgram.MaxNativeParameters = 256;     /* r420 */
-       ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
+       if (screen->chip_flags & RADEON_CHIPSET_TCL) {
+               ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
+               ctx->Const.VertexProgram.MaxNativeInstructions =
+                 VSF_MAX_FRAGMENT_LENGTH / 4;
+               ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
+               ctx->Const.VertexProgram.MaxTemps = 32;
+               ctx->Const.VertexProgram.MaxNativeTemps =
+                 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
+               ctx->Const.VertexProgram.MaxNativeParameters = 256;     /* r420 */
+               ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
+       }
 
        ctx->Const.FragmentProgram.MaxNativeTemps = PFS_NUM_TEMP_REGS;
        ctx->Const.FragmentProgram.MaxNativeAttribs = 11;       /* copy i915... */
@@ -336,7 +375,7 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        ctx->Const.FragmentProgram.MaxNativeTexIndirections =
            PFS_MAX_TEX_INDIRECT;
        ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;    /* and these are?? */
-       _tnl_ProgramCacheInit(ctx);
+       ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
        ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
 
        driInitExtensions(ctx, card_extensions, GL_TRUE);
@@ -362,6 +401,8 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        radeonInitSpanFuncs(ctx);
        r300InitCmdBuf(r300);
        r300InitState(r300);
+       if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
+               r300InitSwtcl(ctx);
 
        TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
 
@@ -446,15 +487,6 @@ static void r300FreeGartAllocations(r300ContextPtr r300)
 
                        r300->rmm->u_list[i].pending = 0;
                        r300->rmm->u_list[i].ptr = NULL;
-                       if (r300->rmm->u_list[i].fb) {
-                               LOCK_HARDWARE(&(r300->radeon));
-                               ret = mmFreeMem(r300->rmm->u_list[i].fb);
-                               UNLOCK_HARDWARE(&(r300->radeon));
-                               if (ret)
-                                       fprintf(stderr, "failed to free!\n");
-                               r300->rmm->u_list[i].fb = NULL;
-                       }
-                       r300->rmm->u_list[i].ref_count = 0;
                }
        }
        r300->rmm->u_head = i;
@@ -469,6 +501,7 @@ void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
        r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
        radeonContextPtr radeon = (radeonContextPtr) r300;
        radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
+       int i;
 
        if (RADEON_DEBUG & DEBUG_DRI) {
                fprintf(stderr, "Destroying context !\n");
@@ -489,7 +522,6 @@ void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
                release_texture_heaps =
                    (r300->radeon.glCtx->Shared->RefCount == 1);
                _swsetup_DestroyContext(r300->radeon.glCtx);
-               _tnl_ProgramCacheDestroy(r300->radeon.glCtx);
                _tnl_DestroyContext(r300->radeon.glCtx);
                _vbo_DestroyContext(r300->radeon.glCtx);
                _swrast_DestroyContext(r300->radeon.glCtx);
@@ -523,13 +555,18 @@ void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
                        assert(is_empty_list(&r300->swapped));
                }
 
+                /* Drop texture object references from current hardware state */
+               for (i = 0; i < 8; i++) {
+                       _mesa_reference_texobj(&r300->state.texture.unit[i].texobj, NULL);
+               }
+
                radeonCleanupContext(&r300->radeon);
 
 #ifdef USER_BUFFERS
                /* the memory manager might be accessed when Mesa frees the shared
                 * state, so don't destroy it earlier
                 */
-               radeon_mm_destroy(r300);
+               r300_mem_destroy(r300);
 #endif
 
                /* free the option cache */