nvc0: do not force re-binding of compute constbufs on Fermi
[mesa.git] / src / gallium / auxiliary / util / u_suballoc.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2012 Marek Olšák <maraeo@gmail.com>
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /* A simple allocator that suballocates memory from a large buffer. */
30
31 #include "pipe/p_defines.h"
32 #include "util/u_inlines.h"
33 #include "pipe/p_context.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
36
37 #include "u_suballoc.h"
38
39
40 struct u_suballocator {
41 struct pipe_context *pipe;
42
43 unsigned size; /* Size of the whole buffer, in bytes. */
44 unsigned alignment; /* Alignment of each sub-allocation. */
45 unsigned bind; /* Bitmask of PIPE_BIND_* flags. */
46 unsigned usage; /* One of PIPE_USAGE_* flags. */
47 boolean zero_buffer_memory; /* If the buffer contents should be zeroed. */
48
49 struct pipe_resource *buffer; /* The buffer we suballocate from. */
50 unsigned offset; /* Aligned offset pointing at the first unused byte. */
51 };
52
53
54 /**
55 * Create a suballocator.
56 *
57 * \p zero_buffer_memory determines whether the buffer contents should be
58 * cleared to 0 after the allocation.
59 */
60 struct u_suballocator *
61 u_suballocator_create(struct pipe_context *pipe, unsigned size,
62 unsigned alignment, unsigned bind, unsigned usage,
63 boolean zero_buffer_memory)
64 {
65 struct u_suballocator *allocator = CALLOC_STRUCT(u_suballocator);
66 if (!allocator)
67 return NULL;
68
69 allocator->pipe = pipe;
70 allocator->size = align(size, alignment);
71 allocator->alignment = alignment;
72 allocator->bind = bind;
73 allocator->usage = usage;
74 allocator->zero_buffer_memory = zero_buffer_memory;
75 return allocator;
76 }
77
78 void
79 u_suballocator_destroy(struct u_suballocator *allocator)
80 {
81 pipe_resource_reference(&allocator->buffer, NULL);
82 FREE(allocator);
83 }
84
85 void
86 u_suballocator_alloc(struct u_suballocator *allocator, unsigned size,
87 unsigned *out_offset, struct pipe_resource **outbuf)
88 {
89 unsigned alloc_size = align(size, allocator->alignment);
90
91 /* Don't allow allocations larger than the buffer size. */
92 if (alloc_size > allocator->size)
93 goto fail;
94
95 /* Make sure we have enough space in the buffer. */
96 if (!allocator->buffer ||
97 allocator->offset + alloc_size > allocator->size) {
98 /* Allocate a new buffer. */
99 pipe_resource_reference(&allocator->buffer, NULL);
100 allocator->offset = 0;
101 allocator->buffer =
102 pipe_buffer_create(allocator->pipe->screen, allocator->bind,
103 allocator->usage, allocator->size);
104 if (!allocator->buffer)
105 goto fail;
106
107 /* Clear the memory if needed. */
108 if (allocator->zero_buffer_memory) {
109 struct pipe_transfer *transfer = NULL;
110 void *ptr;
111
112 ptr = pipe_buffer_map(allocator->pipe, allocator->buffer,
113 PIPE_TRANSFER_WRITE, &transfer);
114 memset(ptr, 0, allocator->size);
115 pipe_buffer_unmap(allocator->pipe, transfer);
116 }
117 }
118
119 assert(allocator->offset % allocator->alignment == 0);
120 assert(allocator->offset < allocator->buffer->width0);
121 assert(allocator->offset + alloc_size <= allocator->buffer->width0);
122
123 /* Return the buffer. */
124 *out_offset = allocator->offset;
125 pipe_resource_reference(outbuf, allocator->buffer);
126
127 allocator->offset += alloc_size;
128 return;
129
130 fail:
131 pipe_resource_reference(outbuf, NULL);
132 }