VirtualBox

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

Last change on this file since 52002 was 51315, checked in by vboxsync, 10 years ago

crOpenGL: missing file commit

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