2019-11-16 18:10:10 +01:00
|
|
|
// Copyright 2019 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build darwin freebsd linux windows
|
|
|
|
// +build !js
|
|
|
|
// +build !android
|
|
|
|
// +build !ios
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
const canUsePBO = true
|
|
|
|
|
|
|
|
type pboState struct {
|
|
|
|
image *Image
|
2019-11-19 17:59:56 +01:00
|
|
|
mappedPBO uintptr
|
2019-11-20 04:23:45 +01:00
|
|
|
mapped []byte
|
2019-11-16 18:10:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var thePBOState pboState
|
|
|
|
|
2019-11-19 18:41:31 +01:00
|
|
|
func (s *pboState) mapPBO(img *Image) {
|
2019-11-24 10:32:35 +01:00
|
|
|
w, h := img.width, img.height
|
2019-11-16 18:10:10 +01:00
|
|
|
if img.pbo == *new(buffer) {
|
|
|
|
img.pbo = img.driver.context.newPixelBufferObject(w, h)
|
|
|
|
}
|
|
|
|
s.image = img
|
2019-11-23 16:08:38 +01:00
|
|
|
s.mappedPBO = img.driver.context.mapPixelBuffer(img.pbo, img.textureNative)
|
2019-11-16 18:10:10 +01:00
|
|
|
|
2019-11-19 17:59:56 +01:00
|
|
|
if s.mappedPBO == 0 {
|
2019-11-16 18:10:10 +01:00
|
|
|
panic("opengl: mapPixelBuffer failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapped []byte
|
|
|
|
sh := (*reflect.SliceHeader)(unsafe.Pointer(&mapped))
|
2019-11-19 17:59:56 +01:00
|
|
|
sh.Data = s.mappedPBO
|
2019-11-16 18:10:10 +01:00
|
|
|
sh.Len = 4 * w * h
|
|
|
|
sh.Cap = 4 * w * h
|
2019-11-20 04:23:45 +01:00
|
|
|
s.mapped = mapped
|
|
|
|
}
|
2019-11-16 18:10:10 +01:00
|
|
|
|
2019-11-20 04:23:45 +01:00
|
|
|
func (s *pboState) draw(pix []byte, x, y, width, height int) {
|
2019-11-24 10:32:35 +01:00
|
|
|
w := s.image.width
|
2019-11-16 18:10:10 +01:00
|
|
|
stride := 4 * w
|
|
|
|
offset := 4 * (y*w + x)
|
|
|
|
for j := 0; j < height; j++ {
|
2019-11-20 04:23:45 +01:00
|
|
|
copy(s.mapped[offset+stride*j:offset+stride*j+4*width], pix[4*width*j:4*width*(j+1)])
|
2019-11-16 18:10:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:41:31 +01:00
|
|
|
func (s *pboState) unmapPBO() {
|
2019-11-16 18:10:10 +01:00
|
|
|
i := s.image
|
2019-11-24 10:32:35 +01:00
|
|
|
i.driver.context.unmapPixelBuffer(i.pbo, i.width, i.height)
|
2019-11-16 18:10:10 +01:00
|
|
|
|
|
|
|
s.image = nil
|
2019-11-19 17:59:56 +01:00
|
|
|
s.mappedPBO = 0
|
2019-11-20 04:23:45 +01:00
|
|
|
s.mapped = nil
|
2019-11-16 18:10:10 +01:00
|
|
|
}
|