aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/swr/swr_scratch.cpp
blob: db095dea7e33b049618309a9ba03cc65f6fd5b83 (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
/****************************************************************************
 * 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_screen.h"
#include "swr_scratch.h"
#include "swr_fence_work.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) {
         space->current_size = max_size_in_flight;

         if (space->base) {
            /* defer delete, use aligned-free */
            struct swr_screen *screen = swr_screen(ctx->pipe.screen);
            swr_fence_work_free(screen->flush_fence, space->base, true);
            space->base = NULL;
         }

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

      /* Wrap */
      if (((uint8_t *)space->head + size)
          >= ((uint8_t *)space->base + space->current_size)) {
         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) {
      AlignedFree(scratch->vs_constants.base);
      AlignedFree(scratch->fs_constants.base);
      AlignedFree(scratch->gs_constants.base);
      AlignedFree(scratch->vertex_buffer.base);
      AlignedFree(scratch->index_buffer.base);
      FREE(scratch);
   }
}