1/*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23#ifndef _NINE_BASETEXTURE9_H_
24#define _NINE_BASETEXTURE9_H_
25
26#include "device9.h"
27#include "resource9.h"
28#include "util/u_inlines.h"
29#include "util/list.h"
30
31struct NineBaseTexture9
32{
33    struct NineResource9 base;
34    struct list_head list; /* for update_textures */
35    struct list_head list2; /* for managed_textures */
36
37    /* g3d */
38    struct pipe_sampler_view *view[2]; /* linear and sRGB */
39
40    D3DFORMAT format;
41
42    int16_t bind_count; /* to Device9->state.texture */
43
44    boolean shadow;
45    boolean fetch4_compatible;
46    uint8_t pstype; /* 0: 2D, 1: 1D, 2: CUBE, 3: 3D */
47
48    boolean dirty_mip;
49    D3DTEXTUREFILTERTYPE mipfilter;
50
51    unsigned level_count;
52
53    /* Specific to managed textures */
54    struct {
55        boolean dirty;
56        DWORD lod;
57        DWORD lod_resident;
58    } managed;
59};
60static inline struct NineBaseTexture9 *
61NineBaseTexture9( void *data )
62{
63    return (struct NineBaseTexture9 *)data;
64}
65
66HRESULT
67NineBaseTexture9_ctor( struct NineBaseTexture9 *This,
68                       struct NineUnknownParams *pParams,
69                       struct pipe_resource *initResource,
70                       D3DRESOURCETYPE Type,
71                       D3DFORMAT format,
72                       D3DPOOL Pool,
73                       DWORD Usage);
74
75void
76NineBaseTexture9_dtor( struct NineBaseTexture9 *This );
77
78DWORD NINE_WINAPI
79NineBaseTexture9_SetLOD( struct NineBaseTexture9 *This,
80                         DWORD LODNew );
81
82DWORD NINE_WINAPI
83NineBaseTexture9_GetLOD( struct NineBaseTexture9 *This );
84
85DWORD NINE_WINAPI
86NineBaseTexture9_GetLevelCount( struct NineBaseTexture9 *This );
87
88HRESULT NINE_WINAPI
89NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9 *This,
90                                       D3DTEXTUREFILTERTYPE FilterType );
91
92D3DTEXTUREFILTERTYPE NINE_WINAPI
93NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9 *This );
94
95void NINE_WINAPI
96NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This );
97
98void NINE_WINAPI
99NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This );
100
101void
102NineBaseTexture9_UnLoad( struct NineBaseTexture9 *This );
103
104/* For D3DPOOL_MANAGED only (after SetLOD change): */
105HRESULT
106NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
107                                     BOOL CopyData );
108
109/* For D3DPOOL_MANAGED only: */
110HRESULT
111NineBaseTexture9_UploadSelf( struct NineBaseTexture9 *This );
112
113HRESULT
114NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
115                                    const int sRGB );
116
117static inline void
118NineBaseTexture9_Validate( struct NineBaseTexture9 *This )
119{
120    DBG_FLAG(DBG_BASETEXTURE, "This=%p dirty=%i dirty_mip=%i lod=%u/%u\n",
121             This, This->managed.dirty, This->dirty_mip, This->managed.lod, This->managed.lod_resident);
122    if ((This->base.pool == D3DPOOL_MANAGED) &&
123        (This->managed.dirty || This->managed.lod != This->managed.lod_resident))
124        NineBaseTexture9_UploadSelf(This);
125    if (This->dirty_mip)
126        NineBaseTexture9_GenerateMipSubLevels(This);
127}
128
129static inline struct pipe_sampler_view *
130NineBaseTexture9_GetSamplerView( struct NineBaseTexture9 *This, const int sRGB )
131{
132    if (!This->view[sRGB])
133        NineBaseTexture9_UpdateSamplerView(This, sRGB);
134    return This->view[sRGB];
135}
136
137static void inline
138NineBindTextureToDevice( struct NineDevice9 *device,
139                         struct NineBaseTexture9 **slot,
140                         struct NineBaseTexture9 *tex )
141{
142    struct NineBaseTexture9 *old = *slot;
143
144    if (tex) {
145        if ((tex->managed.dirty | tex->dirty_mip) && list_is_empty(&tex->list))
146            list_add(&tex->list, &device->update_textures);
147
148        tex->bind_count++;
149    }
150    if (old) {
151        old->bind_count--;
152        if (!old->bind_count)
153            list_delinit(&old->list);
154    }
155
156    nine_bind(slot, tex);
157}
158
159#if defined(DEBUG) || !defined(NDEBUG)
160void
161NineBaseTexture9_Dump( struct NineBaseTexture9 *This );
162#else
163static inline void
164NineBaseTexture9_Dump( struct NineBaseTexture9 *This ) { }
165#endif
166
167#define BASETEX_REGISTER_UPDATE(t) do { \
168    if (((t)->managed.dirty | ((t)->dirty_mip)) && (t)->bind_count) \
169        if (list_is_empty(&(t)->list)) \
170            list_add(&(t)->list, &(t)->base.base.device->update_textures); \
171    } while(0)
172
173#endif /* _NINE_BASETEXTURE9_H_ */
174