VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/include/cr_pack.h@ 78375

Last change on this file since 78375 was 78375, checked in by vboxsync, 6 years ago

Additions/common/crOpengl,GuestHost/OpenGL,HostServices/SharedOpenGL: Eliminate all global variables from the state tracker library (state_tracker) in preparation of the SPU DLL merging, bugref:9435

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 19.2 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved.
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#ifndef CR_PACK_H
8#define CR_PACK_H
9
10#include "cr_compiler.h"
11#include "cr_error.h"
12#include "cr_protocol.h"
13#include "cr_opcodes.h"
14#ifndef IN_RING0
15# include "cr_glstate.h"
16#endif
17#include "state/cr_statetypes.h"
18#include "state/cr_currentpointers.h"
19#include "state/cr_client.h"
20#ifndef IN_RING0
21#include "cr_threads.h"
22#endif
23
24#include <iprt/types.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30typedef struct CRPackContext_t CRPackContext;
31
32/**
33 * Packer buffer
34 */
35typedef struct
36{
37 void *pack; /**< the actual storage space/buffer */
38 unsigned int size; /**< size of pack[] buffer */
39 unsigned int mtu;
40 unsigned char *data_start, *data_current, *data_end;
41 unsigned char *opcode_start, *opcode_current, *opcode_end;
42 GLboolean geometry_only; /**< just used for debugging */
43 GLboolean holds_BeginEnd;
44 GLboolean in_BeginEnd;
45 GLboolean canBarf;
46 GLboolean holds_List;
47 GLboolean in_List;
48 CRPackContext *context;
49} CRPackBuffer;
50
51typedef void (*CRPackFlushFunc)(void *arg);
52typedef void (*CRPackSendHugeFunc)(CROpcode, void *);
53typedef void (*CRPackErrorHandlerFunc)(int line, const char *file, GLenum error, const char *info);
54
55#define CRPACKBLOCKSTATE_OP_BEGIN 0x01
56#define CRPACKBLOCKSTATE_OP_NEWLIST 0x02
57#define CRPACKBLOCKSTATE_OP_BEGINQUERY 0x04
58#define CRPACKBLOCKSTATE_OP_ALL 0x07
59
60#define CRPACKBLOCKSTATE_IS_OP_STARTED(_state, _op) (!!((_state) & (_op)))
61
62#define CRPACKBLOCKSTATE_IS_STARTED(_state) (!!(_state))
63
64#define CRPACKBLOCKSTATE_OP_START(_state, _op) do { \
65 Assert(!CRPACKBLOCKSTATE_IS_OP_STARTED(_state, _op)); \
66 (_state) |= (_op); \
67 } while (0)
68
69#define CRPACKBLOCKSTATE_OP_STOP(_state, _op) do { \
70 Assert(CRPACKBLOCKSTATE_IS_OP_STARTED(_state, _op)); \
71 (_state) &= ~(_op); \
72 } while (0)
73
74/**
75 * Packer context
76 */
77struct CRPackContext_t
78{
79 CRPackBuffer buffer; /**< not a pointer, see comments in pack_buffer.c */
80 CRPackFlushFunc Flush;
81 void *flush_arg;
82 CRPackSendHugeFunc SendHuge;
83 CRPackErrorHandlerFunc Error;
84 CRCurrentStatePointers current;
85 uint32_t u32CmdBlockState;
86 GLvectorf bounds_min, bounds_max;
87 int updateBBOX;
88 CRPackBuffer *currentBuffer;
89#ifndef IN_RING0
90 CRmutex mutex;
91#endif
92 char *file; /**< for debugging only */
93 int line; /**< for debugging only */
94};
95
96#if !defined(IN_RING0)
97# define CR_PACKER_CONTEXT_ARGSINGLEDECL void
98# define CR_PACKER_CONTEXT_ARGDECL
99# define CR_PACKER_CONTEXT_ARG
100# define CR_PACKER_CONTEXT_ARG_NOREF() do {} while (0)
101# define CR_PACKER_CONTEXT_ARGCTX(C)
102extern CRtsd _PackerTSD;
103#ifndef IN_RING0
104extern DECLHIDDEN(PCRStateTracker) g_pStateTracker; /** Hack to make the state tracker available to pack_client.c which uses crStateGetCurrent(). */
105#endif
106# define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = (CRPackContext *) crGetTSD(&_PackerTSD)
107# define CR_LOCK_PACKER_CONTEXT(PC) crLockMutex(&((PC)->mutex))
108# define CR_UNLOCK_PACKER_CONTEXT(PC) crUnlockMutex(&((PC)->mutex))
109extern uint32_t cr_packer_cmd_blocks_enabled;
110#else /* if defined IN_RING0 */
111# define CR_PACKER_CONTEXT_ARGSINGLEDECL CRPackContext *_pCtx
112# define CR_PACKER_CONTEXT_ARGDECL CR_PACKER_CONTEXT_ARGSINGLEDECL,
113# define CR_PACKER_CONTEXT_ARG _pCtx,
114# define CR_PACKER_CONTEXT_ARG_NOREF() RT_NOREF_PV(_pCtx)
115# define CR_PACKER_CONTEXT_ARGCTX(C) C,
116# define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = _pCtx
117# define CR_LOCK_PACKER_CONTEXT(PC)
118# define CR_UNLOCK_PACKER_CONTEXT(PC)
119#endif
120
121extern DECLEXPORT(CRPackContext *) crPackNewContext(void);
122extern DECLEXPORT(void) crPackDeleteContext(CRPackContext *pc);
123extern DECLEXPORT(void) crPackSetContext( CRPackContext *pc );
124extern DECLEXPORT(CRPackContext *) crPackGetContext( void );
125
126extern DECLEXPORT(void) crPackSetBuffer( CRPackContext *pc, CRPackBuffer *buffer );
127extern DECLEXPORT(void) crPackSetBufferDEBUG( const char *file, int line, CRPackContext *pc, CRPackBuffer *buffer );
128extern DECLEXPORT(void) crPackReleaseBuffer( CRPackContext *pc );
129extern DECLEXPORT(void) crPackResetPointers( CRPackContext *pc );
130
131extern DECLEXPORT(int) crPackMaxOpcodes( int buffer_size );
132extern DECLEXPORT(int) crPackMaxData( int buffer_size );
133extern DECLEXPORT(void) crPackInitBuffer( CRPackBuffer *buffer, void *buf, int size, int mtu
134#ifdef IN_RING0
135 , unsigned int num_opcodes
136#endif
137 );
138
139extern DECLEXPORT(void) crPackFlushFunc( CRPackContext *pc, CRPackFlushFunc ff );
140extern DECLEXPORT(void) crPackFlushArg( CRPackContext *pc, void *flush_arg );
141extern DECLEXPORT(void) crPackSendHugeFunc( CRPackContext *pc, CRPackSendHugeFunc shf );
142extern DECLEXPORT(void) crPackErrorFunction( CRPackContext *pc, CRPackErrorHandlerFunc errf );
143extern DECLEXPORT(void) crPackOffsetCurrentPointers( int offset );
144extern DECLEXPORT(void) crPackNullCurrentPointers( void );
145
146extern DECLEXPORT(void) crPackResetBoundingBox( CRPackContext *pc );
147extern DECLEXPORT(GLboolean) crPackGetBoundingBox( CRPackContext *pc,
148 GLfloat *xmin, GLfloat *ymin, GLfloat *zmin,
149 GLfloat *xmax, GLfloat *ymax, GLfloat *zmax);
150
151extern DECLEXPORT(void) crPackAppendBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
152extern DECLEXPORT(void) crPackAppendBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer, const CRrecti *bounds );
153extern DECLEXPORT(int) crPackCanHoldBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
154extern DECLEXPORT(int) crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
155
156#if defined(LINUX) || defined(WINDOWS)
157#define CR_UNALIGNED_ACCESS_OKAY
158#else
159#undef CR_UNALIGNED_ACCESS_OKAY
160#endif
161#ifndef IN_RING0
162extern DECLEXPORT(void) crWriteUnalignedDouble( void *buffer, double d );
163#endif
164
165extern DECLEXPORT(void) *crPackAlloc( CR_PACKER_CONTEXT_ARGDECL unsigned int len );
166extern DECLEXPORT(void) crHugePacket( CR_PACKER_CONTEXT_ARGDECL CROpcode op, void *ptr );
167extern DECLEXPORT(void) crPackFree( CR_PACKER_CONTEXT_ARGDECL void *ptr );
168extern DECLEXPORT(void) crNetworkPointerWrite( CRNetworkPointer *, void * );
169
170extern DECLEXPORT(void) crPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c, const GLfloat *pZva);
171extern DECLEXPORT(void) crPackExpandDrawArraysSWAP(GLenum mode, GLint first, GLsizei count, CRClientState *c, const GLfloat *pZva);
172
173extern DECLEXPORT(void) crPackUnrollDrawElements(GLsizei count, GLenum type, const GLvoid *indices);
174extern DECLEXPORT(void) crPackUnrollDrawElementsSWAP(GLsizei count, GLenum type, const GLvoid *indices);
175
176extern DECLEXPORT(void) crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
177extern DECLEXPORT(void) crPackExpandDrawElementsSWAP(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
178
179extern DECLEXPORT(void) crPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
180extern DECLEXPORT(void) crPackExpandDrawRangeElementsSWAP(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
181
182extern DECLEXPORT(void) crPackExpandArrayElement(GLint index, CRClientState *c, const GLfloat *pZva);
183extern DECLEXPORT(void) crPackExpandArrayElementSWAP(GLint index, CRClientState *c, const GLfloat *pZva);
184
185extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
186extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXTSWAP( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
187
188extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
189extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXTSWAP( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
190
191extern DECLEXPORT(void) crPackCapsSet(uint32_t u32Caps);
192
193/**
194 * Return number of opcodes in given buffer.
195 */
196static INLINE int
197crPackNumOpcodes(const CRPackBuffer *buffer)
198{
199 CRASSERT(buffer->opcode_start - buffer->opcode_current >= 0);
200 return buffer->opcode_start - buffer->opcode_current;
201}
202
203DECLINLINE(bool) crPackBufferIsEmpty(CRPackBuffer *buffer)
204{
205 return crPackNumOpcodes(buffer) == 0;
206}
207
208/**
209 * Return amount of data (in bytes) in buffer.
210 */
211static INLINE int
212crPackNumData(const CRPackBuffer *buffer)
213{
214 CRASSERT(buffer->data_current - buffer->data_start >= 0);
215 return buffer->data_current - buffer->data_start; /* in bytes */
216}
217
218
219static INLINE int
220crPackCanHoldOpcode(const CRPackContext *pc, int num_opcode, int num_data)
221{
222 int fitsInMTU, opcodesFit, dataFits;
223
224 CRASSERT(pc->currentBuffer);
225
226 fitsInMTU = (((pc->buffer.data_current - pc->buffer.opcode_current - 1
227 + num_opcode + num_data
228 + 0x3 ) & ~0x3) + sizeof(CRMessageOpcodes)
229 <= pc->buffer.mtu);
230 opcodesFit = (pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end);
231 dataFits = (pc->buffer.data_current + num_data <= pc->buffer.data_end);
232
233 return fitsInMTU && opcodesFit && dataFits;
234}
235
236
237#define CR_PACK_SPECIAL_OP( _pc, _op) \
238 do { \
239 data_ptr = pc->buffer.data_current; \
240 (_pc)->buffer.data_current += 4; \
241 WRITE_OPCODE( (_pc), (_op) ); \
242 WRITE_DATA( 0, GLuint, 0xdeadbeef ); \
243 data_ptr = NULL; /* <- sanity*/ \
244 } while (0)
245
246#define CR_CMDBLOCK_OP( _pc, _op) \
247 do { \
248 CR_PACK_SPECIAL_OP( _pc, _op); \
249 } while (0)
250
251#define CR_CMDBLOCK_IS_STARTED( pc, op ) CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)
252
253#define CR_CMDBLOCK_BEGIN( pc, op ) \
254 do { \
255 CR_LOCK_PACKER_CONTEXT(pc); \
256 if (!cr_packer_cmd_blocks_enabled) break; \
257 if (!CRPACKBLOCKSTATE_IS_STARTED((pc)->u32CmdBlockState)) { \
258 THREADASSERT( pc ); \
259 CRASSERT( (pc)->currentBuffer ); \
260 if (!crPackBufferIsEmpty(&(pc)->buffer)) { \
261 if ((*(pc)->buffer.opcode_start) != CR_NOP_OPCODE) { \
262 (pc)->Flush( (pc)->flush_arg ); \
263 Assert(crPackCanHoldOpcode( (pc), 1, 4 ) ); \
264 CR_CMDBLOCK_OP( (pc), CR_CMDBLOCKBEGIN_OPCODE ); \
265 } \
266 else { \
267 (*(pc)->buffer.opcode_start) = CR_CMDBLOCKBEGIN_OPCODE; \
268 } \
269 } \
270 else { \
271 Assert(crPackCanHoldOpcode( (pc), 1, 4 ) ); \
272 CR_CMDBLOCK_OP( (pc), CR_CMDBLOCKBEGIN_OPCODE ); \
273 } \
274 } \
275 Assert(!CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
276 CRPACKBLOCKSTATE_OP_START((pc)->u32CmdBlockState, op); \
277 Assert(CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
278 } while (0)
279
280#define CR_CMDBLOCK_END( pc, op ) \
281 do { \
282 if (!cr_packer_cmd_blocks_enabled) break; \
283 Assert(CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
284 CRPACKBLOCKSTATE_OP_STOP((pc)->u32CmdBlockState, op); \
285 Assert(!CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
286 if (!CRPACKBLOCKSTATE_IS_STARTED((pc)->u32CmdBlockState)) { \
287 THREADASSERT( pc ); \
288 CRASSERT( (pc)->currentBuffer ); \
289 if (!crPackBufferIsEmpty(&(pc)->buffer)) { \
290 if ((*(pc)->buffer.opcode_start) != CR_CMDBLOCKBEGIN_OPCODE) {\
291 if ( !crPackCanHoldOpcode( pc, 1, 4 ) ) { \
292 (pc)->Flush( (pc)->flush_arg ); \
293 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
294 } \
295 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKEND_OPCODE ); \
296 (pc)->Flush( (pc)->flush_arg ); \
297 } \
298 else { \
299 (*(pc)->buffer.opcode_start) = CR_NOP_OPCODE; \
300 } \
301 } \
302 else { \
303 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
304 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKEND_OPCODE ); \
305 (pc)->Flush( pc->flush_arg ); \
306 } \
307 } \
308 } while (0)
309
310#define CR_CMDBLOCK_CHECK_FLUSH( pc ) \
311 do { \
312 if (!(cr_packer_cmd_blocks_enabled & CR_VBOX_CAP_CMDBLOCKS_FLUSH)) break; \
313 if(!CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, CRPACKBLOCKSTATE_OP_NEWLIST)) break; \
314 THREADASSERT( pc ); \
315 CRASSERT( (pc)->currentBuffer ); \
316 if ( !crPackCanHoldOpcode( pc, 1, 4 ) ) { \
317 (pc)->Flush( (pc)->flush_arg ); \
318 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
319 } \
320 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKFLUSH_OPCODE ); \
321 (pc)->Flush( (pc)->flush_arg ); \
322 } while (0)
323
324/**
325 * Alloc space for a message of 'len' bytes (plus 1 opcode).
326 * Only flush if buffer is full.
327 */
328#define CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH(pc, len, lock) \
329 do { \
330 THREADASSERT( pc ); \
331 if (lock) CR_LOCK_PACKER_CONTEXT(pc); \
332 CRASSERT( pc->currentBuffer ); \
333 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
334 pc->Flush( pc->flush_arg ); \
335 CRASSERT(crPackCanHoldOpcode( pc, 1, (len) ) ); \
336 } \
337 data_ptr = pc->buffer.data_current; \
338 pc->buffer.data_current += (len); \
339 } while (0)
340
341/**
342 * As above, flush if the buffer contains vertex data and we're
343 * no longer inside glBegin/glEnd.
344 */
345#define CR_GET_BUFFERED_POINTER( pc, len ) \
346 do { \
347 CR_LOCK_PACKER_CONTEXT(pc); \
348 CRASSERT( pc->currentBuffer ); \
349 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
350 CRASSERT( 0 ); /* should never be here currently */ \
351 pc->Flush( pc->flush_arg ); \
352 pc->buffer.holds_BeginEnd = 0; \
353 } \
354 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
355 } while (0)
356
357/**
358 * As above, but without lock.
359 */
360#define CR_GET_BUFFERED_POINTER_NOLOCK( pc, len ) \
361 do { \
362 CRASSERT( pc->currentBuffer ); \
363 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
364 CRASSERT( 0 ); /* should never be here currently */ \
365 pc->Flush( pc->flush_arg ); \
366 pc->buffer.holds_BeginEnd = 0; \
367 } \
368 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
369 } while (0)
370
371
372/**
373 * As above, but for vertex data between glBegin/End (counts vertices).
374 */
375#define CR_GET_BUFFERED_COUNT_POINTER( pc, len ) \
376 do { \
377 CR_LOCK_PACKER_CONTEXT(pc); \
378 CRASSERT( pc->currentBuffer ); \
379 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
380 pc->Flush( pc->flush_arg ); \
381 CRASSERT( crPackCanHoldOpcode( pc, 1, (len) ) ); \
382 } \
383 data_ptr = pc->buffer.data_current; \
384 pc->current.vtx_count++; \
385 pc->buffer.data_current += (len); \
386 } while (0)
387
388
389/**
390 * Allocate space for a msg/command that has no arguments, such
391 * as glFinish().
392 */
393#define CR_GET_BUFFERED_POINTER_NO_ARGS( pc ) \
394 CR_GET_BUFFERED_POINTER( pc, 4 ); \
395 WRITE_DATA( 0, GLuint, 0xdeadbeef )
396
397#define WRITE_DATA( offset, type, data ) \
398 *( (type *) (data_ptr + (offset))) = (data)
399
400/* Write data to current location and auto increment */
401#define WRITE_DATA_AI(type, data) \
402 { \
403 *((type*) (data_ptr)) = (data); \
404 data_ptr += sizeof(type); \
405 }
406
407#ifdef CR_UNALIGNED_ACCESS_OKAY
408#define WRITE_DOUBLE( offset, data ) \
409 WRITE_DATA( offset, GLdouble, data )
410#else
411# ifndef IN_RING0
412# define WRITE_DOUBLE( offset, data ) \
413 crWriteUnalignedDouble( data_ptr + (offset), (data) )
414# else
415# define WRITE_DOUBLE( offset, data ) \
416 AssertReleaseFailed()
417# endif
418#endif
419
420#ifndef IN_RING0
421#define WRITE_SWAPPED_DOUBLE( offset, data ) \
422 crWriteSwappedDouble( data_ptr + (offset), (data) )
423#else
424#define WRITE_SWAPPED_DOUBLE( offset, data ) \
425 AssertReleaseFailed()
426#endif
427
428#define WRITE_OPCODE( pc, opcode ) \
429 *(pc->buffer.opcode_current--) = (unsigned char) opcode
430
431#define WRITE_NETWORK_POINTER( offset, data ) \
432 crNetworkPointerWrite( (CRNetworkPointer *) ( data_ptr + (offset) ), (data) )
433
434#ifdef __cplusplus
435}
436#endif
437
438#endif /* CR_PACK_H */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette