Commit | Line | Data |
---|---|---|
bf5f0ca2 | 1 | // Copyright (c) 2019 OPEN CASCADE SAS |
b311480e | 2 | // |
973c2be1 | 3 | // This file is part of Open CASCADE Technology software library. |
b311480e | 4 | // |
d5f74e42 | 5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
9 | // distribution for complete text of the license and disclaimer of any warranty. | |
b311480e | 10 | // |
973c2be1 | 11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. | |
b311480e | 13 | |
bf5f0ca2 | 14 | #include <OpenGl_AspectsTextureSet.hxx> |
30f0ad28 | 15 | |
30f0ad28 | 16 | #include <OpenGl_Context.hxx> |
cc8cbabe | 17 | #include <OpenGl_Sampler.hxx> |
bf75be98 | 18 | #include <OpenGl_Texture.hxx> |
bf5f0ca2 | 19 | #include <OpenGl_TextureSet.hxx> |
2166f0fa | 20 | |
c04c30b3 | 21 | #include <Graphic3d_TextureParams.hxx> |
4269bd1b | 22 | |
c04c30b3 | 23 | #include <Image_PixMap.hxx> |
24 | ||
bf75be98 | 25 | namespace |
2166f0fa | 26 | { |
bf5f0ca2 | 27 | static const TCollection_AsciiString THE_EMPTY_KEY; |
2166f0fa SK |
28 | } |
29 | ||
bf75be98 | 30 | // ======================================================================= |
31 | // function : Release | |
32 | // purpose : | |
33 | // ======================================================================= | |
bf5f0ca2 | 34 | void OpenGl_AspectsTextureSet::Release (OpenGl_Context* theCtx) |
fd4a6963 | 35 | { |
cc8cbabe | 36 | if (myTextures.IsNull()) |
37 | { | |
38 | return; | |
39 | } | |
40 | ||
41 | for (OpenGl_TextureSet::Iterator aTextureIter (myTextures); aTextureIter.More(); aTextureIter.Next()) | |
fd4a6963 | 42 | { |
cc8cbabe | 43 | Handle(OpenGl_Texture)& aTextureRes = aTextureIter.ChangeValue(); |
44 | if (aTextureRes.IsNull()) | |
d2edda76 | 45 | { |
cc8cbabe | 46 | continue; |
47 | } | |
48 | ||
49 | if (theCtx != NULL) | |
50 | { | |
51 | if (aTextureRes->ResourceId().IsEmpty()) | |
d2edda76 | 52 | { |
cc8cbabe | 53 | theCtx->DelayedRelease (aTextureRes); |
d2edda76 | 54 | } |
cc8cbabe | 55 | else |
56 | { | |
57 | const TCollection_AsciiString aName = aTextureRes->ResourceId(); | |
58 | aTextureRes.Nullify(); // we need nullify all handles before ReleaseResource() call | |
59 | theCtx->ReleaseResource (aName, Standard_True); | |
60 | } | |
61 | } | |
62 | aTextureRes.Nullify(); | |
63 | } | |
64 | myIsTextureReady = Standard_False; | |
65 | } | |
66 | ||
67 | // ======================================================================= | |
bf5f0ca2 | 68 | // function : UpdateRediness |
cc8cbabe | 69 | // purpose : |
70 | // ======================================================================= | |
bf5f0ca2 | 71 | void OpenGl_AspectsTextureSet::UpdateRediness (const Handle(Graphic3d_TextureSet)& theTextures) |
cc8cbabe | 72 | { |
73 | const Standard_Integer aNbTexturesOld = !myTextures.IsNull() ? myTextures->Size() : 0; | |
74 | const Standard_Integer aNbTexturesNew = !theTextures.IsNull() ? theTextures->Size() : 0; | |
75 | if (aNbTexturesOld != aNbTexturesNew) | |
76 | { | |
77 | myIsTextureReady = Standard_False; | |
78 | return; | |
79 | } | |
80 | if (aNbTexturesOld == 0) | |
81 | { | |
82 | return; | |
83 | } | |
84 | ||
85 | Graphic3d_TextureSet::Iterator aTextureIter (theTextures); | |
86 | OpenGl_TextureSet::Iterator aResIter (myTextures); | |
87 | for (; aResIter.More(); aResIter.Next(), aTextureIter.Next()) | |
88 | { | |
89 | const Handle(OpenGl_Texture)& aResource = aResIter.Value(); | |
90 | const Handle(Graphic3d_TextureMap)& aTexture = aTextureIter.Value(); | |
91 | if (aTexture.IsNull() != aResource.IsNull()) | |
92 | { | |
93 | myIsTextureReady = Standard_False; | |
94 | return; | |
95 | } | |
96 | else if (aTexture.IsNull()) | |
97 | { | |
98 | continue; | |
d2edda76 | 99 | } |
100 | ||
cc8cbabe | 101 | const TCollection_AsciiString& aTextureKey = aTexture->GetId(); |
102 | if (aTextureKey.IsEmpty() || aResource->ResourceId() != aTextureKey) | |
103 | { | |
104 | myIsTextureReady = Standard_False; | |
105 | return; | |
106 | } | |
107 | else if (aResource->Revision() != aTexture->Revision()) | |
30f0ad28 | 108 | { |
cc8cbabe | 109 | myIsTextureReady = Standard_False; |
110 | return; | |
30f0ad28 | 111 | } |
112 | else | |
fd4a6963 | 113 | { |
cc8cbabe | 114 | // just invalidate texture parameters |
115 | aResource->Sampler()->SetParameters (aTexture->GetParams()); | |
30f0ad28 | 116 | } |
117 | } | |
cc8cbabe | 118 | } |
30f0ad28 | 119 | |
cc8cbabe | 120 | // ======================================================================= |
bf5f0ca2 | 121 | // function : build |
cc8cbabe | 122 | // purpose : |
123 | // ======================================================================= | |
bf5f0ca2 | 124 | void OpenGl_AspectsTextureSet::build (const Handle(OpenGl_Context)& theCtx, |
125 | const Handle(Graphic3d_TextureSet)& theTextures) | |
cc8cbabe | 126 | { |
127 | // release old texture resources | |
128 | const Standard_Integer aNbTexturesOld = !myTextures.IsNull() ? myTextures->Size() : 0; | |
129 | const Standard_Integer aNbTexturesNew = !theTextures.IsNull() ? theTextures->Size() : 0; | |
130 | if (aNbTexturesOld != aNbTexturesNew) | |
131 | { | |
bf5f0ca2 | 132 | Release (theCtx.get()); |
cc8cbabe | 133 | if (aNbTexturesNew > 0) |
134 | { | |
135 | myTextures = new OpenGl_TextureSet (theTextures->Size()); | |
136 | } | |
137 | else | |
138 | { | |
139 | myTextures.Nullify(); | |
140 | } | |
141 | } | |
142 | if (myTextures.IsNull()) | |
143 | { | |
144 | return; | |
145 | } | |
30f0ad28 | 146 | |
cc8cbabe | 147 | Graphic3d_TextureSet::Iterator aTextureIter (theTextures); |
148 | OpenGl_TextureSet::Iterator aResIter (myTextures); | |
149 | for (; aResIter.More(); aResIter.Next(), aTextureIter.Next()) | |
30f0ad28 | 150 | { |
cc8cbabe | 151 | Handle(OpenGl_Texture)& aResource = aResIter.ChangeValue(); |
152 | const Handle(Graphic3d_TextureMap)& aTexture = aTextureIter.Value(); | |
153 | if (!aResource.IsNull()) | |
154 | { | |
155 | if (!aTexture.IsNull() | |
156 | && aTexture->GetId() == aResource->ResourceId() | |
157 | && aTexture->Revision() != aResource->Revision()) | |
158 | { | |
159 | if (Handle(Image_PixMap) anImage = aTexture->GetImage()) | |
160 | { | |
161 | aResource->Sampler()->SetParameters (aTexture->GetParams()); | |
162 | aResource->Init (theCtx, *anImage.operator->(), aTexture->Type()); | |
163 | aResource->SetRevision (aTexture->Revision()); | |
164 | continue; | |
165 | } | |
166 | } | |
167 | ||
168 | if (aResource->ResourceId().IsEmpty()) | |
169 | { | |
170 | theCtx->DelayedRelease (aResource); | |
171 | aResource.Nullify(); | |
172 | } | |
173 | else | |
174 | { | |
175 | const TCollection_AsciiString aTextureKey = aResource->ResourceId(); | |
176 | aResource.Nullify(); // we need nullify all handles before ReleaseResource() call | |
177 | theCtx->ReleaseResource (aTextureKey, Standard_True); | |
178 | } | |
179 | } | |
180 | ||
181 | if (!aTexture.IsNull()) | |
30f0ad28 | 182 | { |
cc8cbabe | 183 | const TCollection_AsciiString& aTextureKeyNew = aTexture->GetId(); |
184 | if (aTextureKeyNew.IsEmpty() | |
185 | || !theCtx->GetResource<Handle(OpenGl_Texture)> (aTextureKeyNew, aResource)) | |
fd4a6963 | 186 | { |
cc8cbabe | 187 | aResource = new OpenGl_Texture (aTextureKeyNew, aTexture->GetParams()); |
188 | if (Handle(Image_PixMap) anImage = aTexture->GetImage()) | |
189 | { | |
190 | aResource->Init (theCtx, *anImage.operator->(), aTexture->Type()); | |
191 | aResource->SetRevision (aTexture->Revision()); | |
192 | } | |
193 | if (!aTextureKeyNew.IsEmpty()) | |
194 | { | |
195 | theCtx->ShareResource (aTextureKeyNew, aResource); | |
196 | } | |
fd4a6963 | 197 | } |
cc8cbabe | 198 | else |
fd4a6963 | 199 | { |
31fe1f8e | 200 | if (aTexture->Revision() != aResource->Revision()) |
201 | { | |
202 | if (Handle(Image_PixMap) anImage = aTexture->GetImage()) | |
203 | { | |
204 | aResource->Init (theCtx, *anImage.operator->(), aTexture->Type()); | |
205 | aResource->SetRevision (aTexture->Revision()); | |
206 | } | |
207 | } | |
cc8cbabe | 208 | aResource->Sampler()->SetParameters (aTexture->GetParams()); |
fd4a6963 | 209 | } |
210 | } | |
30f0ad28 | 211 | } |
212 | } |