Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / drivers / i965simple / brw_state_pool.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 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 /** @file brw_state_pool.c
33 * Implements the state pool allocator.
34 *
35 * For the 965, we create two state pools for state cache entries. Objects
36 * will be allocated into the pools depending on which state base address
37 * their pointer is relative to in other 965 state.
38 *
39 * The state pools are relatively simple: As objects are allocated, increment
40 * the offset to allocate space. When the pool is "full" (rather, close to
41 * full), we reset the pool and reset the state cache entries that point into
42 * the pool.
43 */
44
45 #include "pipe/internal/p_winsys_screen.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "pipe/p_inlines.h"
49 #include "brw_context.h"
50 #include "brw_state.h"
51
52 boolean brw_pool_alloc( struct brw_mem_pool *pool,
53 unsigned size,
54 unsigned alignment,
55 unsigned *offset_return)
56 {
57 unsigned fixup = align(pool->offset, alignment) - pool->offset;
58
59 size = align(size, 4);
60
61 if (pool->offset + fixup + size >= pool->size) {
62 debug_printf("%s failed\n", __FUNCTION__);
63 assert(0);
64 exit(0);
65 }
66
67 pool->offset += fixup;
68 *offset_return = pool->offset;
69 pool->offset += size;
70
71 return TRUE;
72 }
73
74 static
75 void brw_invalidate_pool( struct brw_mem_pool *pool )
76 {
77 if (BRW_DEBUG & DEBUG_STATE)
78 debug_printf("\n\n\n %s \n\n\n", __FUNCTION__);
79
80 pool->offset = 0;
81
82 brw_clear_all_caches(pool->brw);
83 }
84
85
86 static void brw_init_pool( struct brw_context *brw,
87 unsigned pool_id,
88 unsigned size )
89 {
90 struct brw_mem_pool *pool = &brw->pool[pool_id];
91
92 pool->size = size;
93 pool->brw = brw;
94
95 pool->buffer = pipe_buffer_create(brw->pipe.screen,
96 4096,
97 0 /* DRM_BO_FLAG_MEM_TT */,
98 size);
99 }
100
101 static void brw_destroy_pool( struct brw_context *brw,
102 unsigned pool_id )
103 {
104 struct brw_mem_pool *pool = &brw->pool[pool_id];
105
106 pipe_buffer_reference( pool->brw->pipe.screen,
107 &pool->buffer,
108 NULL );
109 }
110
111
112 void brw_pool_check_wrap( struct brw_context *brw,
113 struct brw_mem_pool *pool )
114 {
115 if (pool->offset > (pool->size * 3) / 4) {
116 brw->state.dirty.brw |= BRW_NEW_SCENE;
117 }
118
119 }
120
121 void brw_init_pools( struct brw_context *brw )
122 {
123 brw_init_pool(brw, BRW_GS_POOL, 0x80000);
124 brw_init_pool(brw, BRW_SS_POOL, 0x80000);
125 }
126
127 void brw_destroy_pools( struct brw_context *brw )
128 {
129 brw_destroy_pool(brw, BRW_GS_POOL);
130 brw_destroy_pool(brw, BRW_SS_POOL);
131 }
132
133
134 void brw_invalidate_pools( struct brw_context *brw )
135 {
136 brw_invalidate_pool(&brw->pool[BRW_GS_POOL]);
137 brw_invalidate_pool(&brw->pool[BRW_SS_POOL]);
138 }