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
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
);
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
);
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
);
54 void * CDECL
wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view
*view
)
56 TRACE("view %p.\n", view
);
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
)))
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
;
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 view
->format_flags
= view
->format
->flags
[resource
->gl_type
];
105 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
107 view
->sub_resource_idx
= 0;
108 view
->buffer_offset
= desc
->u
.buffer
.start_idx
;
109 view
->width
= desc
->u
.buffer
.count
;
115 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
116 struct wined3d_resource
*sub_resource
;
118 view
->sub_resource_idx
= desc
->u
.texture
.layer_idx
* texture
->level_count
+ desc
->u
.texture
.level_idx
;
119 sub_resource
= wined3d_texture_get_sub_resource(texture
, view
->sub_resource_idx
);
121 view
->buffer_offset
= 0;
122 view
->width
= sub_resource
->width
;
123 view
->height
= sub_resource
->height
;
124 view
->depth
= desc
->u
.texture
.layer_count
;
128 HRESULT CDECL
wined3d_rendertarget_view_create(const struct wined3d_rendertarget_view_desc
*desc
,
129 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
130 struct wined3d_rendertarget_view
**view
)
132 struct wined3d_rendertarget_view
*object
;
134 TRACE("desc %p, resource %p, parent %p, view %p.\n",
135 desc
, resource
, parent
, view
);
137 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
138 return E_OUTOFMEMORY
;
140 wined3d_rendertarget_view_init(object
, desc
, resource
, parent
, parent_ops
);
142 TRACE("Created render target view %p.\n", object
);
148 HRESULT CDECL
wined3d_rendertarget_view_create_from_surface(struct wined3d_surface
*surface
,
149 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_rendertarget_view
**view
)
151 struct wined3d_rendertarget_view_desc desc
;
153 TRACE("surface %p, view %p.\n", surface
, view
);
155 desc
.format_id
= surface
->resource
.format
->id
;
156 desc
.u
.texture
.level_idx
= surface
->texture_level
;
157 desc
.u
.texture
.layer_idx
= surface
->texture_layer
;
158 desc
.u
.texture
.layer_count
= 1;
160 return wined3d_rendertarget_view_create(&desc
, &surface
->container
->resource
, parent
, parent_ops
, view
);
163 ULONG CDECL
wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view
*view
)
165 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
167 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
172 ULONG CDECL
wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view
*view
)
174 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
176 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
180 /* Call wined3d_object_destroyed() before releasing the resource,
181 * since releasing the resource may end up destroying the parent. */
182 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
183 wined3d_resource_decref(view
->resource
);
184 HeapFree(GetProcessHeap(), 0, view
);
190 void * CDECL
wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view
*view
)
192 TRACE("view %p.\n", view
);
197 HRESULT CDECL
wined3d_shader_resource_view_create(struct wined3d_resource
*resource
, void *parent
,
198 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_shader_resource_view
**view
)
200 struct wined3d_shader_resource_view
*object
;
202 TRACE("parent %p, parent_ops %p, view %p.\n", parent
, parent_ops
, view
);
204 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
205 return E_OUTOFMEMORY
;
207 object
->refcount
= 1;
208 object
->resource
= resource
;
209 wined3d_resource_incref(resource
);
210 object
->parent
= parent
;
211 object
->parent_ops
= parent_ops
;
213 TRACE("Created shader resource view %p.\n", object
);