github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/exp/shiny/driver/gldriver/texture.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gldriver
     6  
     7  import (
     8  	"encoding/binary"
     9  	"image"
    10  	"image/color"
    11  	"image/draw"
    12  
    13  	"golang.org/x/exp/shiny/screen"
    14  	"golang.org/x/mobile/gl"
    15  )
    16  
    17  type textureImpl struct {
    18  	w    *windowImpl
    19  	id   gl.Texture
    20  	size image.Point
    21  }
    22  
    23  func (t *textureImpl) Size() image.Point       { return t.size }
    24  func (t *textureImpl) Bounds() image.Rectangle { return image.Rectangle{Max: t.size} }
    25  
    26  func (t *textureImpl) Release() {
    27  	t.w.glctxMu.Lock()
    28  	defer t.w.glctxMu.Unlock()
    29  
    30  	t.w.glctx.DeleteTexture(t.id)
    31  	t.id = gl.Texture{}
    32  }
    33  
    34  func (t *textureImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) {
    35  	t.w.glctxMu.Lock()
    36  	defer t.w.glctxMu.Unlock()
    37  
    38  	// TODO: adjust if dp is outside dst bounds, or r is outside src bounds.
    39  	t.w.glctx.BindTexture(gl.TEXTURE_2D, t.id)
    40  	m := src.RGBA().SubImage(sr).(*image.RGBA)
    41  	b := m.Bounds()
    42  	// TODO check m bounds smaller than t.size
    43  	t.w.glctx.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, b.Dx(), b.Dy(), gl.RGBA, gl.UNSIGNED_BYTE, m.Pix)
    44  }
    45  
    46  func (t *textureImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) {
    47  	t.w.glctxMu.Lock()
    48  	defer t.w.glctxMu.Unlock()
    49  
    50  	// TODO.
    51  }
    52  
    53  var quadCoords = f32Bytes(binary.LittleEndian,
    54  	0, 0, // top left
    55  	1, 0, // top right
    56  	0, 1, // bottom left
    57  	1, 1, // bottom right
    58  )
    59  
    60  const textureVertexSrc = `#version 100
    61  uniform mat3 mvp;
    62  uniform mat3 uvp;
    63  attribute vec3 pos;
    64  attribute vec2 inUV;
    65  varying vec2 uv;
    66  void main() {
    67  	vec3 p = pos;
    68  	p.z = 1.0;
    69  	gl_Position = vec4(mvp * p, 1);
    70  	uv = (uvp * vec3(inUV, 1)).xy;
    71  }
    72  `
    73  
    74  const textureFragmentSrc = `#version 100
    75  precision mediump float;
    76  varying vec2 uv;
    77  uniform sampler2D sample;
    78  void main() {
    79  	gl_FragColor = texture2D(sample, uv);
    80  }
    81  `
    82  
    83  const fillVertexSrc = `#version 100
    84  uniform mat3 mvp;
    85  attribute vec3 pos;
    86  void main() {
    87  	vec3 p = pos;
    88  	p.z = 1.0;
    89  	gl_Position = vec4(mvp * p, 1);
    90  }
    91  `
    92  
    93  const fillFragmentSrc = `#version 100
    94  precision mediump float;
    95  uniform vec4 color;
    96  void main() {
    97  	gl_FragColor = color;
    98  }
    99  `