1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
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:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 **************************************************************************/
36 //#include "state_trackers/xlib/glxheader.h"
37 //#include "state_trackers/xlib/xmesaP.h"
39 #include "pipe/p_winsys.h"
40 #include "pipe/p_inlines.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "i965simple/brw_winsys.h"
44 #include "i965simple/brw_screen.h"
46 #include "xlib_brw_aub.h"
57 boolean dump_on_unmap
;
62 struct aub_pipe_winsys
{
63 struct pipe_winsys winsys
;
65 struct brw_aubfile
*aubfile
;
67 /* This is simple, isn't it:
75 /* Turn a pipe winsys into an aub/pipe winsys:
77 static inline struct aub_pipe_winsys
*
78 aub_pipe_winsys( struct pipe_winsys
*winsys
)
80 return (struct aub_pipe_winsys
*)winsys
;
85 static INLINE
struct aub_buffer
*
86 aub_bo( struct pipe_buffer
*bo
)
88 return (struct aub_buffer
*)bo
;
91 static INLINE
struct pipe_buffer
*
92 pipe_bo( struct aub_buffer
*bo
)
94 return (struct pipe_buffer
*)bo
;
100 static void *aub_buffer_map(struct pipe_winsys
*winsys
,
101 struct pipe_buffer
*buf
,
104 struct aub_buffer
*sbo
= aub_bo(buf
);
108 if (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
)
109 sbo
->dump_on_unmap
= 1;
115 static void aub_buffer_unmap(struct pipe_winsys
*winsys
,
116 struct pipe_buffer
*buf
)
118 struct aub_pipe_winsys
*iws
= aub_pipe_winsys(winsys
);
119 struct aub_buffer
*sbo
= aub_bo(buf
);
123 if (sbo
->map_count
== 0 &&
124 sbo
->dump_on_unmap
) {
126 sbo
->dump_on_unmap
= 0;
128 brw_aub_gtt_data( iws
->aubfile
,
139 aub_buffer_destroy(struct pipe_winsys
*winsys
,
140 struct pipe_buffer
*buf
)
147 void xlib_brw_commands_aub(struct pipe_winsys
*winsys
,
151 struct aub_pipe_winsys
*iws
= aub_pipe_winsys(winsys
);
152 unsigned size
= nr_dwords
* 4;
154 assert(iws
->used
+ size
< iws
->size
);
156 brw_aub_gtt_cmds( iws
->aubfile
,
157 AUB_BUF_START
+ iws
->used
,
159 nr_dwords
* sizeof(int) );
161 iws
->used
+= align(size
, 4096);
167 static struct aub_pipe_winsys
*global_winsys
= NULL
;
172 /* Pipe has no concept of pools. We choose the tex/region pool
175 static struct pipe_buffer
*
176 aub_buffer_create(struct pipe_winsys
*winsys
,
181 struct aub_pipe_winsys
*iws
= aub_pipe_winsys(winsys
);
182 struct aub_buffer
*sbo
= CALLOC_STRUCT(aub_buffer
);
186 /* Could reuse buffers that are not referenced in current
187 * batchbuffer. Can't do that atm, so always reallocate:
189 assert(iws
->used
+ size
< iws
->size
);
190 sbo
->data
= iws
->pool
+ iws
->used
;
191 sbo
->offset
= AUB_BUF_START
+ iws
->used
;
192 iws
->used
+= align(size
, 4096);
200 static struct pipe_buffer
*
201 aub_user_buffer_create(struct pipe_winsys
*winsys
, void *ptr
, unsigned bytes
)
203 struct aub_buffer
*sbo
;
205 /* Lets hope this is meant for upload, not as a result!
207 sbo
= aub_bo(aub_buffer_create( winsys
, 0, 0, 0 ));
216 /* The state tracker (should!) keep track of whether the fake
217 * frontbuffer has been touched by any rendering since the last time
218 * we copied its contents to the real frontbuffer. Our task is easy:
221 aub_flush_frontbuffer( struct pipe_winsys
*winsys
,
222 struct pipe_surface
*surface
,
223 void *context_private
)
225 // struct aub_pipe_winsys *iws = aub_pipe_winsys(winsys);
226 brw_aub_dump_bmp( global_winsys
->aubfile
,
228 aub_bo(surface
->buffer
)->offset
);
231 static struct pipe_surface
*
232 aub_i915_surface_alloc(struct pipe_winsys
*winsys
)
234 struct pipe_surface
*surf
= CALLOC_STRUCT(pipe_surface
);
237 surf
->winsys
= winsys
;
244 * Round n up to next multiple.
246 static INLINE
unsigned
247 round_up(unsigned n
, unsigned multiple
)
249 return (n
+ multiple
- 1) & ~(multiple
- 1);
253 aub_i915_surface_alloc_storage(struct pipe_winsys
*winsys
,
254 struct pipe_surface
*surf
,
255 unsigned width
, unsigned height
,
256 enum pipe_format format
,
260 const unsigned alignment
= 64;
263 surf
->height
= height
;
264 surf
->format
= format
;
265 pf_get_block(format
, &surf
->block
);
266 surf
->nblocksx
= pf_get_nblocksx(&surf
->block
, width
);
267 surf
->nblocksy
= pf_get_nblocksy(&surf
->block
, height
);
268 surf
->stride
= round_up(surf
->nblocksx
* surf
->block
.size
, alignment
);
271 assert(!surf
->buffer
);
272 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
273 PIPE_BUFFER_USAGE_PIXEL
,
274 surf
->stride
* surf
->nblocksy
);
282 aub_i915_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
284 struct pipe_surface
*surf
= *s
;
286 if (surf
->refcount
== 0) {
288 winsys_buffer_reference(winsys
, &surf
->buffer
, NULL
);
297 aub_get_name( struct pipe_winsys
*winsys
)
303 xlib_brw_destroy_pipe_winsys_aub( struct pipe_winsys
*winsys
)
306 struct aub_pipe_winsys
*iws
= aub_pipe_winsys(winsys
);
307 brw_aub_destroy(iws
->aubfile
);
315 xlib_create_brw_winsys( void )
317 struct aub_pipe_winsys
*iws
= CALLOC_STRUCT( aub_pipe_winsys
);
319 /* Fill in this struct with callbacks that pipe will need to
320 * communicate with the window system, buffer manager, etc.
322 * Pipe would be happy with a malloc based memory manager, but
323 * the SwapBuffers implementation in this winsys driver requires
324 * that rendering be done to an appropriate _DriBufferObject.
326 iws
->winsys
.buffer_create
= aub_buffer_create
;
327 iws
->winsys
.user_buffer_create
= aub_user_buffer_create
;
328 iws
->winsys
.buffer_map
= aub_buffer_map
;
329 iws
->winsys
.buffer_unmap
= aub_buffer_unmap
;
330 iws
->winsys
.buffer_destroy
= aub_buffer_destroy
;
331 iws
->winsys
.flush_frontbuffer
= aub_flush_frontbuffer
;
332 iws
->winsys
.get_name
= aub_get_name
;
333 iws
->winsys
.destroy
= xlib_brw_destroy_pipe_winsys_aub
;
335 iws
->winsys
.surface_alloc
= aub_i915_surface_alloc
;
336 iws
->winsys
.surface_alloc_storage
= aub_i915_surface_alloc_storage
;
337 iws
->winsys
.surface_release
= aub_i915_surface_release
;
339 iws
->aubfile
= brw_aubfile_create();
340 iws
->size
= AUB_BUF_SIZE
;
341 iws
->pool
= malloc(AUB_BUF_SIZE
);
343 /* HACK: static copy of this pointer:
345 assert(global_winsys
== NULL
);
353 xlib_create_brw_screen( struct pipe_winsys
*winsys
)
355 return brw_create_screen(winsys
, 0/* XXX pci_id */);
359 /* These per-screen functions are acually made available to the driver
360 * through the brw_winsys (per-context) entity.
362 unsigned xlib_brw_get_buffer_offset( struct pipe_winsys
*pws
,
363 struct pipe_buffer
*buf
,
364 unsigned access_flags
)
366 return aub_bo(buf
)->offset
;
369 void xlib_brw_buffer_subdata_typed( struct pipe_winsys
*pws
,
370 struct pipe_buffer
*buf
,
371 unsigned long offset
,
376 unsigned aub_type
= DW_GENERAL_STATE
;
377 unsigned aub_sub_type
;
381 aub_sub_type
= DWGS_COLOR_CALC_VIEWPORT_STATE
;
384 aub_sub_type
= DWGS_COLOR_CALC_STATE
;
387 aub_sub_type
= DWGS_KERNEL_INSTRUCTIONS
;
389 case BRW_SAMPLER_DEFAULT_COLOR
:
390 aub_sub_type
= DWGS_SAMPLER_DEFAULT_COLOR
;
393 aub_sub_type
= DWGS_SAMPLER_STATE
;
396 aub_sub_type
= DWGS_WINDOWER_IZ_STATE
;
399 aub_sub_type
= DWGS_KERNEL_INSTRUCTIONS
;
402 aub_sub_type
= DWGS_STRIPS_FANS_VIEWPORT_STATE
;
405 aub_sub_type
= DWGS_STRIPS_FANS_STATE
;
408 aub_sub_type
= DWGS_VERTEX_SHADER_STATE
;
411 aub_sub_type
= DWGS_KERNEL_INSTRUCTIONS
;
414 aub_sub_type
= DWGS_GEOMETRY_SHADER_STATE
;
417 aub_sub_type
= DWGS_KERNEL_INSTRUCTIONS
;
420 aub_sub_type
= DWGS_CLIPPER_VIEWPORT_STATE
;
423 aub_sub_type
= DWGS_CLIPPER_STATE
;
426 aub_sub_type
= DWGS_KERNEL_INSTRUCTIONS
;
429 aub_type
= DW_SURFACE_STATE
;
430 aub_sub_type
= DWSS_SURFACE_STATE
;
432 case BRW_SS_SURF_BIND
:
433 aub_type
= DW_SURFACE_STATE
;
434 aub_sub_type
= DWSS_BINDING_TABLE_STATE
;
436 case BRW_CONSTANT_BUFFER
:
437 aub_type
= DW_CONSTANT_URB_ENTRY
;
447 struct aub_pipe_winsys
*iws
= aub_pipe_winsys(pws
);
448 struct aub_buffer
*sbo
= aub_bo(buf
);
450 assert(sbo
->size
> offset
+ size
);
451 memcpy(sbo
->data
+ offset
, data
, size
);
453 brw_aub_gtt_data( iws
->aubfile
,
454 sbo
->offset
+ offset
,
464 xlib_brw_display_surface(struct xmesa_buffer
*b
,
465 struct pipe_surface
*surf
)
467 brw_aub_dump_bmp( global_winsys
->aubfile
,
469 aub_bo(surf
->buffer
)->offset
);