Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / i965 / brw_vtbl.c
1 /*
2  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3  Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4  develop this 3D driver.
5  
6  Permission is hereby granted, free of charge, to any person obtaining
7  a copy of this software and associated documentation files (the
8  "Software"), to deal in the Software without restriction, including
9  without limitation the rights to use, copy, modify, merge, publish,
10  distribute, sublicense, and/or sell copies of the Software, and to
11  permit persons to whom the Software is furnished to do so, subject to
12  the following conditions:
13  
14  The above copyright notice and this permission notice (including the
15  next paragraph) shall be included in all copies or substantial
16  portions of the Software.
17  
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  
26 **********************************************************************/
27
28 /*
29  * Authors:
30  *   Keith Whitwell <keith@tungstengraphics.com>
31  */
32
33 #include "main/glheader.h"
34 #include "main/mtypes.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/colormac.h"
38 #include "main/renderbuffer.h"
39 #include "main/framebuffer.h"
40
41 #include "intel_batchbuffer.h" 
42 #include "intel_regions.h" 
43 #include "intel_fbo.h"
44
45 #include "brw_context.h"
46 #include "brw_defines.h"
47 #include "brw_state.h"
48 #include "brw_draw.h"
49 #include "brw_vs.h"
50 #include "brw_wm.h"
51
52 #include "../glsl/ralloc.h"
53
54 static void
55 dri_bo_release(drm_intel_bo **bo)
56 {
57    drm_intel_bo_unreference(*bo);
58    *bo = NULL;
59 }
60
61
62 /**
63  * called from intelDestroyContext()
64  */
65 static void brw_destroy_context( struct intel_context *intel )
66 {
67    struct brw_context *brw = brw_context(&intel->ctx);
68
69    brw_destroy_state(brw);
70    brw_draw_destroy( brw );
71    brw_clear_validated_bos(brw);
72    ralloc_free(brw->wm.compile_data);
73
74    dri_bo_release(&brw->curbe.curbe_bo);
75    dri_bo_release(&brw->vs.const_bo);
76    dri_bo_release(&brw->wm.const_bo);
77
78    free(brw->curbe.last_buf);
79    free(brw->curbe.next_buf);
80 }
81
82 /**
83  * Update the hardware state for drawing into a window or framebuffer object.
84  *
85  * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
86  * places within the driver.
87  *
88  * Basically, this needs to be called any time the current framebuffer
89  * changes, the renderbuffers change, or we need to draw into different
90  * color buffers.
91  */
92 static void
93 brw_update_draw_buffer(struct intel_context *intel)
94 {
95    struct gl_context *ctx = &intel->ctx;
96    struct gl_framebuffer *fb = ctx->DrawBuffer;
97    struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
98    bool fb_has_hiz = intel_framebuffer_has_hiz(fb);
99
100    if (!fb) {
101       /* this can happen during the initial context initialization */
102       return;
103    }
104
105    /*
106     * If intel_context is using separate stencil, but the depth attachment
107     * (gl_framebuffer.Attachment[BUFFER_DEPTH]) has a packed depth/stencil
108     * format, then we must install the real depth buffer at fb->_DepthBuffer
109     * and set fb->_DepthBuffer->Wrapped before calling _mesa_update_framebuffer.
110     * Otherwise, _mesa_update_framebuffer will create and install a swras
111     * depth wrapper instead.
112     *
113     * Ditto for stencil.
114     */
115    irbDepth = intel_get_renderbuffer(fb, BUFFER_DEPTH);
116    if (irbDepth && irbDepth->Base.Format == MESA_FORMAT_X8_Z24) {
117       _mesa_reference_renderbuffer(&fb->_DepthBuffer, &irbDepth->Base);
118       irbDepth->Base.Wrapped = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
119    }
120
121    irbStencil = intel_get_renderbuffer(fb, BUFFER_STENCIL);
122    if (irbStencil && irbStencil->Base.Format == MESA_FORMAT_S8) {
123       _mesa_reference_renderbuffer(&fb->_StencilBuffer, &irbStencil->Base);
124       irbStencil->Base.Wrapped = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
125    }
126
127    /* Do this here, not core Mesa, since this function is called from
128     * many places within the driver.
129     */
130    if (ctx->NewState & _NEW_BUFFERS) {
131       /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
132       _mesa_update_framebuffer(ctx);
133       /* this updates the DrawBuffer's Width/Height if it's a FBO */
134       _mesa_update_draw_buffer_bounds(ctx);
135    }
136
137    if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
138       /* this may occur when we're called by glBindFrameBuffer() during
139        * the process of someone setting up renderbuffers, etc.
140        */
141       /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
142       return;
143    }
144
145    /* Check some stencil invariants.  These should probably be in
146     * emit_depthbuffer().
147     */
148    if (irbStencil && irbStencil->region) {
149       if (!intel->has_separate_stencil)
150          assert(irbStencil->Base.Format == MESA_FORMAT_S8_Z24);
151       if (fb_has_hiz || intel->must_use_separate_stencil)
152          assert(irbStencil->Base.Format == MESA_FORMAT_S8);
153       if (irbStencil->Base.Format == MESA_FORMAT_S8)
154          assert(intel->has_separate_stencil);
155    }
156
157    /* Mesa's Stencil._Enabled field is updated when
158     * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
159     * only changes with _NEW_STENCIL (which seems sensible).  So flag it
160     * here since this is the _NEW_BUFFERS path.
161     */
162    intel->NewGLState |= (_NEW_DEPTH | _NEW_STENCIL);
163
164    /* The driver uses this in places that need to look up
165     * renderbuffers' buffer objects.
166     */
167    intel->NewGLState |= _NEW_BUFFERS;
168
169    /* update viewport/scissor since it depends on window size */
170    intel->NewGLState |= _NEW_VIEWPORT | _NEW_SCISSOR;
171
172    /* Update culling direction which changes depending on the
173     * orientation of the buffer:
174     */
175    intel->NewGLState |= _NEW_POLYGON;
176 }
177
178 /**
179  * called from intel_batchbuffer_flush and children before sending a
180  * batchbuffer off.
181  */
182 static void brw_finish_batch(struct intel_context *intel)
183 {
184    struct brw_context *brw = brw_context(&intel->ctx);
185    brw_emit_query_end(brw);
186
187    if (brw->curbe.curbe_bo) {
188       drm_intel_gem_bo_unmap_gtt(brw->curbe.curbe_bo);
189       drm_intel_bo_unreference(brw->curbe.curbe_bo);
190       brw->curbe.curbe_bo = NULL;
191    }
192 }
193
194
195 /**
196  * called from intelFlushBatchLocked
197  */
198 static void brw_new_batch( struct intel_context *intel )
199 {
200    struct brw_context *brw = brw_context(&intel->ctx);
201
202    /* Mark all context state as needing to be re-emitted.
203     * This is probably not as severe as on 915, since almost all of our state
204     * is just in referenced buffers.
205     */
206    brw->state.dirty.brw |= BRW_NEW_CONTEXT | BRW_NEW_BATCH;
207
208    /* Assume that the last command before the start of our batch was a
209     * primitive, for safety.
210     */
211    intel->batch.need_workaround_flush = true;
212
213    brw->vb.nr_current_buffers = 0;
214    brw->ib.type = -1;
215
216    /* Mark that the current program cache BO has been used by the GPU.
217     * It will be reallocated if we need to put new programs in for the
218     * next batch.
219     */
220    brw->cache.bo_used_by_gpu = true;
221 }
222
223 static void brw_invalidate_state( struct intel_context *intel, GLuint new_state )
224 {
225    /* nothing */
226 }
227
228 /**
229  * \see intel_context.vtbl.is_hiz_depth_format
230  */
231 static bool brw_is_hiz_depth_format(struct intel_context *intel,
232                                     gl_format format)
233 {
234    /* In the future, this will support Z_FLOAT32. */
235    return intel->has_hiz && (format == MESA_FORMAT_X8_Z24);
236 }
237
238
239 void brwInitVtbl( struct brw_context *brw )
240 {
241    brw->intel.vtbl.check_vertex_size = 0;
242    brw->intel.vtbl.emit_state = 0;
243    brw->intel.vtbl.reduced_primitive_state = 0;
244    brw->intel.vtbl.render_start = 0;
245    brw->intel.vtbl.update_texture_state = 0;
246
247    brw->intel.vtbl.invalidate_state = brw_invalidate_state;
248    brw->intel.vtbl.new_batch = brw_new_batch;
249    brw->intel.vtbl.finish_batch = brw_finish_batch;
250    brw->intel.vtbl.destroy = brw_destroy_context;
251    brw->intel.vtbl.update_draw_buffer = brw_update_draw_buffer;
252    brw->intel.vtbl.debug_batch = brw_debug_batch;
253    brw->intel.vtbl.render_target_supported = brw_render_target_supported;
254    brw->intel.vtbl.is_hiz_depth_format = brw_is_hiz_depth_format;
255 }