intel_batchbuffer.h revision bc3b8a39a7090d95942faf378e776e89c490e250
16b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell#ifndef INTEL_BATCHBUFFER_H
26b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell#define INTEL_BATCHBUFFER_H
36b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
4ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/mtypes.h"
54cd3ef58a989f61ff22669648e4117426c6e603cDave Airlie
635fd72756a05463568d94862f4fcd234903e1204Eric Anholt#include "intel_context.h"
73628185f566e178a12b493fb89abf52b4b281f99Eric Anholt#include "intel_bufmgr.h"
8f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie#include "intel_reg.h"
96b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
108e21bb516f87bfdde90d0f469ede4192435b9235Dave Airlie#define BATCH_SZ 16384
116b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell#define BATCH_RESERVED 16
126b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
13a04b632350e5d0e9994fc667afc59407a39da0baEric Anholtenum cliprect_mode {
14a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   /**
15a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * Batchbuffer contents may be looped over per cliprect, but do not
16a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * require it.
17a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    */
18a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   IGNORE_CLIPRECTS,
19a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   /**
20a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * Batchbuffer contents require looping over per cliprect at batch submit
21a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * time.
220cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt    *
230cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt    * This will be upgraded to NO_LOOP_CLIPRECTS when there's a single
240cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt    * constant cliprect, as in DRI2 or FBO rendering.
25a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    */
26a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   LOOP_CLIPRECTS,
27a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   /**
28a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * Batchbuffer contents contain drawing that should not be executed multiple
29a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * times.
30a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    */
31a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   NO_LOOP_CLIPRECTS,
32a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   /**
33a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * Batchbuffer contents contain drawing that already handles cliprects, such
34a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * as 2D drawing to front/back/depth that doesn't respect DRAWING_RECTANGLE.
350cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt    *
36a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    * Equivalent behavior to NO_LOOP_CLIPRECTS, but may not persist in batch
370cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt    * outside of LOCK/UNLOCK.  This is upgraded to just NO_LOOP_CLIPRECTS when
380cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt    * there's a constant cliprect, as in DRI2 or FBO rendering.
39a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt    */
40a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   REFERENCES_CLIPRECTS
41beddf653a914903156712aa472b5deaddb7bbaedEric Anholt};
426b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
436b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellstruct intel_batchbuffer
446b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell{
456b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   struct intel_context *intel;
466b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
47cfc21190af1038f204d38ab4764a9c731b0323a5Eric Anholt   dri_bo *buf;
48f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie
49f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie   GLubyte *buffer;
50145523ba3acb95a9ff390430a9e0a3fa958cae1bKeith Packard
516b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   GLubyte *map;
526b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   GLubyte *ptr;
536b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
54a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   enum cliprect_mode cliprect_mode;
55beddf653a914903156712aa472b5deaddb7bbaedEric Anholt
566b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   GLuint size;
57a8fee3a498c8c4966d57a5273408477f3aa3ce73Keith Whitwell
58bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   /** Tracking of BEGIN_BATCH()/OUT_BATCH()/ADVANCE_BATCH() debugging */
59bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   struct {
60bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt      GLuint total;
61bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt      GLubyte *start_ptr;
62bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   } emit;
63bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt
64a8fee3a498c8c4966d57a5273408477f3aa3ce73Keith Whitwell   GLuint dirty_state;
656b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell};
666b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
676b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellstruct intel_batchbuffer *intel_batchbuffer_alloc(struct intel_context
686b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                                                  *intel);
696b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
706b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellvoid intel_batchbuffer_free(struct intel_batchbuffer *batch);
716b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
726b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
73ab2933df65628d8f1f0a3f49129442bc8d2c3a2bEric Anholtvoid _intel_batchbuffer_flush(struct intel_batchbuffer *batch,
74ab2933df65628d8f1f0a3f49129442bc8d2c3a2bEric Anholt			      const char *file, int line);
75ab2933df65628d8f1f0a3f49129442bc8d2c3a2bEric Anholt
76ab2933df65628d8f1f0a3f49129442bc8d2c3a2bEric Anholt#define intel_batchbuffer_flush(batch) \
77ab2933df65628d8f1f0a3f49129442bc8d2c3a2bEric Anholt	_intel_batchbuffer_flush(batch, __FILE__, __LINE__)
786b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
796b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellvoid intel_batchbuffer_reset(struct intel_batchbuffer *batch);
806b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
816b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
826b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell/* Unlike bmBufferData, this currently requires the buffer be mapped.
836b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell * Consider it a convenience function wrapping multple
846b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell * intel_buffer_dword() calls.
856b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell */
866b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellvoid intel_batchbuffer_data(struct intel_batchbuffer *batch,
87beddf653a914903156712aa472b5deaddb7bbaedEric Anholt                            const void *data, GLuint bytes,
88a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt			    enum cliprect_mode cliprect_mode);
896b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
906b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellvoid intel_batchbuffer_release_space(struct intel_batchbuffer *batch,
916b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell                                     GLuint bytes);
926b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
936b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith WhitwellGLboolean intel_batchbuffer_emit_reloc(struct intel_batchbuffer *batch,
94cfc21190af1038f204d38ab4764a9c731b0323a5Eric Anholt                                       dri_bo *buffer,
95f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie				       uint32_t read_domains,
96f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie				       uint32_t write_domain,
97f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie				       uint32_t offset);
986b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
996b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell/* Inline functions - might actually be better off with these
1006b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell * non-inlined.  Certainly better off switching all command packets to
1016b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell * be passed as structs rather than dwords, but that's a little bit of
1026b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell * work...
1036b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell */
104f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic INLINE GLint
1056b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellintel_batchbuffer_space(struct intel_batchbuffer *batch)
1066b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell{
1076b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   return (batch->size - BATCH_RESERVED) - (batch->ptr - batch->map);
1086b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell}
1096b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
1106b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
1116b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellstatic INLINE void
1126b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellintel_batchbuffer_emit_dword(struct intel_batchbuffer *batch, GLuint dword)
1136b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell{
1146b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   assert(batch->map);
1156b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   assert(intel_batchbuffer_space(batch) >= 4);
1166b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   *(GLuint *) (batch->ptr) = dword;
1176b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   batch->ptr += 4;
1186b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell}
1196b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
1206b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellstatic INLINE void
1216b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwellintel_batchbuffer_require_space(struct intel_batchbuffer *batch,
122beddf653a914903156712aa472b5deaddb7bbaedEric Anholt                                GLuint sz,
123a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt				enum cliprect_mode cliprect_mode)
1246b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell{
1256b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell   assert(sz < batch->size - 8);
126beddf653a914903156712aa472b5deaddb7bbaedEric Anholt   if (intel_batchbuffer_space(batch) < sz)
1276b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell      intel_batchbuffer_flush(batch);
1286b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
1290cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt   if ((cliprect_mode == LOOP_CLIPRECTS ||
1300cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt	cliprect_mode == REFERENCES_CLIPRECTS) &&
1310cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt       batch->intel->constant_cliprect)
1320cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt      cliprect_mode = NO_LOOP_CLIPRECTS;
1330cade4de4f74f6b0e86fb6622e2fc370c73fd840Eric Anholt
134a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   if (cliprect_mode != IGNORE_CLIPRECTS) {
135a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt      if (batch->cliprect_mode == IGNORE_CLIPRECTS) {
136a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt	 batch->cliprect_mode = cliprect_mode;
137a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt      } else {
138b52398571b7f5c537c2f5e1c87f1b8bdf3d8ea9bDave Airlie	 if (batch->cliprect_mode != cliprect_mode) {
139a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt	    intel_batchbuffer_flush(batch);
140b52398571b7f5c537c2f5e1c87f1b8bdf3d8ea9bDave Airlie	    batch->cliprect_mode = cliprect_mode;
141b52398571b7f5c537c2f5e1c87f1b8bdf3d8ea9bDave Airlie	 }
142a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt      }
143a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   }
1446b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell}
1456b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
1466b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell/* Here are the crusty old macros, to be removed:
1476b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell */
1486b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell#define BATCH_LOCALS
1496b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
150a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt#define BEGIN_BATCH(n, cliprect_mode) do {				\
151a04b632350e5d0e9994fc667afc59407a39da0baEric Anholt   intel_batchbuffer_require_space(intel->batch, (n)*4, cliprect_mode); \
152bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   assert(intel->batch->emit.start_ptr == NULL);			\
153bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   intel->batch->emit.total = (n) * 4;					\
154bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   intel->batch->emit.start_ptr = intel->batch->ptr;			\
1556b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell} while (0)
1566b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
157bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt#define OUT_BATCH(d) intel_batchbuffer_emit_dword(intel->batch, d)
1586b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
159f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie#define OUT_RELOC(buf, read_domains, write_domain, delta) do {		\
160cfc21190af1038f204d38ab4764a9c731b0323a5Eric Anholt   assert((delta) >= 0);						\
161f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie   intel_batchbuffer_emit_reloc(intel->batch, buf,			\
162f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie				read_domains, write_domain, delta);	\
1636b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell} while (0)
1646b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
165bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt#define ADVANCE_BATCH() do {						\
166bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   unsigned int _n = intel->batch->ptr - intel->batch->emit.start_ptr;	\
167bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   assert(intel->batch->emit.start_ptr != NULL);			\
168bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   if (_n != intel->batch->emit.total) {				\
169bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt      fprintf(stderr, "ADVANCE_BATCH: %d of %d dwords emitted\n",	\
170bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt	      _n, intel->batch->emit.total);				\
171bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt      abort();								\
172bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   }									\
173bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt   intel->batch->emit.start_ptr = NULL;					\
174bc3b8a39a7090d95942faf378e776e89c490e250Eric Anholt} while(0)
1756b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
1766b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell
177f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic INLINE void
178f75843a517bd188639e6866db2a7b04de3524e16Dave Airlieintel_batchbuffer_emit_mi_flush(struct intel_batchbuffer *batch)
179f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie{
180f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie   intel_batchbuffer_require_space(batch, 4, IGNORE_CLIPRECTS);
181f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie   intel_batchbuffer_emit_dword(batch, MI_FLUSH);
182f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie}
183f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie
1846b9e31f3eb3dbe20cbc8493b963bbc6530e392c6Keith Whitwell#endif
185