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    uint8_t pstype; /* 0: 2D, 1: 1D, 2: CUBE, 3: 3D */
46
47    boolean dirty_mip;
48    D3DTEXTUREFILTERTYPE mipfilter;
49
50    /* Specific to managed textures */
51    struct {
52        boolean dirty;
53        DWORD lod;
54        DWORD lod_resident;
55    } managed;
56};
57static inline struct NineBaseTexture9 *
58NineBaseTexture9( void *data )
59{
60    return (struct NineBaseTexture9 *)data;
61}
62
63HRESULT
64NineBaseTexture9_ctor( struct NineBaseTexture9 *This,
65                       struct NineUnknownParams *pParams,
66                       struct pipe_resource *initResource,
67                       D3DRESOURCETYPE Type,
68                       D3DFORMAT format,
69                       D3DPOOL Pool,
70                       DWORD Usage);
71
72void
73NineBaseTexture9_dtor( struct NineBaseTexture9 *This );
74
75DWORD NINE_WINAPI
76NineBaseTexture9_SetLOD( struct NineBaseTexture9 *This,
77                         DWORD LODNew );
78
79DWORD NINE_WINAPI
80NineBaseTexture9_GetLOD( struct NineBaseTexture9 *This );
81
82DWORD NINE_WINAPI
83NineBaseTexture9_GetLevelCount( struct NineBaseTexture9 *This );
84
85HRESULT NINE_WINAPI
86NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9 *This,
87                                       D3DTEXTUREFILTERTYPE FilterType );
88
89D3DTEXTUREFILTERTYPE NINE_WINAPI
90NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9 *This );
91
92void NINE_WINAPI
93NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This );
94
95void NINE_WINAPI
96NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This );
97
98void
99NineBaseTexture9_UnLoad( struct NineBaseTexture9 *This );
100
101/* For D3DPOOL_MANAGED only (after SetLOD change): */
102HRESULT
103NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
104                                     BOOL CopyData );
105
106/* For D3DPOOL_MANAGED only: */
107HRESULT
108NineBaseTexture9_UploadSelf( struct NineBaseTexture9 *This );
109
110HRESULT
111NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
112                                    const int sRGB );
113
114static inline void
115NineBaseTexture9_Validate( struct NineBaseTexture9 *This )
116{
117    DBG_FLAG(DBG_BASETEXTURE, "This=%p dirty=%i dirty_mip=%i lod=%u/%u\n",
118             This, This->managed.dirty, This->dirty_mip, This->managed.lod, This->managed.lod_resident);
119    if ((This->base.pool == D3DPOOL_MANAGED) &&
120        (This->managed.dirty || This->managed.lod != This->managed.lod_resident))
121        NineBaseTexture9_UploadSelf(This);
122    if (This->dirty_mip)
123        NineBaseTexture9_GenerateMipSubLevels(This);
124}
125
126static inline struct pipe_sampler_view *
127NineBaseTexture9_GetSamplerView( struct NineBaseTexture9 *This, const int sRGB )
128{
129    if (!This->view[sRGB])
130        NineBaseTexture9_UpdateSamplerView(This, sRGB);
131    return This->view[sRGB];
132}
133
134static void inline
135NineBindTextureToDevice( struct NineDevice9 *device,
136                         struct NineBaseTexture9 **slot,
137                         struct NineBaseTexture9 *tex )
138{
139    struct NineBaseTexture9 *old = *slot;
140
141    if (tex) {
142        if ((tex->managed.dirty | tex->dirty_mip) && LIST_IS_EMPTY(&tex->list))
143            list_add(&tex->list, &device->update_textures);
144
145        tex->bind_count++;
146    }
147    if (old)
148        old->bind_count--;
149
150    nine_bind(slot, tex);
151}
152
153#ifdef DEBUG
154void
155NineBaseTexture9_Dump( struct NineBaseTexture9 *This );
156#else
157static inline void
158NineBaseTexture9_Dump( struct NineBaseTexture9 *This ) { }
159#endif
160
161#define BASETEX_REGISTER_UPDATE(t) do { \
162    if (((t)->managed.dirty | ((t)->dirty_mip)) && (t)->bind_count) \
163        if (LIST_IS_EMPTY(&(t)->list)) \
164            list_add(&(t)->list, &(t)->base.base.device->update_textures); \
165    } while(0)
166
167#endif /* _NINE_BASETEXTURE9_H_ */
168