VirtualBox

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

最後變更 在這個檔案從50973是 50973,由 vboxsync 提交於 11 年 前

crOpenGL: command blocks (disabled for now)

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 17.6 KB
 
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 );
139DECLINLINE(bool) crPackBufferIsEmpty(CRPackBuffer *buffer)
140{
141 return (buffer->opcode_current == buffer->data_current -1);
142}
143
144extern DECLEXPORT(void) crPackFlushFunc( CRPackContext *pc, CRPackFlushFunc ff );
145extern DECLEXPORT(void) crPackFlushArg( CRPackContext *pc, void *flush_arg );
146extern DECLEXPORT(void) crPackSendHugeFunc( CRPackContext *pc, CRPackSendHugeFunc shf );
147extern DECLEXPORT(void) crPackErrorFunction( CRPackContext *pc, CRPackErrorHandlerFunc errf );
148extern DECLEXPORT(void) crPackOffsetCurrentPointers( int offset );
149extern DECLEXPORT(void) crPackNullCurrentPointers( void );
150
151extern DECLEXPORT(void) crPackResetBoundingBox( CRPackContext *pc );
152extern DECLEXPORT(GLboolean) crPackGetBoundingBox( CRPackContext *pc,
153 GLfloat *xmin, GLfloat *ymin, GLfloat *zmin,
154 GLfloat *xmax, GLfloat *ymax, GLfloat *zmax);
155
156extern DECLEXPORT(void) crPackAppendBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
157extern DECLEXPORT(void) crPackAppendBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer, const CRrecti *bounds );
158extern DECLEXPORT(int) crPackCanHoldBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
159extern DECLEXPORT(int) crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
160
161#if defined(LINUX) || defined(WINDOWS)
162#define CR_UNALIGNED_ACCESS_OKAY
163#else
164#undef CR_UNALIGNED_ACCESS_OKAY
165#endif
166#ifndef IN_RING0
167extern DECLEXPORT(void) crWriteUnalignedDouble( void *buffer, double d );
168extern DECLEXPORT(void) crWriteSwappedDouble( void *buffer, double d );
169#endif
170
171extern DECLEXPORT(void) *crPackAlloc( CR_PACKER_CONTEXT_ARGDECL unsigned int len );
172extern DECLEXPORT(void) crHugePacket( CR_PACKER_CONTEXT_ARGDECL CROpcode op, void *ptr );
173extern DECLEXPORT(void) crPackFree( CR_PACKER_CONTEXT_ARGDECL void *ptr );
174extern DECLEXPORT(void) crNetworkPointerWrite( CRNetworkPointer *, void * );
175
176extern DECLEXPORT(void) crPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c);
177extern DECLEXPORT(void) crPackExpandDrawArraysSWAP(GLenum mode, GLint first, GLsizei count, CRClientState *c);
178
179extern DECLEXPORT(void) crPackUnrollDrawElements(GLsizei count, GLenum type, const GLvoid *indices);
180extern DECLEXPORT(void) crPackUnrollDrawElementsSWAP(GLsizei count, GLenum type, const GLvoid *indices);
181
182extern DECLEXPORT(void) crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
183extern DECLEXPORT(void) crPackExpandDrawElementsSWAP(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
184
185extern DECLEXPORT(void) crPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
186extern DECLEXPORT(void) crPackExpandDrawRangeElementsSWAP(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
187
188extern DECLEXPORT(void) crPackExpandArrayElement(GLint index, CRClientState *c);
189extern DECLEXPORT(void) crPackExpandArrayElementSWAP(GLint index, CRClientState *c);
190
191extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c );
192extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXTSWAP( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c );
193
194extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c );
195extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXTSWAP( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c );
196
197
198/**
199 * Return number of opcodes in given buffer.
200 */
201static INLINE int
202crPackNumOpcodes(const CRPackBuffer *buffer)
203{
204 CRASSERT(buffer->opcode_start - buffer->opcode_current >= 0);
205 return buffer->opcode_start - buffer->opcode_current;
206}
207
208
209/**
210 * Return amount of data (in bytes) in buffer.
211 */
212static INLINE int
213crPackNumData(const CRPackBuffer *buffer)
214{
215 CRASSERT(buffer->data_current - buffer->data_start >= 0);
216 return buffer->data_current - buffer->data_start; /* in bytes */
217}
218
219
220static INLINE int
221crPackCanHoldOpcode(const CRPackContext *pc, int num_opcode, int num_data)
222{
223 int fitsInMTU, opcodesFit, dataFits;
224
225 CRASSERT(pc->currentBuffer);
226
227 fitsInMTU = (((pc->buffer.data_current - pc->buffer.opcode_current - 1
228 + num_opcode + num_data
229 + 0x3 ) & ~0x3) + sizeof(CRMessageOpcodes)
230 <= pc->buffer.mtu);
231 opcodesFit = (pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end);
232 dataFits = (pc->buffer.data_current + num_data <= pc->buffer.data_end);
233
234 return fitsInMTU && opcodesFit && dataFits;
235}
236
237
238#define CR_PACK_SPECIAL_OP( _pc, _op) \
239 do { \
240 data_ptr = pc->buffer.data_current; \
241 (_pc)->buffer.data_current += 4; \
242 WRITE_OPCODE( (_pc), (_op) ); \
243 WRITE_DATA( 0, GLuint, 0xdeadbeef ); \
244 data_ptr = NULL; /* <- sanity*/ \
245 } while (0)
246
247#define CR_CMDBLOCK_OP( _pc, _op) \
248 do { \
249 CR_PACK_SPECIAL_OP( _pc, _op); \
250 } while (0)
251
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 CRPACKBLOCKSTATE_OP_START(pc->u32CmdBlockState, op); \
276 } while (0)
277
278#define CR_CMDBLOCK_END( pc, op ) \
279 do { \
280 if (!cr_packer_cmd_blocks_enabled) break; \
281 CRPACKBLOCKSTATE_OP_STOP(pc->u32CmdBlockState, op); \
282 if (!CRPACKBLOCKSTATE_IS_STARTED(pc->u32CmdBlockState)) { \
283 THREADASSERT( pc ); \
284 CRASSERT( pc->currentBuffer ); \
285 if (!crPackBufferIsEmpty(&pc->buffer)) { \
286 if ((*pc->buffer.opcode_start) != CR_CMDBLOCKBEGIN_OPCODE) {\
287 if ( !crPackCanHoldOpcode( pc, 1, 4 ) ) { \
288 pc->Flush( pc->flush_arg ); \
289 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
290 } \
291 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKEND_OPCODE ); \
292 pc->Flush( pc->flush_arg ); \
293 } \
294 else { \
295 (*pc->buffer.opcode_start) = CR_NOP_OPCODE; \
296 } \
297 } \
298 else { \
299 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
300 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKEND_OPCODE ); \
301 pc->Flush( pc->flush_arg ); \
302 } \
303 } \
304 } while (0)
305
306/**
307 * Alloc space for a message of 'len' bytes (plus 1 opcode).
308 * Only flush if buffer is full.
309 */
310#define CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH(pc, len, lock) \
311 do { \
312 THREADASSERT( pc ); \
313 if (lock) CR_LOCK_PACKER_CONTEXT(pc); \
314 CRASSERT( pc->currentBuffer ); \
315 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
316 pc->Flush( pc->flush_arg ); \
317 CRASSERT(crPackCanHoldOpcode( pc, 1, (len) ) ); \
318 } \
319 data_ptr = pc->buffer.data_current; \
320 pc->buffer.data_current += (len); \
321 } while (0)
322
323/**
324 * As above, flush if the buffer contains vertex data and we're
325 * no longer inside glBegin/glEnd.
326 */
327#define CR_GET_BUFFERED_POINTER( pc, len ) \
328 do { \
329 CR_LOCK_PACKER_CONTEXT(pc); \
330 CRASSERT( pc->currentBuffer ); \
331 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
332 CRASSERT( 0 ); /* should never be here currently */ \
333 pc->Flush( pc->flush_arg ); \
334 pc->buffer.holds_BeginEnd = 0; \
335 } \
336 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
337 } while (0)
338
339/**
340 * As above, but without lock.
341 */
342#define CR_GET_BUFFERED_POINTER_NOLOCK( pc, len ) \
343 do { \
344 CRASSERT( pc->currentBuffer ); \
345 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
346 CRASSERT( 0 ); /* should never be here currently */ \
347 pc->Flush( pc->flush_arg ); \
348 pc->buffer.holds_BeginEnd = 0; \
349 } \
350 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
351 } while (0)
352
353
354/**
355 * As above, but for vertex data between glBegin/End (counts vertices).
356 */
357#define CR_GET_BUFFERED_COUNT_POINTER( pc, len ) \
358 do { \
359 CR_LOCK_PACKER_CONTEXT(pc); \
360 CRASSERT( pc->currentBuffer ); \
361 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
362 pc->Flush( pc->flush_arg ); \
363 CRASSERT( crPackCanHoldOpcode( pc, 1, (len) ) ); \
364 } \
365 data_ptr = pc->buffer.data_current; \
366 pc->current.vtx_count++; \
367 pc->buffer.data_current += (len); \
368 } while (0)
369
370
371/**
372 * Allocate space for a msg/command that has no arguments, such
373 * as glFinish().
374 */
375#define CR_GET_BUFFERED_POINTER_NO_ARGS( pc ) \
376 CR_GET_BUFFERED_POINTER( pc, 4 ); \
377 WRITE_DATA( 0, GLuint, 0xdeadbeef )
378
379#define WRITE_DATA( offset, type, data ) \
380 *( (type *) (data_ptr + (offset))) = (data)
381
382/* Write data to current location and auto increment */
383#define WRITE_DATA_AI(type, data) \
384 { \
385 *((type*) (data_ptr)) = (data); \
386 data_ptr += sizeof(type); \
387 }
388
389#ifdef CR_UNALIGNED_ACCESS_OKAY
390#define WRITE_DOUBLE( offset, data ) \
391 WRITE_DATA( offset, GLdouble, data )
392#else
393# ifndef IN_RING0
394# define WRITE_DOUBLE( offset, data ) \
395 crWriteUnalignedDouble( data_ptr + (offset), (data) )
396# else
397# define WRITE_DOUBLE( offset, data ) \
398 AssertReleaseFailed()
399# endif
400#endif
401
402#ifndef IN_RING0
403#define WRITE_SWAPPED_DOUBLE( offset, data ) \
404 crWriteSwappedDouble( data_ptr + (offset), (data) )
405#else
406#define WRITE_SWAPPED_DOUBLE( offset, data ) \
407 AssertReleaseFailed()
408#endif
409
410#define WRITE_OPCODE( pc, opcode ) \
411 *(pc->buffer.opcode_current--) = (unsigned char) opcode
412
413#define WRITE_NETWORK_POINTER( offset, data ) \
414 crNetworkPointerWrite( (CRNetworkPointer *) ( data_ptr + (offset) ), (data) )
415
416#ifdef __cplusplus
417}
418#endif
419
420#endif /* CR_PACK_H */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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