Skip to content

Commit 3521c41

Browse files
authored
Merge pull request #424 from YoYoGames/gmb-2754-vertex-submit-ext
GMB-2754: Add new function vertex_submit_ext
2 parents 4092acd + 3bc118f commit 3521c41

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

scripts/Builders/yyPrimBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// @if feature("gl") && (function("draw_primitive_*") || function("draw_vertex*") || function("vertex_*"))
1414

15-
// primitive type translation is shared between draw_primitive_* and vertex_submit
15+
// primitive type translation is shared between draw_primitive_* and vertex_submit(_ext)
1616
var PrimType_POINTLIST = 1,
1717
PrimType_LINELIST = 2,
1818
PrimType_LINESTRIP = 3,

scripts/Builders/yyVBufferBuilder.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,31 @@ function yyVBufferBuilder(_size) {
355355
/// </summary>
356356
// #############################################################################################
357357
/** @this {yyVBufferBuilder} */
358-
this.Submit = function(_primType, _texture) {
358+
this.Submit = function(_primType, _texture, _offset, _number) {
359+
if (_offset === undefined) _offset = 0;
360+
if (_number === undefined) _number = -1;
361+
362+
if (_offset < 0) {
363+
yyError("vertex_submit_ext: offset cannot be a negative number!");
364+
return;
365+
}
366+
367+
var vertexCount = (_number < 0) ? m_vertexCount : _number;
368+
if (_offset + vertexCount > m_vertexCount) {
369+
vertexCount = m_vertexCount - _offset;
370+
}
371+
372+
if (vertexCount <= 0) {
373+
return;
374+
}
375+
359376
if (m_frozen) {
360377

361378
// Directly submit the vertex buffer
362379
// (I don't believe a flush is necessary as it shouldn't affect
363380
// the building up of the current in-flight vertex buffer)
364381
if (_texture == -1) {
365-
g_webGL.DispatchVBuffer(_primType,null, m_VBuffer, 0);
382+
g_webGL.DispatchVBuffer(_primType,null, m_VBuffer, _offset, vertexCount);
366383
}
367384
else {
368385
// Check whether the webgl texture has been initialised yet and do so if not
@@ -373,13 +390,13 @@ function yyVBufferBuilder(_size) {
373390
return;
374391
}
375392
}
376-
g_webGL.DispatchVBuffer(_primType, _texture.WebGLTexture.webgl_textureid, m_VBuffer, 0);
393+
g_webGL.DispatchVBuffer(_primType, _texture.WebGLTexture.webgl_textureid, m_VBuffer, _offset, vertexCount);
377394
}
378395
}
379396
else {
380397
var pBuff;
381398
if (_texture == -1) {
382-
pBuff = g_webGL.AllocVerts(_primType, null, m_FVF, m_vertexCount);
399+
pBuff = g_webGL.AllocVerts(_primType, null, m_FVF, vertexCount);
383400
} else {
384401
// Check whether the webgl texture has been initialised yet and do so if not
385402
if (_texture && !_texture.WebGLTexture.webgl_textureid) {
@@ -389,18 +406,18 @@ function yyVBufferBuilder(_size) {
389406
return;
390407
}
391408
}
392-
pBuff = g_webGL.AllocVerts(_primType, _texture.WebGLTexture.webgl_textureid, m_FVF, m_vertexCount);
409+
pBuff = g_webGL.AllocVerts(_primType, _texture.WebGLTexture.webgl_textureid, m_FVF, vertexCount);
393410
}
394411

395412
// Get the data across
396413
var currpos = pBuff.Current * m_vertexFormat.ByteSize;
397414

398415
// Open a data view onto the vertex array to allow for easy copying to the VBuffer vertex data store
399-
var vertexDataView = new Int8Array(m_arrayBuffer, 0, m_vertexCount * m_vertexFormat.ByteSize);
416+
var vertexDataView = new Int8Array(m_arrayBuffer, _offset * m_vertexFormat.ByteSize, vertexCount * m_vertexFormat.ByteSize);
400417

401418
// This buffer copy might seem a bit unnecessary but it's there so that the vertex buffer can be reused
402419
pBuff.VertexData.set(vertexDataView, currpos);
403-
pBuff.Current += m_vertexCount;
420+
pBuff.Current += vertexCount;
404421
}
405422
};
406423

scripts/libWebGL/libWebGL.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,10 +2103,10 @@ function yyWebGL(_canvas, _options) {
21032103
/// </summary>
21042104
// #############################################################################################
21052105
/** @this {yyWebGL} */
2106-
this.DispatchVBuffer = function (_prim, _texture, _vbuffer, _vertexStart) {
2106+
this.DispatchVBuffer = function (_prim, _texture, _vbuffer, _vertexStart, _vertexCount) {
21072107

21082108
yyGL.REQUIRE((_texture == null) || (_texture instanceof yyGLTexture), "Texture is not a yyGLTexture", yyGL.ERRORLEVEL_Development);
2109-
m_VBufferManager.Dispatch(_prim, _texture, _vbuffer, _vertexStart);
2109+
m_VBufferManager.Dispatch(_prim, _texture, _vbuffer, _vertexStart, _vertexCount);
21102110
};
21112111

21122112

scripts/libWebGL/yyVBufferManager.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ function yyVBufferManager(_commandBuilder,_renderStateManager) {
5151
/// </summary>
5252
// #############################################################################################
5353
/** @this {yyVBufferManager} */
54-
this.Dispatch = function (_prim, _texture, _vbuffer, _vertexStart) {
54+
this.Dispatch = function (_prim, _texture, _vbuffer, _vertexStart, _vertexCount) {
55+
var size = (_vertexCount === undefined)
56+
? _vbuffer.Current - _vertexStart
57+
: _vertexCount;
5558

56-
var size = _vbuffer.Current - _vertexStart;
5759
switch (_prim)
5860
{
5961
case yyGL.PRIM_TRIANGLE:

scripts/yyBufferVertex.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ var vertex_create_buffer,
3939
vertex_ubyte4,
4040
vertex_freeze,
4141
vertex_submit,
42+
vertex_submit_ext,
4243
vertex_get_number,
4344
vertex_get_buffer_size,
4445
vertex_create_buffer_from_buffer,
4546
vertex_create_buffer_from_buffer_ext,
46-
vertex_update_buffer_from_buffer,
47-
vertex_update_buffer_from_vertex,
47+
vertex_update_buffer_from_buffer,
48+
vertex_update_buffer_from_vertex,
4849
draw_flush;
4950

5051
// @if feature("2d")
@@ -70,12 +71,13 @@ var vertex_create_buffer,
7071
vertex_ubyte4 = _stub("vertex_ubyte4");
7172
vertex_freeze = _stub("vertex_freeze");
7273
vertex_submit = _stub("vertex_submit");
74+
vertex_submit_ext = _stub("vertex_submit_ext");
7375
vertex_get_number = _stub("vertex_get_number");
7476
vertex_get_buffer_size = _stub("vertex_get_buffer_size");
7577
vertex_create_buffer_from_buffer = _stub("vertex_create_buffer_from_buffer", -1);
7678
vertex_create_buffer_from_buffer_ext = _stub("vertex_create_buffer_from_buffer_ext", -1);
77-
vertex_update_buffer_from_buffer = _stub("vertex_update_buffer_from_buffer", -1);
78-
vertex_update_buffer_from_vertex = _stub("vertex_update_buffer_from_vertex", -1);
79+
vertex_update_buffer_from_buffer = _stub("vertex_update_buffer_from_buffer", -1);
80+
vertex_update_buffer_from_vertex = _stub("vertex_update_buffer_from_vertex", -1);
7981
draw_flush = ()=>{};
8082
})();
8183
// @endif
@@ -117,6 +119,7 @@ function InitBufferVertexFunctions() {
117119
vertex_ubyte4 = WebGL_vertex_ubyte4_RELEASE;
118120
vertex_freeze = WebGL_vertex_freeze_RELEASE;
119121
vertex_submit = WebGL_vertex_submit_RELEASE;
122+
vertex_submit_ext = WebGL_vertex_submit_ext_RELEASE;
120123
vertex_get_number = WebGL_vertex_get_number_RELEASE;
121124
vertex_get_buffer_size = WebGL_vertex_get_buffer_size_RELEASE;
122125
draw_flush = WebGL_draw_flush_RELEASE;
@@ -707,6 +710,34 @@ function WebGL_vertex_submit_RELEASE(_buffer, _primType, _texture)
707710
}
708711
}
709712

713+
// #############################################################################################
714+
/// Function:<summary>
715+
/// Draw a portion of a vertex buffer
716+
/// </summary>
717+
/// In: <param name="_buffer">Buffer to get count of</param>
718+
/// In: <param name="_primType">Type of primitive to render with</param>
719+
/// In: <param name="_texture">Texture handle to draw with, or -1 for FLAT</param>
720+
/// In: <param name="_offset">The index of the first vertex.</param>
721+
/// In: <param name="_number">Number of vertices to draw.</param>
722+
// #############################################################################################
723+
function WebGL_vertex_submit_ext_RELEASE(_buffer, _primType, _texture, _offset, _number)
724+
{
725+
g_webGL.Flush();
726+
var prim, vertexBuffer = g_vertexBuffers[yyGetInt32(_buffer)];
727+
if (vertexBuffer) {
728+
729+
if (g_CurrentShader != -1) {
730+
var shader = g_shaderPrograms[g_CurrentShader].program;
731+
var pVertexFormat = vertexBuffer.GetFormat();
732+
if (pVertexFormat.Format.length < shader.AttribIndices.length) {
733+
ErrorOnce("Trying to use a vertex buffer with too few inputs for the seleted shader.");
734+
}
735+
}
736+
737+
vertexBuffer.Submit(WebGL_translate_primitive_builder_type(yyGetInt32(_primType)), _texture, yyGetInt32(_offset), yyGetInt32(_number));
738+
}
739+
}
740+
710741
// #############################################################################################
711742
/// Function:<summary>
712743
/// Get the number of vertices in a vertex buffer

0 commit comments

Comments
 (0)