radeonsi: implement TC L2 write-back (flush) without cache invalidation
[mesa.git] / src / gallium / drivers / swr / swr_scratch.cpp
1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24 #include "util/u_memory.h"
25 #include "swr_context.h"
26 #include "swr_scratch.h"
27 #include "api.h"
28
29
30 void *
31 swr_copy_to_scratch_space(struct swr_context *ctx,
32 struct swr_scratch_space *space,
33 const void *user_buffer,
34 unsigned int size)
35 {
36 void *ptr;
37 assert(space);
38 assert(user_buffer);
39 assert(size);
40
41 if (size >= 2048) { /* XXX TODO create KNOB_ for this */
42 /* Use per draw SwrAllocDrawContextMemory for larger copies */
43 ptr = SwrAllocDrawContextMemory(ctx->swrContext, size, 4);
44 } else {
45 /* Allocate enough so that MAX_DRAWS_IN_FLIGHT sets fit. */
46 unsigned int max_size_in_flight = size * KNOB_MAX_DRAWS_IN_FLIGHT;
47
48 /* Need to grow space */
49 if (max_size_in_flight > space->current_size) {
50 /* Must idle the pipeline, this is infrequent */
51 SwrWaitForIdle(ctx->swrContext);
52
53 space->current_size = max_size_in_flight;
54
55 if (space->base) {
56 align_free(space->base);
57 space->base = NULL;
58 }
59
60 if (!space->base) {
61 space->base = (uint8_t *)align_malloc(space->current_size, 4);
62 space->head = (void *)space->base;
63 }
64 }
65
66 /* Wrap */
67 if (((uint8_t *)space->head + size)
68 >= ((uint8_t *)space->base + space->current_size)) {
69 /*
70 * TODO XXX: Should add a fence on wrap. Assumption is that
71 * current_space >> size, and there are at least MAX_DRAWS_IN_FLIGHT
72 * draws in scratch. So fence would always be met on wrap. A fence
73 * would ensure that first frame in buffer is done before wrapping.
74 * If fence ever needs to be waited on, can increase buffer size.
75 * So far in testing, this hasn't been necessary.
76 */
77 space->head = space->base;
78 }
79
80 ptr = space->head;
81 space->head = (uint8_t *)space->head + size;
82 }
83
84 /* Copy user_buffer to scratch */
85 memcpy(ptr, user_buffer, size);
86
87 return ptr;
88 }
89
90
91 void
92 swr_init_scratch_buffers(struct swr_context *ctx)
93 {
94 struct swr_scratch_buffers *scratch;
95
96 scratch = CALLOC_STRUCT(swr_scratch_buffers);
97 ctx->scratch = scratch;
98 }
99
100 void
101 swr_destroy_scratch_buffers(struct swr_context *ctx)
102 {
103 struct swr_scratch_buffers *scratch = ctx->scratch;
104
105 if (scratch) {
106 if (scratch->vs_constants.base)
107 align_free(scratch->vs_constants.base);
108 if (scratch->fs_constants.base)
109 align_free(scratch->fs_constants.base);
110 if (scratch->vertex_buffer.base)
111 align_free(scratch->vertex_buffer.base);
112 if (scratch->index_buffer.base)
113 align_free(scratch->index_buffer.base);
114 FREE(scratch);
115 }
116 }