Merge remote branch 'origin/7.8'
[mesa.git] / src / gallium / drivers / cell / ppu / cell_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * Authors
30 * Brian Paul
31 */
32
33
34 #include <stdio.h>
35
36 #include "pipe/p_defines.h"
37 #include "pipe/p_format.h"
38 #include "util/u_memory.h"
39 #include "pipe/p_screen.h"
40
41 #include "draw/draw_context.h"
42 #include "draw/draw_private.h"
43
44 #include "cell/common.h"
45 #include "cell_batch.h"
46 #include "cell_clear.h"
47 #include "cell_context.h"
48 #include "cell_draw_arrays.h"
49 #include "cell_fence.h"
50 #include "cell_flush.h"
51 #include "cell_state.h"
52 #include "cell_surface.h"
53 #include "cell_spu.h"
54 #include "cell_pipe_state.h"
55 #include "cell_texture.h"
56 #include "cell_vbuf.h"
57
58
59
60 static void
61 cell_destroy_context( struct pipe_context *pipe )
62 {
63 struct cell_context *cell = cell_context(pipe);
64
65 util_delete_keymap(cell->fragment_ops_cache, NULL);
66
67 cell_spu_exit(cell);
68
69 align_free(cell);
70 }
71
72
73 static struct draw_context *
74 cell_draw_create(struct cell_context *cell)
75 {
76 struct draw_context *draw = draw_create();
77
78 #if 0 /* broken */
79 if (getenv("GALLIUM_CELL_VS")) {
80 /* plug in SPU-based vertex transformation code */
81 draw->shader_queue_flush = cell_vertex_shader_queue_flush;
82 draw->driver_private = cell;
83 }
84 #endif
85
86 return draw;
87 }
88
89
90 static const struct debug_named_value cell_debug_flags[] = {
91 {"checker", CELL_DEBUG_CHECKER},/**< modulate tile clear color by SPU ID */
92 {"asm", CELL_DEBUG_ASM}, /**< dump SPU asm code */
93 {"sync", CELL_DEBUG_SYNC}, /**< SPUs do synchronous DMA */
94 {"fragops", CELL_DEBUG_FRAGMENT_OPS}, /**< SPUs emit fragment ops debug messages*/
95 {"fragopfallback", CELL_DEBUG_FRAGMENT_OP_FALLBACK}, /**< SPUs use reference implementation for fragment ops*/
96 {"cmd", CELL_DEBUG_CMD}, /**< SPUs dump command buffer info */
97 {"cache", CELL_DEBUG_CACHE}, /**< report texture cache stats on exit */
98 {NULL, 0}
99 };
100
101 static unsigned int
102 cell_is_resource_referenced( struct pipe_context *pipe,
103 struct pipe_resource *texture,
104 unsigned face, unsigned level)
105 {
106 /**
107 * FIXME: Optimize.
108 */
109
110 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
111 }
112
113
114 struct pipe_context *
115 cell_create_context(struct pipe_screen *screen,
116 void *priv )
117 {
118 struct cell_context *cell;
119 uint i;
120
121 /* some fields need to be 16-byte aligned, so align the whole object */
122 cell = (struct cell_context*) align_malloc(sizeof(struct cell_context), 16);
123 if (!cell)
124 return NULL;
125
126 memset(cell, 0, sizeof(*cell));
127
128 cell->winsys = NULL; /* XXX: fixme - get this from screen? */
129 cell->pipe.winsys = NULL;
130 cell->pipe.screen = screen;
131 cell->pipe.priv = priv;
132 cell->pipe.destroy = cell_destroy_context;
133
134 cell->pipe.clear = cell_clear;
135 cell->pipe.flush = cell_flush;
136
137 cell->pipe.is_resource_referenced = cell_is_resource_referenced;
138
139 #if 0
140 cell->pipe.begin_query = cell_begin_query;
141 cell->pipe.end_query = cell_end_query;
142 cell->pipe.wait_query = cell_wait_query;
143 #endif
144
145 cell_init_draw_functions(cell);
146 cell_init_state_functions(cell);
147 cell_init_shader_functions(cell);
148 cell_init_surface_functions(cell);
149 cell_init_vertex_functions(cell);
150 cell_init_texture_transfer_funcs(cell);
151
152 cell->draw = cell_draw_create(cell);
153
154 /* Create cache of fragment ops generated code */
155 cell->fragment_ops_cache =
156 util_new_keymap(sizeof(struct cell_fragment_ops_key), ~0, NULL);
157
158 cell_init_vbuf(cell);
159
160 draw_set_rasterize_stage(cell->draw, cell->vbuf);
161
162 /* convert all points/lines to tris for the time being */
163 draw_wide_point_threshold(cell->draw, 0.0);
164 draw_wide_line_threshold(cell->draw, 0.0);
165
166 /* get env vars or read config file to get debug flags */
167 cell->debug_flags = debug_get_flags_option("CELL_DEBUG",
168 cell_debug_flags,
169 0 );
170
171 for (i = 0; i < CELL_NUM_BUFFERS; i++)
172 cell_fence_init(&cell->fenced_buffers[i].fence);
173
174
175 /*
176 * SPU stuff
177 */
178 /* This call only works with SDK 3.0. Anyone still using 2.1??? */
179 cell->num_cells = spe_cpu_info_get(SPE_COUNT_PHYSICAL_CPU_NODES, -1);
180 cell->num_spus = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);
181 if (cell->debug_flags) {
182 printf("Cell: found %d Cell(s) with %u SPUs\n",
183 cell->num_cells, cell->num_spus);
184 }
185 if (getenv("CELL_NUM_SPUS")) {
186 cell->num_spus = atoi(getenv("CELL_NUM_SPUS"));
187 assert(cell->num_spus > 0);
188 }
189
190 cell_start_spus(cell);
191
192 cell_init_batch_buffers(cell);
193
194 /* make sure SPU initializations are done before proceeding */
195 cell_flush_int(cell, CELL_FLUSH_WAIT);
196
197 return &cell->pipe;
198 }