summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/nvfx/nvfx_buffer.c
blob: 05b824b8f74f57b9e1fab2220b4962743c883f42 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#include "util/u_inlines.h"
#include "util/u_memory.h"
#include "util/u_math.h"

#include "nouveau/nouveau_screen.h"
#include "nouveau/nouveau_winsys.h"
#include "nvfx_resource.h"


/* Currently using separate implementations for buffers and textures,
 * even though gallium has a unified abstraction of these objects.
 * Eventually these should be combined, and mechanisms like transfers
 * be adapted to work for both buffer and texture uploads.
 */
static void nvfx_buffer_destroy(struct pipe_screen *pscreen,
				struct pipe_resource *presource)
{
	struct nvfx_resource *buffer = nvfx_resource(presource);

	nouveau_screen_bo_release(pscreen, buffer->bo);
	FREE(buffer);
}




/* Utility functions for transfer create/destroy are hooked in and
 * just record the arguments to those functions.
 */
static void *
nvfx_buffer_transfer_map( struct pipe_context *pipe,
			  struct pipe_transfer *transfer )
{
	struct nvfx_resource *buffer = nvfx_resource(transfer->resource);
	uint8_t *map;

	map = nouveau_screen_bo_map_range( pipe->screen,
					   buffer->bo,
					   transfer->box.x,
					   transfer->box.width,
					   nouveau_screen_transfer_flags(transfer->usage) );
	if (map == NULL)
		return NULL;
	
	return map + transfer->box.x;
}



static void nvfx_buffer_transfer_flush_region( struct pipe_context *pipe,
					       struct pipe_transfer *transfer,
					       const struct pipe_box *box)
{
	struct nvfx_resource *buffer = nvfx_resource(transfer->resource);

	nouveau_screen_bo_map_flush_range(pipe->screen,
					  buffer->bo,
					  transfer->box.x + box->x,
					  box->width);
}

static void nvfx_buffer_transfer_unmap( struct pipe_context *pipe,
					struct pipe_transfer *transfer )
{
	struct nvfx_resource *buffer = nvfx_resource(transfer->resource);

	nouveau_screen_bo_unmap(pipe->screen, buffer->bo);
}




struct u_resource_vtbl nvfx_buffer_vtbl = 
{
	u_default_resource_get_handle,      /* get_handle */
	nvfx_buffer_destroy,		     /* resource_destroy */
	NULL,			    /* is_resource_referenced */
	u_default_get_transfer,	     /* get_transfer */
	u_default_transfer_destroy,	     /* transfer_destroy */
	nvfx_buffer_transfer_map,	     /* transfer_map */
	nvfx_buffer_transfer_flush_region,  /* transfer_flush_region */
	nvfx_buffer_transfer_unmap,	     /* transfer_unmap */
	u_default_transfer_inline_write   /* transfer_inline_write */
};



struct pipe_resource *
nvfx_buffer_create(struct pipe_screen *pscreen,
		   const struct pipe_resource *template)
{
	struct nvfx_resource *buffer;

	buffer = CALLOC_STRUCT(nvfx_resource);
	if (!buffer)
		return NULL;

	buffer->base = *template;
	buffer->vtbl = &nvfx_buffer_vtbl;
	pipe_reference_init(&buffer->base.reference, 1);
	buffer->base.screen = pscreen;

	buffer->bo = nouveau_screen_bo_new(pscreen,
					   16,
					   buffer->base.usage,
					   buffer->base.bind,
					   buffer->base.width0);

	if (buffer->bo == NULL)
		goto fail;

	return &buffer->base;

fail:
	FREE(buffer);
	return NULL;
}


struct pipe_resource *
nvfx_user_buffer_create(struct pipe_screen *pscreen,
			void *ptr,
			unsigned bytes,
			unsigned usage)
{
	struct nvfx_resource *buffer;

	buffer = CALLOC_STRUCT(nvfx_resource);
	if (!buffer)
		return NULL;

	pipe_reference_init(&buffer->base.reference, 1);
	buffer->vtbl = &nvfx_buffer_vtbl;
	buffer->base.screen = pscreen;
	buffer->base.format = PIPE_FORMAT_R8_UNORM;
	buffer->base.usage = PIPE_USAGE_IMMUTABLE;
	buffer->base.bind = usage;
	buffer->base.width0 = bytes;
	buffer->base.height0 = 1;
	buffer->base.depth0 = 1;

	buffer->bo = nouveau_screen_bo_user(pscreen, ptr, bytes);
	if (!buffer->bo)
		goto fail;
	
	return &buffer->base;

fail:
	FREE(buffer);
	return NULL;
}