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_screen.h"
27 #include "swr_scratch.h"
28 #include "swr_fence_work.h"
29 #include "api.h"
30
31
32 void *
swr_copy_to_scratch_space(struct swr_context * ctx,struct swr_scratch_space * space,const void * user_buffer,unsigned int size)33 swr_copy_to_scratch_space(struct swr_context *ctx,
34 struct swr_scratch_space *space,
35 const void *user_buffer,
36 unsigned int size)
37 {
38 void *ptr;
39 assert(space);
40 assert(size);
41
42 if (size >= 2048) { /* XXX TODO create KNOB_ for this */
43 /* Use per draw SwrAllocDrawContextMemory for larger copies */
44 ptr = SwrAllocDrawContextMemory(ctx->swrContext, size, 4);
45 } else {
46 /* Allocate enough so that MAX_DRAWS_IN_FLIGHT sets fit. */
47 unsigned int max_size_in_flight = size * KNOB_MAX_DRAWS_IN_FLIGHT;
48
49 /* Need to grow space */
50 if (max_size_in_flight > space->current_size) {
51 space->current_size = max_size_in_flight;
52
53 if (space->base) {
54 /* defer delete, use aligned-free */
55 struct swr_screen *screen = swr_screen(ctx->pipe.screen);
56 swr_fence_work_free(screen->flush_fence, space->base, true);
57 space->base = NULL;
58 }
59
60 if (!space->base) {
61 space->base = (uint8_t *)AlignedMalloc(space->current_size,
62 sizeof(void *));
63 space->head = (void *)space->base;
64 }
65 }
66
67 /* Wrap */
68 if (((uint8_t *)space->head + size)
69 >= ((uint8_t *)space->base + space->current_size)) {
70 space->head = space->base;
71 }
72
73 ptr = space->head;
74 space->head = (uint8_t *)space->head + size;
75 }
76
77 /* Copy user_buffer to scratch */
78 if (user_buffer)
79 memcpy(ptr, user_buffer, size);
80
81 return ptr;
82 }
83
84
85 void
swr_init_scratch_buffers(struct swr_context * ctx)86 swr_init_scratch_buffers(struct swr_context *ctx)
87 {
88 struct swr_scratch_buffers *scratch;
89
90 scratch = CALLOC_STRUCT(swr_scratch_buffers);
91 ctx->scratch = scratch;
92 }
93
94 void
swr_destroy_scratch_buffers(struct swr_context * ctx)95 swr_destroy_scratch_buffers(struct swr_context *ctx)
96 {
97 struct swr_scratch_buffers *scratch = ctx->scratch;
98
99 if (scratch) {
100 AlignedFree(scratch->vs_constants.base);
101 AlignedFree(scratch->fs_constants.base);
102 AlignedFree(scratch->vertex_buffer.base);
103 AlignedFree(scratch->index_buffer.base);
104 FREE(scratch);
105 }
106 }
107