|
14 | 14 | #include "./program.hpp" |
15 | 15 | #include "./texture.hpp" |
16 | 16 | #include "./shader.hpp" |
17 | | -#include "./uniform_location.hpp" |
18 | 17 | #include "./active_info.hpp" |
| 18 | +#include "./uniform_location.hpp" |
19 | 19 |
|
20 | 20 | using namespace std; |
21 | 21 | using namespace node; |
@@ -324,15 +324,8 @@ namespace webgl |
324 | 324 | auto program = Napi::ObjectWrap<WebGLProgram>::Unwrap(info[0].As<Napi::Object>()); |
325 | 325 | int pname = info[1].As<Napi::Number>().Int32Value(); |
326 | 326 |
|
327 | | - try |
328 | | - { |
329 | | - int v = glContext_->getProgramParameter(program->handle(), pname); |
330 | | - return Napi::Number::New(env, v); |
331 | | - } |
332 | | - catch (const std::exception &e) |
333 | | - { |
334 | | - return env.Undefined(); |
335 | | - } |
| 327 | + int v = glContext_->getProgramParameter(program->handle(), pname); |
| 328 | + return Napi::Number::New(env, v); |
336 | 329 | } |
337 | 330 |
|
338 | 331 | template <typename ObjectType, typename ContextType> |
@@ -860,41 +853,70 @@ namespace webgl |
860 | 853 | Napi::TypeError::New(env, "framebufferTexture2D() takes 5 arguments.").ThrowAsJavaScriptException(); |
861 | 854 | return env.Undefined(); |
862 | 855 | } |
863 | | - auto jsTexture = info[3]; |
864 | | - if (!jsTexture.IsObject() || WebGLTexture::IsInstanceOf(jsTexture)) |
| 856 | + |
| 857 | + auto target = info[0].ToNumber().Int32Value(); |
| 858 | + auto attachment = info[1].ToNumber().Int32Value(); |
| 859 | + auto textarget = info[2].ToNumber().Int32Value(); |
| 860 | + auto level = info[4].ToNumber().Int32Value(); |
| 861 | + |
| 862 | + if (target != WEBGL_FRAMEBUFFER && |
| 863 | + target != WEBGL2_DRAW_FRAMEBUFFER && |
| 864 | + target != WEBGL2_READ_FRAMEBUFFER) [[unlikely]] |
865 | 865 | { |
866 | | - glContext_->setError("framebufferTexture2d", client_graphics::WebGLError::kInvalidOperation, "texture isn't 0 or the name of an existing texture object"); |
| 866 | + glContext_->setError("framebufferTexture2d", |
| 867 | + client_graphics::WebGLError::kInvalidEnum, |
| 868 | + "target must be FRAMEBUFFER"); |
867 | 869 | return env.Undefined(); |
868 | 870 | } |
869 | 871 |
|
870 | | - auto target = info[0].As<Napi::Number>().Int32Value(); |
871 | | - auto attachment = info[1].As<Napi::Number>().Int32Value(); |
872 | | - auto textarget = info[2].As<Napi::Number>().Int32Value(); |
873 | | - auto texture = Napi::ObjectWrap<WebGLTexture>::Unwrap(jsTexture.As<Napi::Object>()); |
874 | | - auto level = info[4].As<Napi::Number>().Int32Value(); |
875 | | - |
876 | | - if (target != WEBGL_FRAMEBUFFER) |
| 872 | + Napi::Value jsTexture = info[3]; |
| 873 | + WebGLTexture *textureObject = nullptr; |
| 874 | + if (jsTexture.IsNumber()) |
877 | 875 | { |
878 | | - glContext_->setError("framebufferTexture2d", client_graphics::WebGLError::kInvalidEnum, "target must be FRAMEBUFFER"); |
879 | | - return env.Undefined(); |
| 876 | + // Only the number 0 is allowed to reset the texture binding. |
| 877 | + if (jsTexture.ToNumber().Int32Value() != 0) |
| 878 | + { |
| 879 | + glContext_->setError("framebufferTexture2d", |
| 880 | + client_graphics::WebGLError::kInvalidOperation, |
| 881 | + "texture must be 0 if it is a number"); |
| 882 | + return env.Undefined(); |
| 883 | + } |
| 884 | + else |
| 885 | + { |
| 886 | + textureObject = nullptr; |
| 887 | + } |
| 888 | + } |
| 889 | + else if (jsTexture.IsNull()) // `null` means reset framebuffer's texture |
| 890 | + { |
| 891 | + textureObject = nullptr; |
880 | 892 | } |
881 | | - if (textarget != WEBGL_TEXTURE_2D && |
882 | | - textarget != WEBGL_TEXTURE_CUBE_MAP_POSITIVE_X && |
883 | | - textarget != WEBGL_TEXTURE_CUBE_MAP_NEGATIVE_X && |
884 | | - textarget != WEBGL_TEXTURE_CUBE_MAP_POSITIVE_Y && |
885 | | - textarget != WEBGL_TEXTURE_CUBE_MAP_NEGATIVE_Y && |
886 | | - textarget != WEBGL_TEXTURE_CUBE_MAP_POSITIVE_Z && |
887 | | - textarget != WEBGL_TEXTURE_CUBE_MAP_NEGATIVE_Z) |
| 893 | + else if (jsTexture.IsObject()) |
888 | 894 | { |
889 | | - glContext_->setError("framebufferTexture2d", client_graphics::WebGLError::kInvalidEnum, "textarget must be TEXTURE_2D or one of the TEXTURE_CUBE_MAP_* targets"); |
| 895 | + if (!WebGLTexture::IsInstanceOf(jsTexture)) |
| 896 | + { |
| 897 | + glContext_->setError("framebufferTexture2d", |
| 898 | + client_graphics::WebGLError::kInvalidOperation, |
| 899 | + "texture is not a valid WebGLTexture object."); |
| 900 | + return env.Undefined(); |
| 901 | + } |
| 902 | + else |
| 903 | + { |
| 904 | + textureObject = Napi::ObjectWrap<WebGLTexture>::Unwrap(jsTexture.ToObject()); |
| 905 | + } |
| 906 | + } |
| 907 | + else |
| 908 | + { |
| 909 | + glContext_->setError("framebufferTexture2d", |
| 910 | + client_graphics::WebGLError::kInvalidOperation, |
| 911 | + "texture must be a number, null or a WebGLTexture object"); |
890 | 912 | return env.Undefined(); |
891 | 913 | } |
892 | 914 |
|
893 | 915 | glContext_->framebufferTexture2D( |
894 | 916 | static_cast<client_graphics::WebGLFramebufferBindingTarget>(target), |
895 | 917 | static_cast<client_graphics::WebGLFramebufferAttachment>(attachment), |
896 | 918 | static_cast<client_graphics::WebGLTexture2DTarget>(textarget), |
897 | | - texture->handle(), |
| 919 | + textureObject != nullptr ? textureObject->handle() : nullptr, |
898 | 920 | level); |
899 | 921 | return env.Undefined(); |
900 | 922 | } |
@@ -1455,12 +1477,13 @@ namespace webgl |
1455 | 1477 | Napi::TypeError::New(env, "vertexAttribPointer() takes 6 arguments.").ThrowAsJavaScriptException(); |
1456 | 1478 | return env.Undefined(); |
1457 | 1479 | } |
1458 | | - auto index = info[0].As<Napi::Number>().Uint32Value(); |
1459 | | - auto size = info[1].As<Napi::Number>().Uint32Value(); |
1460 | | - auto type = info[2].As<Napi::Number>().Uint32Value(); |
1461 | | - auto normalized = info[3].As<Napi::Boolean>().Value(); |
1462 | | - auto stride = info[4].As<Napi::Number>().Uint32Value(); |
1463 | | - auto offset = info[5].As<Napi::Number>().Uint32Value(); |
| 1480 | + |
| 1481 | + auto index = info[0].ToNumber().Uint32Value(); |
| 1482 | + auto size = info[1].ToNumber().Uint32Value(); |
| 1483 | + auto type = info[2].ToNumber().Uint32Value(); |
| 1484 | + auto normalized = info[3].ToBoolean().Value(); |
| 1485 | + auto stride = info[4].ToNumber().Uint32Value(); |
| 1486 | + auto offset = info[5].ToNumber().Uint32Value(); |
1464 | 1487 |
|
1465 | 1488 | glContext_->vertexAttribPointer(index, size, type, normalized, stride, offset); |
1466 | 1489 | return env.Undefined(); |
@@ -1545,10 +1568,10 @@ namespace webgl |
1545 | 1568 | auto program = Napi::ObjectWrap<WebGLProgram>::Unwrap(info[0].As<Napi::Object>()); |
1546 | 1569 | std::string name = info[1].As<Napi::String>().Utf8Value(); |
1547 | 1570 | auto loc = glContext_->getAttribLocation(program->handle(), name); |
1548 | | - if (loc.has_value()) |
1549 | | - return Napi::Number::New(env, loc.value()); |
1550 | | - else |
| 1571 | + if (!loc.has_value()) |
1551 | 1572 | return Napi::Number::New(env, -1); |
| 1573 | + else |
| 1574 | + return Napi::Number::New(env, loc.value().index.value_or(-1)); |
1552 | 1575 | } |
1553 | 1576 |
|
1554 | 1577 | template <typename ObjectType, typename ContextType> |
|
0 commit comments