1848b8605Smrg/*
2848b8605Smrg * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
8848b8605Smrg * license, and/or sell copies of the Software, and to permit persons to whom
9848b8605Smrg * the Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18848b8605Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22848b8605Smrg
23848b8605Smrg#include "draw/draw_context.h"
24848b8605Smrg
25848b8605Smrg#include "util/u_memory.h"
26848b8605Smrg#include "util/u_sampler.h"
27b8e80941Smrg#include "util/simple_list.h"
28848b8605Smrg#include "util/u_upload_mgr.h"
29b8e80941Smrg#include "util/os_time.h"
30848b8605Smrg#include "vl/vl_decoder.h"
31848b8605Smrg#include "vl/vl_video_buffer.h"
32848b8605Smrg
33848b8605Smrg#include "r300_cb.h"
34848b8605Smrg#include "r300_context.h"
35848b8605Smrg#include "r300_emit.h"
36848b8605Smrg#include "r300_screen.h"
37848b8605Smrg#include "r300_screen_buffer.h"
38848b8605Smrg#include "compiler/radeon_regalloc.h"
39848b8605Smrg
40848b8605Smrg#include <inttypes.h>
41848b8605Smrg
42848b8605Smrgstatic void r300_release_referenced_objects(struct r300_context *r300)
43848b8605Smrg{
44848b8605Smrg    struct pipe_framebuffer_state *fb =
45848b8605Smrg            (struct pipe_framebuffer_state*)r300->fb_state.state;
46848b8605Smrg    struct r300_textures_state *textures =
47848b8605Smrg            (struct r300_textures_state*)r300->textures_state.state;
48848b8605Smrg    unsigned i;
49848b8605Smrg
50848b8605Smrg    /* Framebuffer state. */
51848b8605Smrg    util_unreference_framebuffer_state(fb);
52848b8605Smrg
53848b8605Smrg    /* Textures. */
54848b8605Smrg    for (i = 0; i < textures->sampler_view_count; i++)
55848b8605Smrg        pipe_sampler_view_reference(
56848b8605Smrg                (struct pipe_sampler_view**)&textures->sampler_views[i], NULL);
57848b8605Smrg
58848b8605Smrg    /* The special dummy texture for texkill. */
59848b8605Smrg    if (r300->texkill_sampler) {
60848b8605Smrg        pipe_sampler_view_reference(
61848b8605Smrg                (struct pipe_sampler_view**)&r300->texkill_sampler,
62848b8605Smrg                NULL);
63848b8605Smrg    }
64848b8605Smrg
65848b8605Smrg    /* Manually-created vertex buffers. */
66b8e80941Smrg    pipe_vertex_buffer_unreference(&r300->dummy_vb);
67848b8605Smrg    pb_reference(&r300->vbo, NULL);
68848b8605Smrg
69848b8605Smrg    r300->context.delete_depth_stencil_alpha_state(&r300->context,
70848b8605Smrg                                                   r300->dsa_decompress_zmask);
71848b8605Smrg}
72848b8605Smrg
73848b8605Smrgstatic void r300_destroy_context(struct pipe_context* context)
74848b8605Smrg{
75848b8605Smrg    struct r300_context* r300 = r300_context(context);
76848b8605Smrg
77848b8605Smrg    if (r300->cs && r300->hyperz_enabled) {
78848b8605Smrg        r300->rws->cs_request_feature(r300->cs, RADEON_FID_R300_HYPERZ_ACCESS, FALSE);
79848b8605Smrg    }
80848b8605Smrg    if (r300->cs && r300->cmask_access) {
81848b8605Smrg        r300->rws->cs_request_feature(r300->cs, RADEON_FID_R300_CMASK_ACCESS, FALSE);
82848b8605Smrg    }
83848b8605Smrg
84848b8605Smrg    if (r300->blitter)
85848b8605Smrg        util_blitter_destroy(r300->blitter);
86848b8605Smrg    if (r300->draw)
87848b8605Smrg        draw_destroy(r300->draw);
88848b8605Smrg
89848b8605Smrg    if (r300->uploader)
90848b8605Smrg        u_upload_destroy(r300->uploader);
91b8e80941Smrg    if (r300->context.stream_uploader)
92b8e80941Smrg        u_upload_destroy(r300->context.stream_uploader);
93848b8605Smrg
94848b8605Smrg    /* XXX: This function assumes r300->query_list was initialized */
95848b8605Smrg    r300_release_referenced_objects(r300);
96848b8605Smrg
97848b8605Smrg    if (r300->cs)
98848b8605Smrg        r300->rws->cs_destroy(r300->cs);
99b8e80941Smrg    if (r300->ctx)
100b8e80941Smrg        r300->rws->ctx_destroy(r300->ctx);
101848b8605Smrg
102848b8605Smrg    rc_destroy_regalloc_state(&r300->fs_regalloc_state);
103848b8605Smrg
104848b8605Smrg    /* XXX: No way to tell if this was initialized or not? */
105b8e80941Smrg    slab_destroy_child(&r300->pool_transfers);
106848b8605Smrg
107848b8605Smrg    /* Free the structs allocated in r300_setup_atoms() */
108848b8605Smrg    if (r300->aa_state.state) {
109848b8605Smrg        FREE(r300->aa_state.state);
110848b8605Smrg        FREE(r300->blend_color_state.state);
111848b8605Smrg        FREE(r300->clip_state.state);
112848b8605Smrg        FREE(r300->fb_state.state);
113848b8605Smrg        FREE(r300->gpu_flush.state);
114848b8605Smrg        FREE(r300->hyperz_state.state);
115848b8605Smrg        FREE(r300->invariant_state.state);
116848b8605Smrg        FREE(r300->rs_block_state.state);
117848b8605Smrg        FREE(r300->sample_mask.state);
118848b8605Smrg        FREE(r300->scissor_state.state);
119848b8605Smrg        FREE(r300->textures_state.state);
120848b8605Smrg        FREE(r300->vap_invariant_state.state);
121848b8605Smrg        FREE(r300->viewport_state.state);
122848b8605Smrg        FREE(r300->ztop_state.state);
123848b8605Smrg        FREE(r300->fs_constants.state);
124848b8605Smrg        FREE(r300->vs_constants.state);
125848b8605Smrg        if (!r300->screen->caps.has_tcl) {
126848b8605Smrg            FREE(r300->vertex_stream_state.state);
127848b8605Smrg        }
128848b8605Smrg    }
129848b8605Smrg    FREE(r300);
130848b8605Smrg}
131848b8605Smrg
132848b8605Smrgstatic void r300_flush_callback(void *data, unsigned flags,
133848b8605Smrg				struct pipe_fence_handle **fence)
134848b8605Smrg{
135848b8605Smrg    struct r300_context* const cs_context_copy = data;
136848b8605Smrg
137848b8605Smrg    r300_flush(&cs_context_copy->context, flags, fence);
138848b8605Smrg}
139848b8605Smrg
140848b8605Smrg#define R300_INIT_ATOM(atomname, atomsize) \
141848b8605Smrg do { \
142848b8605Smrg    r300->atomname.name = #atomname; \
143848b8605Smrg    r300->atomname.state = NULL; \
144848b8605Smrg    r300->atomname.size = atomsize; \
145848b8605Smrg    r300->atomname.emit = r300_emit_##atomname; \
146848b8605Smrg    r300->atomname.dirty = FALSE; \
147848b8605Smrg } while (0)
148848b8605Smrg
149848b8605Smrg#define R300_ALLOC_ATOM(atomname, statetype) \
150848b8605Smrgdo { \
151848b8605Smrg    r300->atomname.state = CALLOC_STRUCT(statetype); \
152848b8605Smrg    if (r300->atomname.state == NULL) \
153848b8605Smrg        return FALSE; \
154848b8605Smrg} while (0)
155848b8605Smrg
156848b8605Smrgstatic boolean r300_setup_atoms(struct r300_context* r300)
157848b8605Smrg{
158848b8605Smrg    boolean is_rv350 = r300->screen->caps.is_rv350;
159848b8605Smrg    boolean is_r500 = r300->screen->caps.is_r500;
160848b8605Smrg    boolean has_tcl = r300->screen->caps.has_tcl;
161848b8605Smrg
162848b8605Smrg    /* Create the actual atom list.
163848b8605Smrg     *
164848b8605Smrg     * Some atoms never change size, others change every emit - those have
165848b8605Smrg     * the size of 0 here.
166848b8605Smrg     *
167848b8605Smrg     * NOTE: The framebuffer state is split into these atoms:
168848b8605Smrg     * - gpu_flush          (unpipelined regs)
169848b8605Smrg     * - aa_state           (unpipelined regs)
170848b8605Smrg     * - fb_state           (unpipelined regs)
171848b8605Smrg     * - hyperz_state       (unpipelined regs followed by pipelined ones)
172848b8605Smrg     * - fb_state_pipelined (pipelined regs)
173848b8605Smrg     * The motivation behind this is to be able to emit a strict
174848b8605Smrg     * subset of the regs, and to have reasonable register ordering. */
175848b8605Smrg    /* SC, GB (unpipelined), RB3D (unpipelined), ZB (unpipelined). */
176848b8605Smrg    R300_INIT_ATOM(gpu_flush, 9);
177848b8605Smrg    R300_INIT_ATOM(aa_state, 4);
178848b8605Smrg    R300_INIT_ATOM(fb_state, 0);
179b8e80941Smrg    R300_INIT_ATOM(hyperz_state, is_r500 || is_rv350 ? 10 : 8);
180848b8605Smrg    /* ZB (unpipelined), SC. */
181848b8605Smrg    R300_INIT_ATOM(ztop_state, 2);
182848b8605Smrg    /* ZB, FG. */
183b8e80941Smrg    R300_INIT_ATOM(dsa_state, is_r500 ? 10 : 6);
184848b8605Smrg    /* RB3D. */
185848b8605Smrg    R300_INIT_ATOM(blend_state, 8);
186848b8605Smrg    R300_INIT_ATOM(blend_color_state, is_r500 ? 3 : 2);
187848b8605Smrg    /* SC. */
188848b8605Smrg    R300_INIT_ATOM(sample_mask, 2);
189848b8605Smrg    R300_INIT_ATOM(scissor_state, 3);
190848b8605Smrg    /* GB, FG, GA, SU, SC, RB3D. */
191848b8605Smrg    R300_INIT_ATOM(invariant_state, 14 + (is_rv350 ? 4 : 0) + (is_r500 ? 4 : 0));
192848b8605Smrg    /* VAP. */
193848b8605Smrg    R300_INIT_ATOM(viewport_state, 9);
194848b8605Smrg    R300_INIT_ATOM(pvs_flush, 2);
195b8e80941Smrg    R300_INIT_ATOM(vap_invariant_state, is_r500 || !has_tcl ? 11 : 9);
196848b8605Smrg    R300_INIT_ATOM(vertex_stream_state, 0);
197848b8605Smrg    R300_INIT_ATOM(vs_state, 0);
198848b8605Smrg    R300_INIT_ATOM(vs_constants, 0);
199848b8605Smrg    R300_INIT_ATOM(clip_state, has_tcl ? 3 + (6 * 4) : 0);
200848b8605Smrg    /* VAP, RS, GA, GB, SU, SC. */
201848b8605Smrg    R300_INIT_ATOM(rs_block_state, 0);
202848b8605Smrg    R300_INIT_ATOM(rs_state, 0);
203848b8605Smrg    /* SC, US. */
204848b8605Smrg    R300_INIT_ATOM(fb_state_pipelined, 8);
205848b8605Smrg    /* US. */
206848b8605Smrg    R300_INIT_ATOM(fs, 0);
207848b8605Smrg    R300_INIT_ATOM(fs_rc_constant_state, 0);
208848b8605Smrg    R300_INIT_ATOM(fs_constants, 0);
209848b8605Smrg    /* TX. */
210848b8605Smrg    R300_INIT_ATOM(texture_cache_inval, 2);
211848b8605Smrg    R300_INIT_ATOM(textures_state, 0);
212848b8605Smrg    /* Clear commands */
213848b8605Smrg    R300_INIT_ATOM(hiz_clear, r300->screen->caps.hiz_ram > 0 ? 4 : 0);
214848b8605Smrg    R300_INIT_ATOM(zmask_clear, r300->screen->caps.zmask_ram > 0 ? 4 : 0);
215848b8605Smrg    R300_INIT_ATOM(cmask_clear, 4);
216848b8605Smrg    /* ZB (unpipelined), SU. */
217848b8605Smrg    R300_INIT_ATOM(query_start, 4);
218848b8605Smrg
219848b8605Smrg    /* Replace emission functions for r500. */
220848b8605Smrg    if (is_r500) {
221848b8605Smrg        r300->fs.emit = r500_emit_fs;
222848b8605Smrg        r300->fs_rc_constant_state.emit = r500_emit_fs_rc_constant_state;
223848b8605Smrg        r300->fs_constants.emit = r500_emit_fs_constants;
224848b8605Smrg    }
225848b8605Smrg
226848b8605Smrg    /* Some non-CSO atoms need explicit space to store the state locally. */
227848b8605Smrg    R300_ALLOC_ATOM(aa_state, r300_aa_state);
228848b8605Smrg    R300_ALLOC_ATOM(blend_color_state, r300_blend_color_state);
229848b8605Smrg    R300_ALLOC_ATOM(clip_state, r300_clip_state);
230848b8605Smrg    R300_ALLOC_ATOM(hyperz_state, r300_hyperz_state);
231848b8605Smrg    R300_ALLOC_ATOM(invariant_state, r300_invariant_state);
232848b8605Smrg    R300_ALLOC_ATOM(textures_state, r300_textures_state);
233848b8605Smrg    R300_ALLOC_ATOM(vap_invariant_state, r300_vap_invariant_state);
234848b8605Smrg    R300_ALLOC_ATOM(viewport_state, r300_viewport_state);
235848b8605Smrg    R300_ALLOC_ATOM(ztop_state, r300_ztop_state);
236848b8605Smrg    R300_ALLOC_ATOM(fb_state, pipe_framebuffer_state);
237848b8605Smrg    R300_ALLOC_ATOM(gpu_flush, pipe_framebuffer_state);
238848b8605Smrg    r300->sample_mask.state = malloc(4);
239848b8605Smrg    R300_ALLOC_ATOM(scissor_state, pipe_scissor_state);
240848b8605Smrg    R300_ALLOC_ATOM(rs_block_state, r300_rs_block);
241848b8605Smrg    R300_ALLOC_ATOM(fs_constants, r300_constant_buffer);
242848b8605Smrg    R300_ALLOC_ATOM(vs_constants, r300_constant_buffer);
243848b8605Smrg    if (!r300->screen->caps.has_tcl) {
244848b8605Smrg        R300_ALLOC_ATOM(vertex_stream_state, r300_vertex_stream_state);
245848b8605Smrg    }
246848b8605Smrg
247848b8605Smrg    /* Some non-CSO atoms don't use the state pointer. */
248848b8605Smrg    r300->fb_state_pipelined.allow_null_state = TRUE;
249848b8605Smrg    r300->fs_rc_constant_state.allow_null_state = TRUE;
250848b8605Smrg    r300->pvs_flush.allow_null_state = TRUE;
251848b8605Smrg    r300->query_start.allow_null_state = TRUE;
252848b8605Smrg    r300->texture_cache_inval.allow_null_state = TRUE;
253848b8605Smrg
254848b8605Smrg    /* Some states must be marked as dirty here to properly set up
255848b8605Smrg     * hardware in the first command stream. */
256848b8605Smrg    r300_mark_atom_dirty(r300, &r300->invariant_state);
257848b8605Smrg    r300_mark_atom_dirty(r300, &r300->pvs_flush);
258848b8605Smrg    r300_mark_atom_dirty(r300, &r300->vap_invariant_state);
259848b8605Smrg    r300_mark_atom_dirty(r300, &r300->texture_cache_inval);
260848b8605Smrg    r300_mark_atom_dirty(r300, &r300->textures_state);
261848b8605Smrg
262848b8605Smrg    return TRUE;
263848b8605Smrg}
264848b8605Smrg
265848b8605Smrg/* Not every state tracker calls every driver function before the first draw
266848b8605Smrg * call and we must initialize the command buffers somehow. */
267848b8605Smrgstatic void r300_init_states(struct pipe_context *pipe)
268848b8605Smrg{
269848b8605Smrg    struct r300_context *r300 = r300_context(pipe);
270848b8605Smrg    struct pipe_blend_color bc = {{0}};
271848b8605Smrg    struct pipe_clip_state cs = {{{0}}};
272848b8605Smrg    struct pipe_scissor_state ss = {0};
273848b8605Smrg    struct r300_gpu_flush *gpuflush =
274848b8605Smrg            (struct r300_gpu_flush*)r300->gpu_flush.state;
275848b8605Smrg    struct r300_vap_invariant_state *vap_invariant =
276848b8605Smrg            (struct r300_vap_invariant_state*)r300->vap_invariant_state.state;
277848b8605Smrg    struct r300_invariant_state *invariant =
278848b8605Smrg            (struct r300_invariant_state*)r300->invariant_state.state;
279848b8605Smrg
280848b8605Smrg    CB_LOCALS;
281848b8605Smrg
282848b8605Smrg    pipe->set_blend_color(pipe, &bc);
283848b8605Smrg    pipe->set_clip_state(pipe, &cs);
284848b8605Smrg    pipe->set_scissor_states(pipe, 0, 1, &ss);
285848b8605Smrg    pipe->set_sample_mask(pipe, ~0);
286848b8605Smrg
287848b8605Smrg    /* Initialize the GPU flush. */
288848b8605Smrg    {
289848b8605Smrg        BEGIN_CB(gpuflush->cb_flush_clean, 6);
290848b8605Smrg
291848b8605Smrg        /* Flush and free renderbuffer caches. */
292848b8605Smrg        OUT_CB_REG(R300_RB3D_DSTCACHE_CTLSTAT,
293848b8605Smrg            R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS |
294848b8605Smrg            R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
295848b8605Smrg        OUT_CB_REG(R300_ZB_ZCACHE_CTLSTAT,
296848b8605Smrg            R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
297848b8605Smrg            R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
298848b8605Smrg
299848b8605Smrg        /* Wait until the GPU is idle.
300848b8605Smrg         * This fixes random pixels sometimes appearing probably caused
301848b8605Smrg         * by incomplete rendering. */
302848b8605Smrg        OUT_CB_REG(RADEON_WAIT_UNTIL, RADEON_WAIT_3D_IDLECLEAN);
303848b8605Smrg        END_CB;
304848b8605Smrg    }
305848b8605Smrg
306848b8605Smrg    /* Initialize the VAP invariant state. */
307848b8605Smrg    {
308848b8605Smrg        BEGIN_CB(vap_invariant->cb, r300->vap_invariant_state.size);
309848b8605Smrg        OUT_CB_REG(VAP_PVS_VTX_TIMEOUT_REG, 0xffff);
310848b8605Smrg        OUT_CB_REG_SEQ(R300_VAP_GB_VERT_CLIP_ADJ, 4);
311848b8605Smrg        OUT_CB_32F(1.0);
312848b8605Smrg        OUT_CB_32F(1.0);
313848b8605Smrg        OUT_CB_32F(1.0);
314848b8605Smrg        OUT_CB_32F(1.0);
315848b8605Smrg        OUT_CB_REG(R300_VAP_PSC_SGN_NORM_CNTL, R300_SGN_NORM_NO_ZERO);
316848b8605Smrg
317848b8605Smrg        if (r300->screen->caps.is_r500) {
318848b8605Smrg            OUT_CB_REG(R500_VAP_TEX_TO_COLOR_CNTL, 0);
319b8e80941Smrg        } else if (!r300->screen->caps.has_tcl) {
320b8e80941Smrg            /* RSxxx:
321b8e80941Smrg             * Static VAP setup since r300_emit_vs_state() is never called.
322b8e80941Smrg             */
323b8e80941Smrg            OUT_CB_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(10) |
324b8e80941Smrg                                      R300_PVS_NUM_CNTLRS(5) |
325b8e80941Smrg                                      R300_PVS_NUM_FPUS(2) |
326b8e80941Smrg                                      R300_PVS_VF_MAX_VTX_NUM(5));
327848b8605Smrg        }
328848b8605Smrg        END_CB;
329848b8605Smrg    }
330848b8605Smrg
331848b8605Smrg    /* Initialize the invariant state. */
332848b8605Smrg    {
333848b8605Smrg        BEGIN_CB(invariant->cb, r300->invariant_state.size);
334848b8605Smrg        OUT_CB_REG(R300_GB_SELECT, 0);
335848b8605Smrg        OUT_CB_REG(R300_FG_FOG_BLEND, 0);
336848b8605Smrg        OUT_CB_REG(R300_GA_OFFSET, 0);
337848b8605Smrg        OUT_CB_REG(R300_SU_TEX_WRAP, 0);
338848b8605Smrg        OUT_CB_REG(R300_SU_DEPTH_SCALE, 0x4B7FFFFF);
339848b8605Smrg        OUT_CB_REG(R300_SU_DEPTH_OFFSET, 0);
340848b8605Smrg        OUT_CB_REG(R300_SC_EDGERULE, 0x2DA49525);
341848b8605Smrg
342848b8605Smrg        if (r300->screen->caps.is_rv350) {
343848b8605Smrg            OUT_CB_REG(R500_RB3D_DISCARD_SRC_PIXEL_LTE_THRESHOLD, 0x01010101);
344848b8605Smrg            OUT_CB_REG(R500_RB3D_DISCARD_SRC_PIXEL_GTE_THRESHOLD, 0xFEFEFEFE);
345848b8605Smrg        }
346848b8605Smrg
347848b8605Smrg        if (r300->screen->caps.is_r500) {
348848b8605Smrg            OUT_CB_REG(R500_GA_COLOR_CONTROL_PS3, 0);
349848b8605Smrg            OUT_CB_REG(R500_SU_TEX_WRAP_PS3, 0);
350848b8605Smrg        }
351848b8605Smrg        END_CB;
352848b8605Smrg    }
353848b8605Smrg
354848b8605Smrg    /* Initialize the hyperz state. */
355848b8605Smrg    {
356848b8605Smrg        struct r300_hyperz_state *hyperz =
357848b8605Smrg            (struct r300_hyperz_state*)r300->hyperz_state.state;
358848b8605Smrg        BEGIN_CB(&hyperz->cb_flush_begin, r300->hyperz_state.size);
359848b8605Smrg        OUT_CB_REG(R300_ZB_ZCACHE_CTLSTAT,
360848b8605Smrg                   R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
361848b8605Smrg        OUT_CB_REG(R300_ZB_BW_CNTL, 0);
362848b8605Smrg        OUT_CB_REG(R300_ZB_DEPTHCLEARVALUE, 0);
363848b8605Smrg        OUT_CB_REG(R300_SC_HYPERZ, R300_SC_HYPERZ_ADJ_2);
364848b8605Smrg
365b8e80941Smrg        if (r300->screen->caps.is_r500 || r300->screen->caps.is_rv350) {
366848b8605Smrg            OUT_CB_REG(R300_GB_Z_PEQ_CONFIG, 0);
367848b8605Smrg        }
368848b8605Smrg        END_CB;
369848b8605Smrg    }
370848b8605Smrg}
371848b8605Smrg
372848b8605Smrgstruct pipe_context* r300_create_context(struct pipe_screen* screen,
373b8e80941Smrg                                         void *priv, unsigned flags)
374848b8605Smrg{
375848b8605Smrg    struct r300_context* r300 = CALLOC_STRUCT(r300_context);
376848b8605Smrg    struct r300_screen* r300screen = r300_screen(screen);
377848b8605Smrg    struct radeon_winsys *rws = r300screen->rws;
378848b8605Smrg
379848b8605Smrg    if (!r300)
380848b8605Smrg        return NULL;
381848b8605Smrg
382848b8605Smrg    r300->rws = rws;
383848b8605Smrg    r300->screen = r300screen;
384848b8605Smrg
385848b8605Smrg    r300->context.screen = screen;
386848b8605Smrg    r300->context.priv = priv;
387848b8605Smrg
388848b8605Smrg    r300->context.destroy = r300_destroy_context;
389848b8605Smrg
390b8e80941Smrg    slab_create_child(&r300->pool_transfers, &r300screen->pool_transfers);
391848b8605Smrg
392b8e80941Smrg    r300->ctx = rws->ctx_create(rws);
393b8e80941Smrg    if (!r300->ctx)
394b8e80941Smrg        goto fail;
395b8e80941Smrg
396b8e80941Smrg    r300->cs = rws->cs_create(r300->ctx, RING_GFX, r300_flush_callback, r300, false);
397848b8605Smrg    if (r300->cs == NULL)
398848b8605Smrg        goto fail;
399848b8605Smrg
400848b8605Smrg    if (!r300screen->caps.has_tcl) {
401848b8605Smrg        /* Create a Draw. This is used for SW TCL. */
402848b8605Smrg        r300->draw = draw_create(&r300->context);
403848b8605Smrg        if (r300->draw == NULL)
404848b8605Smrg            goto fail;
405848b8605Smrg        /* Enable our renderer. */
406848b8605Smrg        draw_set_rasterize_stage(r300->draw, r300_draw_stage(r300));
407848b8605Smrg        /* Disable converting points/lines to triangles. */
408848b8605Smrg        draw_wide_line_threshold(r300->draw, 10000000.f);
409848b8605Smrg        draw_wide_point_threshold(r300->draw, 10000000.f);
410848b8605Smrg        draw_wide_point_sprites(r300->draw, FALSE);
411848b8605Smrg        draw_enable_line_stipple(r300->draw, TRUE);
412848b8605Smrg        draw_enable_point_sprites(r300->draw, FALSE);
413848b8605Smrg    }
414848b8605Smrg
415848b8605Smrg    if (!r300_setup_atoms(r300))
416848b8605Smrg        goto fail;
417848b8605Smrg
418848b8605Smrg    r300_init_blit_functions(r300);
419848b8605Smrg    r300_init_flush_functions(r300);
420848b8605Smrg    r300_init_query_functions(r300);
421848b8605Smrg    r300_init_state_functions(r300);
422848b8605Smrg    r300_init_resource_functions(r300);
423848b8605Smrg    r300_init_render_functions(r300);
424848b8605Smrg    r300_init_states(&r300->context);
425848b8605Smrg
426848b8605Smrg    r300->context.create_video_codec = vl_create_decoder;
427848b8605Smrg    r300->context.create_video_buffer = vl_video_buffer_create;
428848b8605Smrg
429b8e80941Smrg    r300->uploader = u_upload_create(&r300->context, 128 * 1024,
430b8e80941Smrg                                     PIPE_BIND_CUSTOM, PIPE_USAGE_STREAM, 0);
431b8e80941Smrg    r300->context.stream_uploader = u_upload_create(&r300->context, 1024 * 1024,
432b8e80941Smrg                                                    0, PIPE_USAGE_STREAM, 0);
433b8e80941Smrg    r300->context.const_uploader = r300->context.stream_uploader;
434848b8605Smrg
435848b8605Smrg    r300->blitter = util_blitter_create(&r300->context);
436848b8605Smrg    if (r300->blitter == NULL)
437848b8605Smrg        goto fail;
438848b8605Smrg    r300->blitter->draw_rectangle = r300_blitter_draw_rectangle;
439848b8605Smrg
440848b8605Smrg    /* The KIL opcode needs the first texture unit to be enabled
441848b8605Smrg     * on r3xx-r4xx. In order to calm down the CS checker, we bind this
442848b8605Smrg     * dummy texture there. */
443848b8605Smrg    if (!r300->screen->caps.is_r500) {
444848b8605Smrg        struct pipe_resource *tex;
445848b8605Smrg        struct pipe_resource rtempl = {{0}};
446848b8605Smrg        struct pipe_sampler_view vtempl = {{0}};
447848b8605Smrg
448848b8605Smrg        rtempl.target = PIPE_TEXTURE_2D;
449848b8605Smrg        rtempl.format = PIPE_FORMAT_I8_UNORM;
450848b8605Smrg        rtempl.usage = PIPE_USAGE_IMMUTABLE;
451848b8605Smrg        rtempl.width0 = 1;
452848b8605Smrg        rtempl.height0 = 1;
453848b8605Smrg        rtempl.depth0 = 1;
454848b8605Smrg        tex = screen->resource_create(screen, &rtempl);
455848b8605Smrg
456848b8605Smrg        u_sampler_view_default_template(&vtempl, tex, tex->format);
457848b8605Smrg
458848b8605Smrg        r300->texkill_sampler = (struct r300_sampler_view*)
459848b8605Smrg            r300->context.create_sampler_view(&r300->context, tex, &vtempl);
460848b8605Smrg
461848b8605Smrg        pipe_resource_reference(&tex, NULL);
462848b8605Smrg    }
463848b8605Smrg
464848b8605Smrg    if (r300screen->caps.has_tcl) {
465848b8605Smrg        struct pipe_resource vb;
466848b8605Smrg        memset(&vb, 0, sizeof(vb));
467848b8605Smrg        vb.target = PIPE_BUFFER;
468848b8605Smrg        vb.format = PIPE_FORMAT_R8_UNORM;
469848b8605Smrg        vb.usage = PIPE_USAGE_DEFAULT;
470848b8605Smrg        vb.width0 = sizeof(float) * 16;
471848b8605Smrg        vb.height0 = 1;
472848b8605Smrg        vb.depth0 = 1;
473848b8605Smrg
474b8e80941Smrg        r300->dummy_vb.buffer.resource = screen->resource_create(screen, &vb);
475848b8605Smrg        r300->context.set_vertex_buffers(&r300->context, 0, 1, &r300->dummy_vb);
476848b8605Smrg    }
477848b8605Smrg
478848b8605Smrg    {
479848b8605Smrg        struct pipe_depth_stencil_alpha_state dsa;
480848b8605Smrg        memset(&dsa, 0, sizeof(dsa));
481848b8605Smrg        dsa.depth.writemask = 1;
482848b8605Smrg
483848b8605Smrg        r300->dsa_decompress_zmask =
484848b8605Smrg            r300->context.create_depth_stencil_alpha_state(&r300->context,
485848b8605Smrg                                                           &dsa);
486848b8605Smrg    }
487848b8605Smrg
488848b8605Smrg    r300->hyperz_time_of_last_flush = os_time_get();
489848b8605Smrg
490848b8605Smrg    /* Register allocator state */
491848b8605Smrg    rc_init_regalloc_state(&r300->fs_regalloc_state);
492848b8605Smrg
493848b8605Smrg    /* Print driver info. */
494848b8605Smrg#ifdef DEBUG
495848b8605Smrg    {
496848b8605Smrg#else
497848b8605Smrg    if (DBG_ON(r300, DBG_INFO)) {
498848b8605Smrg#endif
499848b8605Smrg        fprintf(stderr,
500848b8605Smrg                "r300: DRM version: %d.%d.%d, Name: %s, ID: 0x%04x, GB: %d, Z: %d\n"
501848b8605Smrg                "r300: GART size: %"PRIu64" MB, VRAM size: %"PRIu64" MB\n"
502848b8605Smrg                "r300: AA compression RAM: %s, Z compression RAM: %s, HiZ RAM: %s\n",
503848b8605Smrg                r300->screen->info.drm_major,
504848b8605Smrg                r300->screen->info.drm_minor,
505848b8605Smrg                r300->screen->info.drm_patchlevel,
506848b8605Smrg                screen->get_name(screen),
507848b8605Smrg                r300->screen->info.pci_id,
508848b8605Smrg                r300->screen->info.r300_num_gb_pipes,
509848b8605Smrg                r300->screen->info.r300_num_z_pipes,
510848b8605Smrg                r300->screen->info.gart_size >> 20,
511848b8605Smrg                r300->screen->info.vram_size >> 20,
512848b8605Smrg                "YES", /* XXX really? */
513848b8605Smrg                r300->screen->caps.zmask_ram ? "YES" : "NO",
514848b8605Smrg                r300->screen->caps.hiz_ram ? "YES" : "NO");
515848b8605Smrg    }
516848b8605Smrg
517848b8605Smrg    return &r300->context;
518848b8605Smrg
519848b8605Smrgfail:
520848b8605Smrg    r300_destroy_context(&r300->context);
521848b8605Smrg    return NULL;
522848b8605Smrg}
523