<div dir="ltr"><div>Hi all,</div><div><br></div><div>The bug for this issue was created:</div><div><a href="https://bugs.freedesktop.org/show_bug.cgi?id=107626">https://bugs.freedesktop.org/show_bug.cgi?id=107626</a></div><div><br></div><div>Regards,</div><div>Andrii.</div><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 20, 2018 at 5:43 PM,  <span dir="ltr"><<a href="mailto:asimiklit.work@gmail.com" target="_blank">asimiklit.work@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Andrii Simiklit <<a href="mailto:andrii.simiklit@globallogic.com">andrii.simiklit@globallogic.<wbr>com</a>><br>
<br>
If we restore the 'new batch' using 'intel_batchbuffer_reset_to_<wbr>saved'<br>
function we must restore the default state of the batch using<br>
'brw_new_batch' function because the 'intel_batchbuffer_flush'<br>
function will not do it for the 'new batch' again.<br>
At least the following fields of the batch<br>
'state_base_address_emitted','<wbr>aperture_space', 'state_used'<br>
should be restored to default values to avoid:<br>
1. the aperture_space overflow<br>
2. the missed STATE_BASE_ADDRESS commad in the batch<br>
3. the memory overconsumption of the 'statebuffer'<br>
   due to uncleared 'state_used' field.<br>
etc.<br>
<br>
Signed-off-by: Andrii Simiklit <<a href="mailto:andrii.simiklit@globallogic.com">andrii.simiklit@globallogic.<wbr>com</a>><br>
---<br>
 src/mesa/drivers/dri/i965/<wbr>intel_batchbuffer.c | 104 +++++++++++++++-----------<br>
 1 file changed, 59 insertions(+), 45 deletions(-)<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/<wbr>intel_batchbuffer.c b/src/mesa/drivers/dri/i965/<wbr>intel_batchbuffer.c<br>
index 65d2c64..b8c5fb4 100644<br>
--- a/src/mesa/drivers/dri/i965/<wbr>intel_batchbuffer.c<br>
+++ b/src/mesa/drivers/dri/i965/<wbr>intel_batchbuffer.c<br>
@@ -219,7 +219,7 @@ add_exec_bo(struct intel_batchbuffer *batch, struct brw_bo *bo)<br>
    bo->index = batch->exec_count;<br>
    batch->exec_bos[batch->exec_<wbr>count] = bo;<br>
    batch->aperture_space += bo->size;<br>
-<br>
+   assert((batch->aperture_space >= 0) && "error 'batch->aperture_space' field is overflown!");<br>
    return batch->exec_count++;<br>
 }<br>
<br>
@@ -290,6 +290,51 @@ intel_batchbuffer_reset_and_<wbr>clear_render_cache(struct brw_context *brw)<br>
    brw_cache_sets_clear(brw);<br>
 }<br>
<br>
+/**<br>
+ * Called when starting a new batch buffer.<br>
+ */<br>
+static void<br>
+brw_new_batch(struct brw_context *brw)<br>
+{<br>
+   /* Unreference any BOs held by the previous batch, and reset counts. */<br>
+   for (int i = 0; i < brw->batch.exec_count; i++) {<br>
+      brw_bo_unreference(brw->batch.<wbr>exec_bos[i]);<br>
+      brw->batch.exec_bos[i] = NULL;<br>
+   }<br>
+   brw->batch.batch_relocs.reloc_<wbr>count = 0;<br>
+   brw->batch.state_relocs.reloc_<wbr>count = 0;<br>
+   brw->batch.exec_count = 0;<br>
+   brw->batch.aperture_space = 0;<br>
+<br>
+   brw_bo_unreference(brw-><a href="http://batch.state.bo" rel="noreferrer" target="_blank">batch.<wbr>state.bo</a>);<br>
+<br>
+   /* Create a new batchbuffer and reset the associated state: */<br>
+   intel_batchbuffer_reset_and_<wbr>clear_render_cache(brw);<br>
+<br>
+   /* If the kernel supports hardware contexts, then most hardware state is<br>
+    * preserved between batches; we only need to re-emit state that is required<br>
+    * to be in every batch.  Otherwise we need to re-emit all the state that<br>
+    * would otherwise be stored in the context (which for all intents and<br>
+    * purposes means everything).<br>
+    */<br>
+   if (brw->hw_ctx == 0) {<br>
+      brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;<br>
+      brw_upload_invariant_state(<wbr>brw);<br>
+   }<br>
+<br>
+   brw->ctx.NewDriverState |= BRW_NEW_BATCH;<br>
+<br>
+   brw->ib.index_size = -1;<br>
+<br>
+   /* We need to periodically reap the shader time results, because rollover<br>
+    * happens every few seconds.  We also want to see results every once in a<br>
+    * while, because many programs won't cleanly destroy our context, so the<br>
+    * end-of-run printout may not happen.<br>
+    */<br>
+   if (INTEL_DEBUG & DEBUG_SHADER_TIME)<br>
+      brw_collect_and_report_shader_<wbr>time(brw);<br>
+}<br>
+<br>
 void<br>
 intel_batchbuffer_save_state(<wbr>struct brw_context *brw)<br>
 {<br>
@@ -311,6 +356,19 @@ intel_batchbuffer_reset_to_<wbr>saved(struct brw_context *brw)<br>
    brw->batch.exec_count = brw->batch.saved.exec_count;<br>
<br>
    brw->batch.map_next = brw->batch.saved.map_next;<br>
+   if (USED_BATCH(brw->batch) == 0)<br>
+   {<br>
+      /**<br>
+       * The 'intel_batchbuffer_flush' function will not call<br>
+       * the 'brw_new_batch' function when the USED_BATCH returns 0.<br>
+       * It may leads to the few following issue:<br>
+       * The 'aperture_space' field can be overflown<br>
+       * The 'statebuffer' buffer contains the big unused space<br>
+       * The STATE_BASE_ADDRESS message is missed<br>
+       * etc<br>
+       **/<br>
+      brw_new_batch(brw);<br>
+   }<br>
 }<br>
<br>
 void<br>
@@ -529,50 +587,6 @@ intel_batchbuffer_require_<wbr>space(struct brw_context *brw, GLuint sz)<br>
    }<br>
 }<br>
<br>
-/**<br>
- * Called when starting a new batch buffer.<br>
- */<br>
-static void<br>
-brw_new_batch(struct brw_context *brw)<br>
-{<br>
-   /* Unreference any BOs held by the previous batch, and reset counts. */<br>
-   for (int i = 0; i < brw->batch.exec_count; i++) {<br>
-      brw_bo_unreference(brw->batch.<wbr>exec_bos[i]);<br>
-      brw->batch.exec_bos[i] = NULL;<br>
-   }<br>
-   brw->batch.batch_relocs.reloc_<wbr>count = 0;<br>
-   brw->batch.state_relocs.reloc_<wbr>count = 0;<br>
-   brw->batch.exec_count = 0;<br>
-   brw->batch.aperture_space = 0;<br>
-<br>
-   brw_bo_unreference(brw-><a href="http://batch.state.bo" rel="noreferrer" target="_blank">batch.<wbr>state.bo</a>);<br>
-<br>
-   /* Create a new batchbuffer and reset the associated state: */<br>
-   intel_batchbuffer_reset_and_<wbr>clear_render_cache(brw);<br>
-<br>
-   /* If the kernel supports hardware contexts, then most hardware state is<br>
-    * preserved between batches; we only need to re-emit state that is required<br>
-    * to be in every batch.  Otherwise we need to re-emit all the state that<br>
-    * would otherwise be stored in the context (which for all intents and<br>
-    * purposes means everything).<br>
-    */<br>
-   if (brw->hw_ctx == 0) {<br>
-      brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;<br>
-      brw_upload_invariant_state(<wbr>brw);<br>
-   }<br>
-<br>
-   brw->ctx.NewDriverState |= BRW_NEW_BATCH;<br>
-<br>
-   brw->ib.index_size = -1;<br>
-<br>
-   /* We need to periodically reap the shader time results, because rollover<br>
-    * happens every few seconds.  We also want to see results every once in a<br>
-    * while, because many programs won't cleanly destroy our context, so the<br>
-    * end-of-run printout may not happen.<br>
-    */<br>
-   if (INTEL_DEBUG & DEBUG_SHADER_TIME)<br>
-      brw_collect_and_report_shader_<wbr>time(brw);<br>
-}<br>
<br>
 /**<br>
  * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and<br>
<span class="HOEnZb"><font color="#888888">-- <br>
2.7.4<br>
<br>
</font></span></blockquote></div><br></div></div>