aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/swr/swr_scratch.cpp
blob: 2515c8beaf390e64f3f0978a90b085e4d3446e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/****************************************************************************
 * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 ***************************************************************************/

#include "util/u_memory.h"
#include "swr_context.h"
#include "swr_scratch.h"
#include "api.h"


void *
swr_copy_to_scratch_space(struct swr_context *ctx,
                          struct swr_scratch_space *space,
                          const void *user_buffer,
                          unsigned int size)
{
   void *ptr;
   assert(space);
   assert(size);

   if (size >= 2048) { /* XXX TODO create KNOB_ for this */
      /* Use per draw SwrAllocDrawContextMemory for larger copies */
      ptr = SwrAllocDrawContextMemory(ctx->swrContext, size, 4);
   } else {
      /* Allocate enough so that MAX_DRAWS_IN_FLIGHT sets fit. */
      unsigned int max_size_in_flight = size * KNOB_MAX_DRAWS_IN_FLIGHT;

      /* Need to grow space */
      if (max_size_in_flight > space->current_size) {
         /* Must idle the pipeline, this is infrequent */
         SwrWaitForIdle(ctx->swrContext);

         space->current_size = max_size_in_flight;

         if (space->base) {
            align_free(space->base);
            space->base = NULL;
         }

         if (!space->base) {
            space->base = (uint8_t *)align_malloc(space->current_size, 4);
            space->head = (void *)space->base;
         }
      }

      /* Wrap */
      if (((uint8_t *)space->head + size)
          >= ((uint8_t *)space->base + space->current_size)) {
         /*
          * TODO XXX: Should add a fence on wrap.  Assumption is that
          * current_space >> size, and there are at least MAX_DRAWS_IN_FLIGHT
          * draws in scratch.  So fence would always be met on wrap.  A fence
          * would ensure that first frame in buffer is done before wrapping.
          * If fence ever needs to be waited on, can increase buffer size.
          * So far in testing, this hasn't been necessary.
          */
         space->head = space->base;
      }

      ptr = space->head;
      space->head = (uint8_t *)space->head + size;
   }

   /* Copy user_buffer to scratch */
   if (user_buffer)
      memcpy(ptr, user_buffer, size);

   return ptr;
}


void
swr_init_scratch_buffers(struct swr_context *ctx)
{
   struct swr_scratch_buffers *scratch;

   scratch = CALLOC_STRUCT(swr_scratch_buffers);
   ctx->scratch = scratch;
}

void
swr_destroy_scratch_buffers(struct swr_context *ctx)
{
   struct swr_scratch_buffers *scratch = ctx->scratch;

   if (scratch) {
      if (scratch->vs_constants.base)
         align_free(scratch->vs_constants.base);
      if (scratch->fs_constants.base)
         align_free(scratch->fs_constants.base);
      if (scratch->vertex_buffer.base)
         align_free(scratch->vertex_buffer.base);
      if (scratch->index_buffer.base)
         align_free(scratch->index_buffer.base);
      FREE(scratch);
   }
}