radeon/r200/r300: merge span code into single shared file
[mesa.git] / src / mesa / drivers / dri / r300 / r300_context.c
index c56a76228974e44f5251202384e506b76adea411..3bd54f7dff90bb9d3699219837661a05dcda2ec6 100644 (file)
@@ -35,15 +35,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  * \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/bufferobj.h"
 
 #include "swrast/swrast.h"
 #include "swrast_setup/swrast_setup.h"
@@ -64,10 +64,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "r300_tex.h"
 #include "r300_emit.h"
 #include "r300_swtcl.h"
+#include "radeon_bo_legacy.h"
 
-#ifdef USER_BUFFERS
-#include "r300_mem.h"
-#endif
 
 #include "vblank.h"
 #include "utils.h"
@@ -79,6 +77,7 @@ int hw_tcl_on = 1;
 
 #define need_GL_EXT_stencil_two_side
 #define need_GL_ARB_multisample
+#define need_GL_ARB_point_parameters
 #define need_GL_ARB_texture_compression
 #define need_GL_ARB_vertex_buffer_object
 #define need_GL_ARB_vertex_program
@@ -98,6 +97,9 @@ const struct dri_extension card_extensions[] = {
   {"GL_ARB_fragment_program",          NULL},
   {"GL_ARB_multisample",               GL_ARB_multisample_functions},
   {"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},
@@ -116,6 +118,7 @@ const struct dri_extension card_extensions[] = {
   {"GL_EXT_multi_draw_arrays",         GL_EXT_multi_draw_arrays_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},
@@ -133,7 +136,6 @@ const struct dri_extension card_extensions[] = {
   {"GL_NV_blend_square",               NULL},
   {"GL_NV_vertex_program",             GL_NV_vertex_program_functions},
   {"GL_SGIS_generate_mipmap",          NULL},
-  {"GL_SGIX_depth_texture",            NULL},
   {NULL,                               NULL}
   /* *INDENT-ON* */
 };
@@ -174,6 +176,92 @@ static const struct tnl_pipeline_stage *r300_pipeline[] = {
        0,
 };
 
+static void r300RunPipeline(GLcontext * ctx)
+{
+    _mesa_lock_context_textures(ctx);
+
+    if (ctx->NewState)
+        _mesa_update_state_locked(ctx);
+    
+    _tnl_run_pipeline(ctx);
+    _mesa_unlock_context_textures(ctx);
+}
+
+static void r300_get_lock(radeonContextPtr rmesa)
+{
+       drm_radeon_sarea_t *sarea = rmesa->sarea;
+
+       if (sarea->ctx_owner != rmesa->dri.hwContext) {
+               sarea->ctx_owner = rmesa->dri.hwContext;
+               if (!rmesa->radeonScreen->kernel_mm)
+                       radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
+       }
+}                
+
+static void r300_vtbl_flush(GLcontext *ctx)
+{
+       r300Flush(ctx);
+}
+
+static void r300_vtbl_set_all_dirty(GLcontext *ctx)
+{
+       r300ContextPtr rmesa = R300_CONTEXT(ctx);
+       rmesa->hw.all_dirty = GL_TRUE;
+}
+
+static void r300_vtbl_emit_state(radeonContextPtr rmesa)
+{
+       r300EmitState((r300ContextPtr)rmesa);
+}
+
+static void r300_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
+{
+    /* please flush pipe do all pending work */
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_SC_SCREENDOOR, 1));
+    radeon_cs_write_dword(cs, 0x0);
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_SC_SCREENDOOR, 1));
+    radeon_cs_write_dword(cs, 0x00FFFFFF);
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_SC_HYPERZ, 1));
+    radeon_cs_write_dword(cs, 0x0);
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_US_CONFIG, 1));
+    radeon_cs_write_dword(cs, 0x0);
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_ZB_CNTL, 1));
+    radeon_cs_write_dword(cs, 0x0);
+    radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen, R300_WAIT_3D));
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_RB3D_DSTCACHE_CTLSTAT, 1));
+    radeon_cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
+    radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
+                                  R300_ZB_ZCACHE_CTLSTAT, 1));
+    radeon_cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
+    radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen,
+                               R300_WAIT_3D | R300_WAIT_3D_CLEAN));
+}
+
+static void r300_vtbl_flush_vertices(radeonContextPtr rmesa)
+{
+   R300_FIREVERTICES(((r300ContextPtr)rmesa));
+}
+
+static void r300_init_vtbl(radeonContextPtr radeon)
+{
+   radeon->vtbl.get_lock = r300_get_lock;
+   radeon->vtbl.update_viewport_offset = r300UpdateViewportOffset;
+   radeon->vtbl.flush = r300_vtbl_flush;
+   radeon->vtbl.set_all_dirty = r300_vtbl_set_all_dirty;
+   radeon->vtbl.update_draw_buffer = r300UpdateDrawBuffer;
+   radeon->vtbl.emit_cs_header = r300_vtbl_emit_cs_header;
+   radeon->vtbl.emit_state = r300_vtbl_emit_state;
+   radeon->vtbl.flush_vertices = r300_vtbl_flush_vertices;
+   radeon->vtbl.swtcl_flush = r300_swtcl_flush;
+}
+
+
 /* Create the device specific rendering context.
  */
 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
@@ -185,7 +273,7 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        struct dd_function_table functions;
        r300ContextPtr r300;
        GLcontext *ctx;
-       int tcl_mode, i;
+       int tcl_mode;
 
        assert(glVisual);
        assert(driContextPriv);
@@ -199,13 +287,14 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
                hw_tcl_on = future_hw_tcl_on = 0;
 
+       r300_init_vtbl(&r300->radeon);
        /* Parse configuration files.
         * Do this here so that initialMaxAnisotropy is set before we create
         * the default textures.
         */
        driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
                            screen->driScreen->myNum, "r300");
-       r300->initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache,
+       r300->radeon.initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache,
                                                     "def_max_anisotropy");
 
        /* Init default driver functions then plug in our R300-specific functions
@@ -217,10 +306,6 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        r300InitTextureFuncs(&functions);
        r300InitShaderFuncs(&functions);
 
-#ifdef USER_BUFFERS
-       r300_mem_init(r300);
-#endif
-
        if (!radeonInitContext(&r300->radeon, &functions,
                               glVisual, driContextPriv,
                               sharedContextPrivate)) {
@@ -229,37 +314,10 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        }
 
        /* Init r300 context data */
-       r300->dma.buf0_address =
-           r300->radeon.radeonScreen->buffers->list[0].address;
-
-       (void)memset(r300->texture_heaps, 0, sizeof(r300->texture_heaps));
-       make_empty_list(&r300->swapped);
-
-       r300->nr_heaps = 1 /* screen->numTexHeaps */ ;
-       assert(r300->nr_heaps < RADEON_NR_TEX_HEAPS);
-       for (i = 0; i < r300->nr_heaps; i++) {
-               /* *INDENT-OFF* */
-               r300->texture_heaps[i] = driCreateTextureHeap(i, r300,
-                                                              screen->
-                                                              texSize[i], 12,
-                                                              RADEON_NR_TEX_REGIONS,
-                                                              (drmTextureRegionPtr)
-                                                              r300->radeon.sarea->
-                                                              tex_list[i],
-                                                              &r300->radeon.sarea->
-                                                              tex_age[i],
-                                                              &r300->swapped,
-                                                              sizeof
-                                                              (r300TexObj),
-                                                              (destroy_texture_object_t
-                                                               *)
-                                                              r300DestroyTexObj);
-               /* *INDENT-ON* */
-       }
-       r300->texture_depth = driQueryOptioni(&r300->radeon.optionCache,
+       r300->radeon.texture_depth = driQueryOptioni(&r300->radeon.optionCache,
                                              "texture_depth");
-       if (r300->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
-               r300->texture_depth = (screen->cpp == 4) ?
+       if (r300->radeon.texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
+               r300->radeon.texture_depth = (screen->cpp == 4) ?
                    DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
 
        /* Set the maximum texture size small enough that we can guarentee that
@@ -277,6 +335,12 @@ 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;
+           ctx->Const.MaxTextureRectSize = 4096;
+       }
 
        ctx->Const.MinPointSize = 1.0;
        ctx->Const.MinPointSizeAA = 1.0;
@@ -288,12 +352,10 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
        ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
 
-#ifdef USER_BUFFERS
        /* Needs further modifications */
 #if 0
        ctx->Const.MaxArrayLockSize =
            ( /*512 */ RADEON_BUFFER_SIZE * 16 * 1024) / (4 * 4);
-#endif
 #endif
 
        /* Initialize the software rasterizer and helper modules.
@@ -344,7 +406,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);
@@ -367,13 +429,12 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
            driQueryOptionb(&r300->radeon.optionCache,
                            "disable_lowimpact_fallback");
 
-       radeonInitSpanFuncs(ctx);
        r300InitCmdBuf(r300);
        r300InitState(r300);
        if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
                r300InitSwtcl(ctx);
 
-       TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
+       TNL_CONTEXT(ctx)->Driver.RunPipeline = r300RunPipeline;
 
        tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
        if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
@@ -396,72 +457,6 @@ GLboolean r300CreateContext(const __GLcontextModes * glVisual,
        return GL_TRUE;
 }
 
-static void r300FreeGartAllocations(r300ContextPtr r300)
-{
-       int i, ret, tries = 0, done_age, in_use = 0;
-       drm_radeon_mem_free_t memfree;
-
-       memfree.region = RADEON_MEM_REGION_GART;
-
-#ifdef USER_BUFFERS
-       for (i = r300->rmm->u_last; i > 0; i--) {
-               if (r300->rmm->u_list[i].ptr == NULL) {
-                       continue;
-               }
-
-               /* check whether this buffer is still in use */
-               if (r300->rmm->u_list[i].pending) {
-                       in_use++;
-               }
-       }
-       /* Cannot flush/lock if no context exists. */
-       if (in_use)
-               r300FlushCmdBuf(r300, __FUNCTION__);
-
-       done_age = radeonGetAge((radeonContextPtr) r300);
-
-       for (i = r300->rmm->u_last; i > 0; i--) {
-               if (r300->rmm->u_list[i].ptr == NULL) {
-                       continue;
-               }
-
-               /* check whether this buffer is still in use */
-               if (!r300->rmm->u_list[i].pending) {
-                       continue;
-               }
-
-               assert(r300->rmm->u_list[i].h_pending == 0);
-
-               tries = 0;
-               while (r300->rmm->u_list[i].age > done_age && tries++ < 1000) {
-                       usleep(10);
-                       done_age = radeonGetAge((radeonContextPtr) r300);
-               }
-               if (tries >= 1000) {
-                       WARN_ONCE("Failed to idle region!");
-               }
-
-               memfree.region_offset = (char *)r300->rmm->u_list[i].ptr -
-                   (char *)r300->radeon.radeonScreen->gartTextures.map;
-
-               ret = drmCommandWrite(r300->radeon.radeonScreen->driScreen->fd,
-                                     DRM_RADEON_FREE, &memfree,
-                                     sizeof(memfree));
-               if (ret) {
-                       fprintf(stderr, "Failed to free at %p\nret = %s\n",
-                               r300->rmm->u_list[i].ptr, strerror(-ret));
-               } else {
-                       if (i == r300->rmm->u_last)
-                               r300->rmm->u_last--;
-
-                       r300->rmm->u_list[i].pending = 0;
-                       r300->rmm->u_list[i].ptr = NULL;
-               }
-       }
-       r300->rmm->u_head = i;
-#endif                         /* USER_BUFFERS */
-}
-
 /* Destroy the device specific context.
  */
 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
@@ -485,24 +480,12 @@ void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
        assert(r300);           /* should never be null */
 
        if (r300) {
-               GLboolean release_texture_heaps;
-
-               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);
 
-               if (r300->dma.current.buf) {
-                       r300ReleaseDmaRegion(r300, &r300->dma.current,
-                                            __FUNCTION__);
-#ifndef USER_BUFFERS
-                       r300FlushCmdBuf(r300, __FUNCTION__);
-#endif
-               }
-               r300FreeGartAllocations(r300);
+               rcommonFlushCmdBuf(&r300->radeon, __FUNCTION__);
                r300DestroyCmdBuf(r300);
 
                if (radeon->state.scissor.pClipRects) {
@@ -510,31 +493,12 @@ void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
                        radeon->state.scissor.pClipRects = NULL;
                }
 
-               if (release_texture_heaps) {
-                       /* This share group is about to go away, free our private
-                        * texture object data.
-                        */
-                       int i;
-
-                       for (i = 0; i < r300->nr_heaps; i++) {
-                               driDestroyTextureHeap(r300->texture_heaps[i]);
-                               r300->texture_heaps[i] = NULL;
-                       }
-
-                       assert(is_empty_list(&r300->swapped));
-               }
-
                radeonCleanupContext(&r300->radeon);
 
-#ifdef USER_BUFFERS
                /* the memory manager might be accessed when Mesa frees the shared
                 * state, so don't destroy it earlier
                 */
-               r300_mem_destroy(r300);
-#endif
 
-               /* free the option cache */
-               driDestroyOptionCache(&r300->radeon.optionCache);
 
                FREE(r300);
        }