| 489 | |
| 490 | |
| 491 | class Quad(Mesh): |
| 492 | # A shared texture for CUDA (pytorch) and OpenGL |
| 493 | # Could be rendererd to screen using blitting or just drawing a quad |
| 494 | def __init__(self, |
| 495 | H: int = 256, W: int = 256, |
| 496 | use_quad_draw: bool = True, |
| 497 | use_quad_cuda: bool = True, |
| 498 | compose: bool = False, |
| 499 | compose_power: float = 1.0, |
| 500 | ): # the texture to blip |
| 501 | self.use_quad_draw = use_quad_draw |
| 502 | self.use_quad_cuda = use_quad_cuda |
| 503 | self.vert_sizes = [3] # only position |
| 504 | self.vert_gl_types = [gl.GL_FLOAT] # only position |
| 505 | self.render_type = Mesh.RenderType.STRIPS # remove side effects of settings _type |
| 506 | self.max_verts, self.max_faces = 0, 0 |
| 507 | self.verts = torch.as_tensor([[-1., -1., 0.5], |
| 508 | [1., -1., 0.5], |
| 509 | [-1., 1., 0.5], |
| 510 | [1., 1., 0.5],]) |
| 511 | self.update_gl_buffers() |
| 512 | self.compile_shaders() |
| 513 | |
| 514 | self.max_H, self.max_W = H, W |
| 515 | self.H, self.W = H, W |
| 516 | self.compose = compose |
| 517 | self.compose_power = compose_power |
| 518 | |
| 519 | self.init_texture() |
| 520 | |
| 521 | @property |
| 522 | def n_faces_bytes(self): return 0 |
| 523 | |
| 524 | def use_gl_program(self, program: shaders.ShaderProgram): |
| 525 | super().use_gl_program(program) |
| 526 | self.uniforms.tex = gl.glGetUniformLocation(program, 'tex') |
| 527 | gl.glUseProgram(self.quad_program) # use a different program |
| 528 | gl.glUniform1i(self.uniforms.tex, 0) |
| 529 | |
| 530 | def compile_shaders(self): |
| 531 | try: |
| 532 | self.quad_program = shaders.compileProgram( |
| 533 | shaders.compileShader(load_shader_source('quad.vert'), gl.GL_VERTEX_SHADER), |
| 534 | shaders.compileShader(load_shader_source('quad.frag'), gl.GL_FRAGMENT_SHADER) |
| 535 | ) |
| 536 | except Exception as e: |
| 537 | print(str(e).encode('utf-8').decode('unicode_escape')) |
| 538 | raise e |
| 539 | |
| 540 | def resize_textures(self, H: int, W: int): # analogy to update_gl_buffers |
| 541 | self.H, self.W = H, W |
| 542 | if self.H > self.max_H or self.W > self.max_W: # max got updated |
| 543 | self.max_H, self.max_W = max(int(self.H * 1.05), self.max_H), max(int(self.W * 1.05), self.max_W) |
| 544 | self.init_texture() |
| 545 | |
| 546 | def init_texture(self): |
| 547 | if hasattr(self, 'cu_tex'): |
| 548 | from cuda import cudart |
no outgoing calls