1af69d88dSmrg/**********************************************************
2af69d88dSmrg * Copyright 2008-2012 VMware, Inc.  All rights reserved.
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person
5af69d88dSmrg * obtaining a copy of this software and associated documentation
6af69d88dSmrg * files (the "Software"), to deal in the Software without
7af69d88dSmrg * restriction, including without limitation the rights to use, copy,
8af69d88dSmrg * modify, merge, publish, distribute, sublicense, and/or sell copies
9af69d88dSmrg * of the Software, and to permit persons to whom the Software is
10af69d88dSmrg * furnished to do so, subject to the following conditions:
11af69d88dSmrg *
12af69d88dSmrg * The above copyright notice and this permission notice shall be
13af69d88dSmrg * included in all copies or substantial portions of the Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16af69d88dSmrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17af69d88dSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18af69d88dSmrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19af69d88dSmrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20af69d88dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21af69d88dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22af69d88dSmrg * SOFTWARE.
23af69d88dSmrg *
24af69d88dSmrg **********************************************************/
25af69d88dSmrg
263464ebd5Sriastradh#include "util/u_debug.h"
273464ebd5Sriastradh
283464ebd5Sriastradh#include "svga_resource.h"
293464ebd5Sriastradh#include "svga_resource_buffer.h"
303464ebd5Sriastradh#include "svga_resource_texture.h"
313464ebd5Sriastradh#include "svga_context.h"
323464ebd5Sriastradh#include "svga_screen.h"
33af69d88dSmrg#include "svga_format.h"
343464ebd5Sriastradh
353464ebd5Sriastradh
3601e04c3fSmrg/**
3701e04c3fSmrg * This is the primary driver entrypoint for allocating graphics memory
3801e04c3fSmrg * (vertex/index/constant buffers, textures, etc)
3901e04c3fSmrg */
403464ebd5Sriastradhstatic struct pipe_resource *
413464ebd5Sriastradhsvga_resource_create(struct pipe_screen *screen,
42af69d88dSmrg                     const struct pipe_resource *template)
433464ebd5Sriastradh{
4401e04c3fSmrg   struct pipe_resource *r;
4501e04c3fSmrg
463464ebd5Sriastradh   if (template->target == PIPE_BUFFER)
4701e04c3fSmrg      r = svga_buffer_create(screen, template);
483464ebd5Sriastradh   else
4901e04c3fSmrg      r = svga_texture_create(screen, template);
5001e04c3fSmrg
5101e04c3fSmrg   if (!r) {
5201e04c3fSmrg      struct svga_screen *svgascreen = svga_screen(screen);
5301e04c3fSmrg      svgascreen->hud.num_failed_allocations++;
5401e04c3fSmrg   }
5501e04c3fSmrg
5601e04c3fSmrg   return r;
573464ebd5Sriastradh}
583464ebd5Sriastradh
59af69d88dSmrg
603464ebd5Sriastradhstatic struct pipe_resource *
613464ebd5Sriastradhsvga_resource_from_handle(struct pipe_screen * screen,
62af69d88dSmrg                          const struct pipe_resource *template,
6301e04c3fSmrg                          struct winsys_handle *whandle,
6401e04c3fSmrg                          unsigned usage)
653464ebd5Sriastradh{
663464ebd5Sriastradh   if (template->target == PIPE_BUFFER)
673464ebd5Sriastradh      return NULL;
683464ebd5Sriastradh   else
693464ebd5Sriastradh      return svga_texture_from_handle(screen, template, whandle);
703464ebd5Sriastradh}
713464ebd5Sriastradh
723464ebd5Sriastradh
73af69d88dSmrg/**
74af69d88dSmrg * Check if a resource (texture, buffer) of the given size
75af69d88dSmrg * and format can be created.
76af69d88dSmrg * \Return TRUE if OK, FALSE if too large.
77af69d88dSmrg */
787ec681f3Smrgstatic bool
79af69d88dSmrgsvga_can_create_resource(struct pipe_screen *screen,
80af69d88dSmrg                         const struct pipe_resource *res)
81af69d88dSmrg{
82af69d88dSmrg   struct svga_screen *svgascreen = svga_screen(screen);
83af69d88dSmrg   struct svga_winsys_screen *sws = svgascreen->sws;
84af69d88dSmrg   SVGA3dSurfaceFormat format;
85af69d88dSmrg   SVGA3dSize base_level_size;
86af69d88dSmrg   uint32 numMipLevels;
8701e04c3fSmrg   uint32 arraySize;
8801e04c3fSmrg   uint32 numSamples;
89af69d88dSmrg
90af69d88dSmrg   if (res->target == PIPE_BUFFER) {
91af69d88dSmrg      format = SVGA3D_BUFFER;
92af69d88dSmrg      base_level_size.width = res->width0;
93af69d88dSmrg      base_level_size.height = 1;
94af69d88dSmrg      base_level_size.depth = 1;
95af69d88dSmrg      numMipLevels = 1;
9601e04c3fSmrg      arraySize = 1;
9701e04c3fSmrg      numSamples = 0;
98af69d88dSmrg
99af69d88dSmrg   } else {
10001e04c3fSmrg      if (res->target == PIPE_TEXTURE_CUBE)
10101e04c3fSmrg         assert(res->array_size == 6);
10201e04c3fSmrg
103af69d88dSmrg      format = svga_translate_format(svgascreen, res->format, res->bind);
104af69d88dSmrg      if (format == SVGA3D_FORMAT_INVALID)
1057ec681f3Smrg         return false;
106af69d88dSmrg
107af69d88dSmrg      base_level_size.width = res->width0;
108af69d88dSmrg      base_level_size.height = res->height0;
109af69d88dSmrg      base_level_size.depth = res->depth0;
110af69d88dSmrg      numMipLevels = res->last_level + 1;
11101e04c3fSmrg      arraySize = res->array_size;
11201e04c3fSmrg      numSamples = res->nr_samples;
113af69d88dSmrg   }
114af69d88dSmrg
115af69d88dSmrg   return sws->surface_can_create(sws, format, base_level_size,
11601e04c3fSmrg                                  arraySize, numMipLevels, numSamples);
117af69d88dSmrg}
118af69d88dSmrg
119af69d88dSmrg
1203464ebd5Sriastradhvoid
1213464ebd5Sriastradhsvga_init_resource_functions(struct svga_context *svga)
1223464ebd5Sriastradh{
1237ec681f3Smrg   svga->pipe.buffer_map = svga_buffer_transfer_map;
1247ec681f3Smrg   svga->pipe.texture_map = svga_texture_transfer_map;
1257ec681f3Smrg   svga->pipe.transfer_flush_region = svga_buffer_transfer_flush_region;
1267ec681f3Smrg   svga->pipe.buffer_unmap = svga_buffer_transfer_unmap;
1277ec681f3Smrg   svga->pipe.texture_unmap = svga_texture_transfer_unmap;
12801e04c3fSmrg   svga->pipe.buffer_subdata = u_default_buffer_subdata;
12901e04c3fSmrg   svga->pipe.texture_subdata = u_default_texture_subdata;
13001e04c3fSmrg
13101e04c3fSmrg   if (svga_have_vgpu10(svga)) {
13201e04c3fSmrg      svga->pipe.generate_mipmap = svga_texture_generate_mipmap;
13301e04c3fSmrg   } else {
13401e04c3fSmrg      svga->pipe.generate_mipmap = NULL;
13501e04c3fSmrg   }
1363464ebd5Sriastradh}
1373464ebd5Sriastradh
1383464ebd5Sriastradhvoid
1393464ebd5Sriastradhsvga_init_screen_resource_functions(struct svga_screen *is)
1403464ebd5Sriastradh{
1413464ebd5Sriastradh   is->screen.resource_create = svga_resource_create;
1423464ebd5Sriastradh   is->screen.resource_from_handle = svga_resource_from_handle;
1437ec681f3Smrg   is->screen.resource_get_handle = svga_resource_get_handle;
1447ec681f3Smrg   is->screen.resource_destroy = svga_resource_destroy;
145af69d88dSmrg   is->screen.can_create_resource = svga_can_create_resource;
1463464ebd5Sriastradh}
147