2014-09-18 02:08:41 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WebGL2Context.h"
|
2014-12-05 10:04:55 +03:00
|
|
|
|
2014-09-18 02:08:41 +04:00
|
|
|
#include "GLContext.h"
|
2014-12-05 10:04:55 +03:00
|
|
|
#include "WebGLBuffer.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "WebGLTransformFeedback.h"
|
2014-09-18 02:08:41 +04:00
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
namespace mozilla {
|
2014-09-18 02:08:41 +04:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Buffer objects
|
|
|
|
|
|
|
|
void
|
2014-12-05 10:04:55 +03:00
|
|
|
WebGL2Context::CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
|
|
|
|
GLintptr readOffset, GLintptr writeOffset,
|
|
|
|
GLsizeiptr size)
|
2014-09-18 02:08:41 +04:00
|
|
|
{
|
2016-06-14 19:02:01 +03:00
|
|
|
const char funcName[] = "copyBufferSubData";
|
2014-12-05 10:04:55 +03:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto& readBuffer = ValidateBufferSelection(funcName, readTarget);
|
|
|
|
if (!readBuffer)
|
2014-12-05 10:04:55 +03:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto& writeBuffer = ValidateBufferSelection(funcName, writeTarget);
|
|
|
|
if (!writeBuffer)
|
2014-12-05 10:04:55 +03:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateNonNegative(funcName, "readOffset", readOffset) ||
|
|
|
|
!ValidateNonNegative(funcName, "writeOffset", writeOffset) ||
|
|
|
|
!ValidateNonNegative(funcName, "size", size))
|
|
|
|
{
|
2014-12-05 10:04:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto fnValidateOffsetSize = [&](const char* info, GLintptr offset,
|
|
|
|
const WebGLBuffer* buffer)
|
|
|
|
{
|
|
|
|
const auto neededBytes = CheckedInt<size_t>(offset) + size;
|
|
|
|
if (!neededBytes.isValid() || neededBytes.value() > buffer->ByteLength()) {
|
|
|
|
ErrorInvalidValue("%s: Invalid %s range.", funcName, info);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2016-06-14 19:02:01 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!fnValidateOffsetSize("read", readOffset, readBuffer) ||
|
|
|
|
!fnValidateOffsetSize("write", writeOffset, writeBuffer))
|
|
|
|
{
|
2014-12-05 10:04:55 +03:00
|
|
|
return;
|
2016-09-10 07:02:54 +03:00
|
|
|
}
|
2014-12-05 10:04:55 +03:00
|
|
|
|
2016-09-29 00:18:23 +03:00
|
|
|
if (readBuffer == writeBuffer) {
|
|
|
|
MOZ_ASSERT((CheckedInt<WebGLsizeiptr>(readOffset) + size).isValid());
|
|
|
|
MOZ_ASSERT((CheckedInt<WebGLsizeiptr>(writeOffset) + size).isValid());
|
|
|
|
|
2016-09-29 00:21:43 +03:00
|
|
|
const bool separate = (readOffset + size <= writeOffset ||
|
|
|
|
writeOffset + size <= readOffset);
|
2016-09-29 00:18:23 +03:00
|
|
|
if (!separate) {
|
|
|
|
ErrorInvalidValue("%s: ranges [readOffset, readOffset + size) and"
|
|
|
|
" [writeOffset, writeOffset + size) overlap",
|
|
|
|
funcName);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-05 10:04:55 +03:00
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto& readType = readBuffer->Content();
|
|
|
|
const auto& writeType = writeBuffer->Content();
|
|
|
|
MOZ_ASSERT(readType != WebGLBuffer::Kind::Undefined);
|
|
|
|
MOZ_ASSERT(writeType != WebGLBuffer::Kind::Undefined);
|
|
|
|
if (writeType != readType) {
|
2016-06-14 19:02:01 +03:00
|
|
|
ErrorInvalidOperation("%s: Can't copy %s data to %s data.",
|
|
|
|
funcName,
|
|
|
|
(readType == WebGLBuffer::Kind::OtherData) ? "other"
|
|
|
|
: "element",
|
|
|
|
(writeType == WebGLBuffer::Kind::OtherData) ? "other"
|
|
|
|
: "element");
|
2015-05-21 02:59:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
gl->MakeCurrent();
|
2016-09-26 23:38:56 +03:00
|
|
|
const ScopedLazyBind readBind(gl, readTarget, readBuffer);
|
|
|
|
const ScopedLazyBind writeBind(gl, writeTarget, writeBuffer);
|
2016-09-10 07:02:54 +03:00
|
|
|
gl->fCopyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size);
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-12 03:38:45 +03:00
|
|
|
WebGL2Context::GetBufferSubData(GLenum target, GLintptr srcByteOffset,
|
|
|
|
const dom::ArrayBufferView& dstData, GLuint dstElemOffset,
|
|
|
|
GLuint dstElemCountOverride)
|
2014-09-18 02:08:41 +04:00
|
|
|
{
|
2016-06-14 19:02:01 +03:00
|
|
|
const char funcName[] = "getBufferSubData";
|
2015-04-17 04:20:22 +03:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
2015-05-22 08:49:30 +03:00
|
|
|
|
2016-10-12 03:38:45 +03:00
|
|
|
if (!ValidateNonNegative(funcName, "srcByteOffset", srcByteOffset))
|
2015-04-17 04:20:22 +03:00
|
|
|
return;
|
2014-09-18 02:08:41 +04:00
|
|
|
|
2016-10-12 03:38:45 +03:00
|
|
|
uint8_t* bytes;
|
|
|
|
size_t byteLen;
|
|
|
|
if (!ValidateArrayBufferView(funcName, dstData, dstElemOffset, dstElemCountOverride,
|
|
|
|
&bytes, &byteLen))
|
|
|
|
{
|
2016-06-14 19:02:01 +03:00
|
|
|
return;
|
2016-10-12 03:38:45 +03:00
|
|
|
}
|
2015-04-17 04:20:22 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////
|
2015-05-22 08:49:30 +03:00
|
|
|
|
2016-10-12 03:38:45 +03:00
|
|
|
const auto& buffer = ValidateBufferSelection(funcName, target);
|
|
|
|
if (!buffer)
|
2015-04-17 04:20:22 +03:00
|
|
|
return;
|
|
|
|
|
2016-10-12 03:38:45 +03:00
|
|
|
if (!buffer->ValidateRange(funcName, srcByteOffset, byteLen))
|
2015-04-17 04:20:22 +03:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////
|
2015-04-17 04:20:22 +03:00
|
|
|
|
2016-10-12 03:38:45 +03:00
|
|
|
if (!CheckedInt<GLsizeiptr>(byteLen).isValid()) {
|
|
|
|
ErrorOutOfMemory("%s: Size too large.", funcName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const GLsizeiptr glByteLen(byteLen);
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////
|
|
|
|
|
|
|
|
gl->MakeCurrent();
|
2016-09-26 23:38:56 +03:00
|
|
|
const ScopedLazyBind readBind(gl, target, buffer);
|
2015-04-17 04:20:22 +03:00
|
|
|
|
2016-10-12 03:38:45 +03:00
|
|
|
if (byteLen) {
|
2016-12-29 11:08:39 +03:00
|
|
|
const bool isTF = (target == LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER);
|
|
|
|
GLenum mapTarget = target;
|
|
|
|
if (isTF) {
|
|
|
|
gl->fBindTransformFeedback(LOCAL_GL_TRANSFORM_FEEDBACK, mEmptyTFO);
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, buffer->mGLName);
|
|
|
|
mapTarget = LOCAL_GL_ARRAY_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto mappedBytes = gl->fMapBufferRange(mapTarget, srcByteOffset, glByteLen,
|
2016-12-20 05:04:37 +03:00
|
|
|
LOCAL_GL_MAP_READ_BIT);
|
2016-10-12 03:38:45 +03:00
|
|
|
memcpy(bytes, mappedBytes, byteLen);
|
2016-12-29 11:08:39 +03:00
|
|
|
gl->fUnmapBuffer(mapTarget);
|
|
|
|
|
|
|
|
if (isTF) {
|
|
|
|
const GLuint vbo = (mBoundArrayBuffer ? mBoundArrayBuffer->mGLName : 0);
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, vbo);
|
|
|
|
const GLuint tfo = (mBoundTransformFeedback ? mBoundTransformFeedback->mGLName
|
|
|
|
: 0);
|
|
|
|
gl->fBindTransformFeedback(LOCAL_GL_TRANSFORM_FEEDBACK, tfo);
|
|
|
|
}
|
2016-10-12 03:38:45 +03:00
|
|
|
}
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
2015-07-15 03:37:28 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|