File: Texture.cpp

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (275 lines) | stat: -rw-r--r-- 8,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275


/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* Copyright (c) Schrodinger, LLC. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* --------------------------------------------------\-----------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/

#include <memory>
#include <unordered_set>

#include"os_python.h"
#include"os_gl.h"

#include "Base.h"
#include "PyMOLGlobals.h"
#include "Texture.h"

#include "Setting.h"
#include "Character.h"
#include "Util.h"
#include "Feedback.h"

#include "Executive.h"
#include "Ortho.h"
#include "ShaderMgr.h"
#include "GenericBuffer.h"

#define POS_START  2

/**
 * Texture unit assigned to the global text texture.
 * See ShaderMgr.cpp for more details.
 */
constexpr std::uint8_t GlobalTextTextureUnit = 3;

struct CTexture {
  std::unordered_set<int> texturedCharIDs; // set of char ids that have been textured
  std::unique_ptr<textureBuffer_t> texture;
  int xpos{};
  int ypos{};
  int maxypos{};
  int text_texture_dim{};
};

void TextureBindTexture(PyMOLGlobals* G) {
  auto I = G->Texture;
  if (I->texture) {
    I->texture->bind();
  }
}

int TextureGetTextTextureSize(PyMOLGlobals * G){
  CTexture *I = G->Texture;
  return I->text_texture_dim;
}

#define INIT_TEXTURE_SIZE 512

int TextureInit(PyMOLGlobals * G)
{
  auto I = new CTexture();
  G->Texture = I;

  I->text_texture_dim = INIT_TEXTURE_SIZE;
  I->ypos = I->maxypos = 0;
  I->xpos = POS_START;
  return (I ? 1 : 0);
}

static
void TextureInitTextTextureImpl(PyMOLGlobals *G, int textureSize);

void TextureInitTextTexture(PyMOLGlobals *G){
  TextureInitTextTextureImpl(G, INIT_TEXTURE_SIZE);
}
void TextureInvalidateTextTexture(PyMOLGlobals * G){
  CTexture *I = G->Texture;
  if (I->texture) {
    I->texturedCharIDs.clear();
    I->texture.reset();
    I->text_texture_dim = INIT_TEXTURE_SIZE;
    I->xpos = POS_START; I->ypos = 0; I->maxypos = POS_START;
  }
}

void TextureInitTextTextureImpl(PyMOLGlobals *G, int textureSizeArg){
  short is_new = 0;
  CTexture *I = G->Texture;
  int textureSize = textureSizeArg;
  if (!textureSize)
    textureSize = INIT_TEXTURE_SIZE;
  if (!I->texture) {
    using namespace tex;
    I->texture =
        std::make_unique<textureBuffer_t>(format::RGBA, data_type::UBYTE,
            filter::NEAREST, filter::NEAREST, wrap::CLAMP, wrap::CLAMP);
    is_new = true;
  }
  if (I->texture) {
    if (is_new) {
      int tex_dim = textureSize;
      int buff_total = tex_dim * tex_dim;
      std::vector<unsigned char> temp_buffer(
          buff_total * GetSizeOfVertexFormat(VertexFormat::UByte4), 0);
      I->texture->bindToTextureUnit(GlobalTextTextureUnit);
      I->texture->texture_data_2D(tex_dim, tex_dim, temp_buffer.data());
      I->text_texture_dim = textureSize;
      I->xpos = POS_START; I->ypos = 0; I->maxypos = POS_START;
    }
  }
}

#include "Rep.h"
bool TextureIsCharTextured(PyMOLGlobals* G, int char_id, float* extent)
{
  CTexture *I = G->Texture;
  int is_new = false;
  int tex_dim = I->text_texture_dim;
  short use_shader = (short) SettingGetGlobal_b(G, cSetting_use_shaders);

  if(G->HaveGUI && G->ValidContext) {
    auto it = I->texturedCharIDs.find(char_id);
    if (it != I->texturedCharIDs.end()) {
      if (I->texture) {
        return true;
      }
      else {
        I->texturedCharIDs.erase(char_id);
      }
    }
    {
      unsigned char *buffer = nullptr;
      if (!I->texture) {
          is_new = true;
      }
      buffer = CharacterGetPixmapBuffer(G, char_id);
      if(buffer) {
        int w = CharacterGetWidth(G, char_id);
        int h = CharacterGetHeight(G, char_id);
        int buff_incr = is_new ? tex_dim : w;
        int buff_total = is_new ? tex_dim * tex_dim : w * h;
        std::vector<unsigned char> temp_buffer(
            buff_total * GetSizeOfVertexFormat(VertexFormat::UByte4), 0);

        {
          int a, b;
          unsigned char *p = buffer, *q;
	  int fa = 0, ta = w;
	  if (is_new){
	    fa += I->xpos; ta += I->xpos;
	  }
          for(b = 0; b < h; b++) {
	    for(a = fa; a < ta; a++) {
              q = temp_buffer.data() + (4 * buff_incr * b) + 4 * a;
              *(q++) = *(p++);
              *(q++) = *(p++);
              *(q++) = *(p++);
              *(q++) = *(p++);
            }
          }
	  if (I->xpos + w > tex_dim){
	    // if the size of the texture goes off the side, go to next row
	    I->xpos = 0;
	    I->ypos = I->maxypos;
	  }
	  if ((I->ypos + h) >= I->text_texture_dim){ // only need to check y since x gets reset above
	    int nrefreshes;
	    I->xpos = POS_START; I->ypos = 0; I->maxypos = POS_START;
	    I->texturedCharIDs.clear();
	    /* Also need to reload the selection markers into the texture, since
	       we are wiping everything out from the texture and starting from the origin */
	    if ((nrefreshes=SceneIncrementTextureRefreshes(G)) > 1){
	      /* Texture was refreshed more than once for this frame, increase size of texture */
	      int newDim = I->text_texture_dim * 2;
	      I->texture.reset();
	      TextureInitTextTextureImpl(G, newDim);
	      PRINTFB(G, FB_OpenGL, FB_Output)
		" Texture OpenGL: nrefreshes=%d newDim=%d\n", nrefreshes, newDim ENDFB(G);

	      //	      printf("nrefreshes=%d newDim=%d\n", nrefreshes, newDim);
	      I->xpos = POS_START; I->ypos = 0; I->maxypos = POS_START;
	      SceneResetTextureRefreshes(G);
	    }
	    ExecutiveInvalidateRep(G, "all", cRepLabel, cRepInvRep);
	    ExecutiveInvalidateSelectionIndicators(G);
	    OrthoInvalidateDoDraw(G);
	    return false;
	  }
          extent[0] = (I->xpos / (float) tex_dim);
          extent[1] = (I->ypos / (float) tex_dim);
          extent[2] = ((I->xpos + w) / (float) tex_dim);
          extent[3] = ((I->ypos + h) / (float) tex_dim);
        }

        if (!I->texture) {
          using namespace tex;
          I->texture =
              std::make_unique<textureBuffer_t>(format::RGBA, data_type::UBYTE,
                  filter::NEAREST, filter::NEAREST, wrap::CLAMP, wrap::CLAMP);
        }
	if (I->texture) {
	  I->texturedCharIDs.emplace(char_id);
          if (is_new) {
	    I->text_texture_dim = tex_dim;
        I->texture->bindToTextureUnit(GlobalTextTextureUnit);
        I->texture->texture_data_2D(tex_dim, tex_dim, temp_buffer.data());
          } else {
	    int xoff = 0, yoff = 0;
	    xoff = I->xpos;
	    yoff = I->ypos;
        I->texture->texture_subdata_2D(xoff, yoff, w, h, temp_buffer.data());
#ifdef _WEBGL
              static bool error_flag = false;
              if ((glGetError()) != 0 && error_flag) {
                PRINTFB(G, FB_OpenGL, FB_Output)
                  " IE11 texSubImage2D bug detected, supressing...\n" ENDFB(G);
                error_flag = true;
              }
#endif
          }
        }
	if (I->ypos + h > I->maxypos){
	  I->maxypos = I->ypos + h + 1;  // added space for running on Ipad/Iphone (weird artifacts)
	}
	if (I->xpos + w > tex_dim){
	  I->xpos = 0;
	  I->ypos = I->maxypos;
	} else {
	  I->xpos += w + 1; // added space for running on Ipad/Iphone (weird artifacts)
	}
        return I->texture != nullptr;
      }
    }
  }
  return true;
}

void TextureGetPlacementForNewSubtexture(PyMOLGlobals * G, int new_texture_width, int new_texture_height, int *new_texture_posx, int *new_texture_posy){
  CTexture *I = G->Texture;
  if (I->xpos + new_texture_width > I->text_texture_dim){
    I->xpos = 0;
    I->ypos = I->maxypos;
  }
  if (I->ypos + new_texture_height > I->maxypos){
    I->maxypos = I->ypos + new_texture_height + 1;  // added space for running on Ipad/Iphone (weird artifacts)
  }
  *new_texture_posx = I->xpos;
  *new_texture_posy = I->ypos;
  I->xpos += new_texture_width + 1; // added space for running on Ipad/Iphone (weird artifacts)
}

void TextureFillNewSubtexture(PyMOLGlobals* G, int width, int height, int x, int y, const void* data)
{
  CTexture *I = G->Texture;
  if (I->texture) {
    I->texture->texture_subdata_2D(x, y, width, height, data);
  }
}

void TextureFree(PyMOLGlobals * G)
{
  /* TODO -- free all the resident textures */
  DeleteP(G->Texture);
}