void draw_batch_flush(int kind) {
  static bool flushing = false;

  // return if the kind of flush being requested
  // is not the mode of flushing we have enabled
  if (draw_batch_mode != kind) return;

  // guard against infinite recursion in case this flush
  // leads to other state changes that trigger another flush
  if (flushing || !draw_batch_dirty) return;
  flushing = true;

  // the never flush mode means the batch isn't drawn
  // we must still clear it from memory to avoid leaks
  if (draw_batch_mode != batch_flush_never) {
    // when we start a batch using draw_batch_begin_deferred
    // the render state is actually made current immediately
    // along with the texture that the batch uses so we don't
    // actually want any state flush to occur while we draw
    // this batch and then we can restore the dirty state for
    // the next batch or vertex submit to flush the new state
    bool wasStateDirty = enigma::draw_get_state_dirty();
    enigma::draw_set_state_dirty(false);
    d3d_model_draw(draw_get_batch_stream());
    enigma::draw_set_state_dirty(wasStateDirty);
  }
  d3d_model_clear(draw_get_batch_stream());

  flushing = false;
  draw_batch_dirty = false;
}
void d3d_primitive_end()
{
  if (prim_d3d_texture != -1) {
    texture_use(get_texture(prim_d3d_texture));
  }
  prim_d3d_texture = -1;
  d3d_model_draw(prim_d3d_model);
  d3d_model_clear(prim_d3d_model);
}
int draw_primitive_end()
{
  if (prim_draw_texture != -1) {
    texture_use(get_texture(prim_draw_texture));
  }
  prim_draw_texture = -1;
  d3d_model_draw(prim_draw_model);
  d3d_model_clear(prim_draw_model);
  return 0;
}