wined3d: Use a separate STATE_TRANSFORM(WINED3D_TS_VIEW) state handler in the GLSL...
[wine/multimedia.git] / dlls / wined3d / view.c
blob9cda64d921fbcf0de1aaf265d07d76164834ada9
1 /*
2 * Copyright 2009, 2011 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 ULONG CDECL wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view *view)
29 ULONG refcount = InterlockedIncrement(&view->refcount);
31 TRACE("%p increasing refcount to %u.\n", view, refcount);
33 return refcount;
36 ULONG CDECL wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view *view)
38 ULONG refcount = InterlockedDecrement(&view->refcount);
40 TRACE("%p decreasing refcount to %u.\n", view, refcount);
42 if (!refcount)
44 /* Call wined3d_object_destroyed() before releasing the resource,
45 * since releasing the resource may end up destroying the parent. */
46 view->parent_ops->wined3d_object_destroyed(view->parent);
47 wined3d_resource_decref(view->resource);
48 HeapFree(GetProcessHeap(), 0, view);
51 return refcount;
54 void * CDECL wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view *view)
56 TRACE("view %p.\n", view);
58 return view->parent;
61 void * CDECL wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view *view)
63 struct wined3d_resource *sub_resource;
65 TRACE("view %p.\n", view);
67 if (view->resource->type == WINED3D_RTYPE_BUFFER)
68 return wined3d_buffer_get_parent(buffer_from_resource(view->resource));
70 if (!(sub_resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(view->resource),
71 view->sub_resource_idx)))
72 return NULL;
74 return wined3d_resource_get_parent(sub_resource);
77 void CDECL wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view *view, void *parent)
79 TRACE("view %p, parent %p.\n", view, parent);
81 view->parent = parent;
84 struct wined3d_resource * CDECL wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view *view)
86 TRACE("view %p.\n", view);
88 return view->resource;
91 static void wined3d_rendertarget_view_init(struct wined3d_rendertarget_view *view,
92 const struct wined3d_rendertarget_view_desc *desc, struct wined3d_resource *resource,
93 void *parent, const struct wined3d_parent_ops *parent_ops)
95 const struct wined3d_gl_info *gl_info = &resource->device->adapter->gl_info;
97 view->refcount = 1;
98 view->resource = resource;
99 wined3d_resource_incref(resource);
100 view->parent = parent;
101 view->parent_ops = parent_ops;
103 view->format = wined3d_get_format(gl_info, desc->format_id);
104 if (resource->type == WINED3D_RTYPE_BUFFER)
106 view->sub_resource_idx = 0;
107 view->buffer_offset = desc->u.buffer.start_idx;
108 view->width = desc->u.buffer.count;
109 view->height = 1;
110 view->depth = 1;
112 else
114 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
115 struct wined3d_resource *sub_resource;
117 view->sub_resource_idx = desc->u.texture.layer_idx * texture->level_count + desc->u.texture.level_idx;
118 sub_resource = wined3d_texture_get_sub_resource(texture, view->sub_resource_idx);
120 view->buffer_offset = 0;
121 view->width = sub_resource->width;
122 view->height = sub_resource->height;
123 view->depth = desc->u.texture.layer_count;
127 HRESULT CDECL wined3d_rendertarget_view_create(const struct wined3d_rendertarget_view_desc *desc,
128 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
129 struct wined3d_rendertarget_view **view)
131 struct wined3d_rendertarget_view *object;
133 TRACE("desc %p, resource %p, parent %p, view %p.\n",
134 desc, resource, parent, view);
136 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
137 return E_OUTOFMEMORY;
139 wined3d_rendertarget_view_init(object, desc, resource, parent, parent_ops);
141 TRACE("Created render target view %p.\n", object);
142 *view = object;
144 return WINED3D_OK;
147 HRESULT CDECL wined3d_rendertarget_view_create_from_surface(struct wined3d_surface *surface,
148 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_rendertarget_view **view)
150 struct wined3d_rendertarget_view_desc desc;
152 TRACE("surface %p, view %p.\n", surface, view);
154 desc.format_id = surface->resource.format->id;
155 desc.u.texture.level_idx = surface->texture_level;
156 desc.u.texture.layer_idx = surface->texture_layer;
157 desc.u.texture.layer_count = 1;
159 return wined3d_rendertarget_view_create(&desc, &surface->container->resource, parent, parent_ops, view);
162 ULONG CDECL wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view *view)
164 ULONG refcount = InterlockedIncrement(&view->refcount);
166 TRACE("%p increasing refcount to %u.\n", view, refcount);
168 return refcount;
171 ULONG CDECL wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view *view)
173 ULONG refcount = InterlockedDecrement(&view->refcount);
175 TRACE("%p decreasing refcount to %u.\n", view, refcount);
177 if (!refcount)
179 /* Call wined3d_object_destroyed() before releasing the resource,
180 * since releasing the resource may end up destroying the parent. */
181 view->parent_ops->wined3d_object_destroyed(view->parent);
182 wined3d_resource_decref(view->resource);
183 HeapFree(GetProcessHeap(), 0, view);
186 return refcount;
189 void * CDECL wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view *view)
191 TRACE("view %p.\n", view);
193 return view->parent;
196 HRESULT CDECL wined3d_shader_resource_view_create(struct wined3d_resource *resource, void *parent,
197 const struct wined3d_parent_ops *parent_ops, struct wined3d_shader_resource_view **view)
199 struct wined3d_shader_resource_view *object;
201 TRACE("parent %p, parent_ops %p, view %p.\n", parent, parent_ops, view);
203 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
204 return E_OUTOFMEMORY;
206 object->refcount = 1;
207 object->resource = resource;
208 wined3d_resource_incref(resource);
209 object->parent = parent;
210 object->parent_ops = parent_ops;
212 TRACE("Created shader resource view %p.\n", object);
213 *view = object;
215 return WINED3D_OK;