1b8e80941Smrg/* 2b8e80941Smrg * Copyright 2011 Joakim Sindholt <opensource@zhasha.com> 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub 8b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom 9b8e80941Smrg * the Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 22b8e80941Smrg 23b8e80941Smrg#include "resource9.h" 24b8e80941Smrg#include "device9.h" 25b8e80941Smrg#include "nine_helpers.h" 26b8e80941Smrg#include "nine_defines.h" 27b8e80941Smrg 28b8e80941Smrg#include "util/u_inlines.h" 29b8e80941Smrg#include "util/u_resource.h" 30b8e80941Smrg 31b8e80941Smrg#include "pipe/p_screen.h" 32b8e80941Smrg 33b8e80941Smrg#define DBG_CHANNEL DBG_RESOURCE 34b8e80941Smrg 35b8e80941SmrgHRESULT 36b8e80941SmrgNineResource9_ctor( struct NineResource9 *This, 37b8e80941Smrg struct NineUnknownParams *pParams, 38b8e80941Smrg struct pipe_resource *initResource, 39b8e80941Smrg BOOL Allocate, 40b8e80941Smrg D3DRESOURCETYPE Type, 41b8e80941Smrg D3DPOOL Pool, 42b8e80941Smrg DWORD Usage) 43b8e80941Smrg{ 44b8e80941Smrg struct pipe_screen *screen; 45b8e80941Smrg HRESULT hr; 46b8e80941Smrg 47b8e80941Smrg DBG("This=%p pParams=%p initResource=%p Allocate=%d " 48b8e80941Smrg "Type=%d Pool=%d Usage=%d\n", 49b8e80941Smrg This, pParams, initResource, (int) Allocate, 50b8e80941Smrg Type, Pool, Usage); 51b8e80941Smrg 52b8e80941Smrg hr = NineUnknown_ctor(&This->base, pParams); 53b8e80941Smrg if (FAILED(hr)) 54b8e80941Smrg return hr; 55b8e80941Smrg 56b8e80941Smrg This->info.screen = screen = This->base.device->screen; 57b8e80941Smrg if (initResource) 58b8e80941Smrg pipe_resource_reference(&This->resource, initResource); 59b8e80941Smrg 60b8e80941Smrg if (Allocate) { 61b8e80941Smrg assert(!initResource); 62b8e80941Smrg 63b8e80941Smrg /* On Windows it is possible allocation fails when 64b8e80941Smrg * IDirect3DDevice9::GetAvailableTextureMem() still reports 65b8e80941Smrg * enough free space. 66b8e80941Smrg * 67b8e80941Smrg * Some games allocate surfaces 68b8e80941Smrg * in a loop until they receive D3DERR_OUTOFVIDEOMEMORY to measure 69b8e80941Smrg * the available texture memory size. 70b8e80941Smrg * 71b8e80941Smrg * We are not using the drivers VRAM statistics because: 72b8e80941Smrg * * This would add overhead to each resource allocation. 73b8e80941Smrg * * Freeing memory is lazy and takes some time, but applications 74b8e80941Smrg * expects the memory counter to change immediately after allocating 75b8e80941Smrg * or freeing memory. 76b8e80941Smrg * 77b8e80941Smrg * Vertexbuffers and indexbuffers are not accounted ! 78b8e80941Smrg */ 79b8e80941Smrg if (This->info.target != PIPE_BUFFER) { 80b8e80941Smrg This->size = util_resource_size(&This->info); 81b8e80941Smrg 82b8e80941Smrg p_atomic_add(&This->base.device->available_texture_mem, -This->size); 83b8e80941Smrg if (This->base.device->available_texture_mem <= 84b8e80941Smrg This->base.device->available_texture_limit) { 85b8e80941Smrg return D3DERR_OUTOFVIDEOMEMORY; 86b8e80941Smrg } 87b8e80941Smrg } 88b8e80941Smrg 89b8e80941Smrg DBG("(%p) Creating pipe_resource.\n", This); 90b8e80941Smrg This->resource = screen->resource_create(screen, &This->info); 91b8e80941Smrg if (!This->resource) 92b8e80941Smrg return D3DERR_OUTOFVIDEOMEMORY; 93b8e80941Smrg } 94b8e80941Smrg 95b8e80941Smrg This->type = Type; 96b8e80941Smrg This->pool = Pool; 97b8e80941Smrg This->usage = Usage; 98b8e80941Smrg This->priority = 0; 99b8e80941Smrg 100b8e80941Smrg return D3D_OK; 101b8e80941Smrg} 102b8e80941Smrg 103b8e80941Smrgvoid 104b8e80941SmrgNineResource9_dtor( struct NineResource9 *This ) 105b8e80941Smrg{ 106b8e80941Smrg DBG("This=%p\n", This); 107b8e80941Smrg 108b8e80941Smrg /* NOTE: We do have to use refcounting, the driver might 109b8e80941Smrg * still hold a reference. */ 110b8e80941Smrg pipe_resource_reference(&This->resource, NULL); 111b8e80941Smrg 112b8e80941Smrg /* NOTE: size is 0, unless something has actually been allocated */ 113b8e80941Smrg if (This->base.device) 114b8e80941Smrg p_atomic_add(&This->base.device->available_texture_mem, This->size); 115b8e80941Smrg 116b8e80941Smrg NineUnknown_dtor(&This->base); 117b8e80941Smrg} 118b8e80941Smrg 119b8e80941Smrgstruct pipe_resource * 120b8e80941SmrgNineResource9_GetResource( struct NineResource9 *This ) 121b8e80941Smrg{ 122b8e80941Smrg return This->resource; 123b8e80941Smrg} 124b8e80941Smrg 125b8e80941SmrgD3DPOOL 126b8e80941SmrgNineResource9_GetPool( struct NineResource9 *This ) 127b8e80941Smrg{ 128b8e80941Smrg return This->pool; 129b8e80941Smrg} 130b8e80941Smrg 131b8e80941SmrgDWORD NINE_WINAPI 132b8e80941SmrgNineResource9_SetPriority( struct NineResource9 *This, 133b8e80941Smrg DWORD PriorityNew ) 134b8e80941Smrg{ 135b8e80941Smrg DWORD prev; 136b8e80941Smrg DBG("This=%p, PriorityNew=%d\n", This, PriorityNew); 137b8e80941Smrg 138b8e80941Smrg if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE) 139b8e80941Smrg return 0; 140b8e80941Smrg 141b8e80941Smrg prev = This->priority; 142b8e80941Smrg This->priority = PriorityNew; 143b8e80941Smrg return prev; 144b8e80941Smrg} 145b8e80941Smrg 146b8e80941SmrgDWORD NINE_WINAPI 147b8e80941SmrgNineResource9_GetPriority( struct NineResource9 *This ) 148b8e80941Smrg{ 149b8e80941Smrg if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE) 150b8e80941Smrg return 0; 151b8e80941Smrg 152b8e80941Smrg return This->priority; 153b8e80941Smrg} 154b8e80941Smrg 155b8e80941Smrg/* NOTE: Don't forget to adjust locked vtable if you change this ! */ 156b8e80941Smrgvoid NINE_WINAPI 157b8e80941SmrgNineResource9_PreLoad( struct NineResource9 *This ) 158b8e80941Smrg{ 159b8e80941Smrg if (This->pool != D3DPOOL_MANAGED) 160b8e80941Smrg return; 161b8e80941Smrg /* We don't treat managed vertex or index buffers different from 162b8e80941Smrg * default ones (are managed vertex buffers even allowed ?), and 163b8e80941Smrg * the PreLoad for textures is overridden by superclass. 164b8e80941Smrg */ 165b8e80941Smrg} 166b8e80941Smrg 167b8e80941SmrgD3DRESOURCETYPE NINE_WINAPI 168b8e80941SmrgNineResource9_GetType( struct NineResource9 *This ) 169b8e80941Smrg{ 170b8e80941Smrg return This->type; 171b8e80941Smrg} 172