Skip to content

Commit

Permalink
Generic 3D Models (#1289)
Browse files Browse the repository at this point in the history
* Every old model class removed and deleted from git
* A new generic model class was created to replace the deleted ones
* Changed `d3d_model_create` to return an int like other resource functions for consistency
* Removed `d3d_model_part_draw` functions because their name is inconsistent (e.g, should be `draw_part` like `draw_background_part`) and because they are not clear enough for a model class which optimizes and combines primitives together, `d3d_model_draw_primitive` with an index to the primitive would be a better function. We also no longer use these functions anywhere, tiles use the vertex and index buffer abstractions directly now which offer range arguments in their submit functions.
* Added a new function `d3d_model_vertex_data` that allows one to specify the model's vertex data in a single function call, but requires a vertex format be provided.
* Updated `GSmodel.h` with comments specifying the preconditions for each group of model specification functions
* Renamed `d3d_model_add_*` functions to remove the `_add_` part of their name to match the original `d3d_model_vertex` function from GM for consistency.
* Added the following new vertex functions:
    * `vertex_format_exists()`: This non-id version of the function will tell us if we are between `vertex_format_begin` and `vertex_format_end` calls. In other words, it tells us if we are still defining a new vertex format.
    * `vertex_format_get_stride()`: Non-id version returns the stride of the vertex format currently being specified.
    * `vertex_format_get_hash()`: Non-id version returns the hash of the vertex format currently being specified.
    * `vertex_format_get_stride(id)`: Returns the stride of the vertex format with the given id. This is basically the number of vertex attributes, it is not the size of a vertex in bytes.
    * `vertex_format_get_hash(id)`: Returns a hash of the vertex format and its type/usage pairs.
    * `vertex_set_format(buffer,format)`: Allows one to change the vertex format of a vertex buffer, which essentially allows the vertex buffer to hold the same data for differently formatted primitives.
* Made `vertex_format_end` set the `enigma::vertexFormat` pointer to 0 so there can be no further accidental mutation of it once it is moved to the vector and assigned a real id for the user.
* Made `vertex_get_number`, `vertex_get_size`, and their index buffer equivalents return the correct value when in the middle of `vertex_begin` and `vertex_end` which previously they would not. This required marking the vertex and index buffers dirty in `vertex_begin` instead of `vertex_end` so that the dirty flag could be used to determine where to grab the number of vertices/indices from.
* Added a non-fatal error message for `vertex_data` that shows in debug mode if the user calls the function but the vertex buffer does not have a valid vertex format, since one is required for the function to correctly decompose the arguments.
* Removed all `d3d_model_has_*` functions because we aren't using them anywhere and we don't need them.
* Changed `graphics_reset_client_state` to unbind the vertex and index buffers (bind the target to 0) in OpenGL1 because it will screw up primitive rendering that uses `glVertexPointer` with a client-side array. This caused issues in Wild Racing GMK because `d3d_draw_floor` was not being drawn and I could not figure out why. 
* Removed `d3d_model_get_stride` and `d3d_model_format` as well as calls to them in the context managers in Bridges because the function no longer makes sense. The model class in this pr has a format for each primitive, hence the entire model doesn't have a consistent stride or format.
* Removed `d3d_model_index` because the new model class doesn't support indexing yet.
* Added caching of both vertex formats and vertex format peers. The peers are non-trivial to construct and we also don't want the user being wasteful with the former either. Caching both independently gave separate performance boosts, so both are necessary. Because of this change, `vertex_format_end` may now return the same id multiple times and never actually return a unique id if the hashes of the vertex format specified are always logically equivalent to another format.
* Fixed a typo in `d3d_model_set_rotation_x` for D3D9, it should have had `gs_scalar` not `double` like its signature in the general header.
* Added a `dynamic` flag to `vertex_freeze` so the user can indicate such usage. By default I set the parameter to false, because freezing the vertex is to be synonymous with making it static (never changing). If the user passes true for dynamic, that is like saying they will update the vertex buffer infrequently (aka GL_DYNAMIC_DRAW). If the user intends to update the vertex buffer every frame, then they should not call `vertex_freeze` just like in GMS (aka this indicates GL_STREAM_DRAW).
  • Loading branch information
RobertBColton authored and JoshDreamland committed Nov 10, 2018
1 parent ecde227 commit 86bde38
Show file tree
Hide file tree
Showing 25 changed files with 1,033 additions and 5,533 deletions.
9 changes: 1 addition & 8 deletions ENIGMAsystem/SHELL/Bridges/General/DX11Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ extern ID3D11RasterizerState* m_rasterState;
class ContextManager {

float last_depth;
int last_stride;
bool hasdrawn;
int shapes_d3d_model;
int shapes_d3d_texture;
Expand All @@ -74,7 +73,6 @@ ContextManager() {
hasdrawn = false;
shapes_d3d_model = -1;
shapes_d3d_texture = -1;
last_stride = -1;
last_depth = 0.0f;
}

Expand Down Expand Up @@ -113,15 +111,11 @@ int GetShapesModel() {
void BeginShapesBatching(int texId) {
if (shapes_d3d_model == -1) {
shapes_d3d_model = d3d_model_create(true);
last_stride = -1;
} else if (texId != shapes_d3d_texture || (d3d_model_get_stride(shapes_d3d_model) != last_stride && last_stride != -1)) {
last_stride = -1;
} else if (texId != shapes_d3d_texture) {
if (!hasdrawn) {
d3d_model_draw(shapes_d3d_model, shapes_d3d_texture);
d3d_model_clear(shapes_d3d_model);
}
} else {
last_stride = d3d_model_get_stride(shapes_d3d_model);
}
hasdrawn = false;
shapes_d3d_texture = texId;
Expand All @@ -134,7 +128,6 @@ void EndShapesBatching() {
d3d_model_draw(shapes_d3d_model, shapes_d3d_texture);
d3d_model_clear(shapes_d3d_model);
shapes_d3d_texture = -1;
last_stride = -1;
}

};
Expand Down
11 changes: 2 additions & 9 deletions ENIGMAsystem/SHELL/Bridges/General/DX9Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ LPDIRECT3DSURFACE9 pBackBuffer;
LPDIRECT3DSURFACE9 pRenderTarget;

float last_depth;
unsigned last_stride;
bool hasdrawn;
int shapes_d3d_model;
int shapes_d3d_texture;
Expand All @@ -80,7 +79,6 @@ ContextManager() {
hasdrawn = false;
shapes_d3d_model = -1;
shapes_d3d_texture = -1;
last_stride = 0;
vertexShader = NULL;
pixelShader = NULL;
last_depth = 0.0f;
Expand Down Expand Up @@ -187,16 +185,12 @@ int GetShapesModel() {

void BeginShapesBatching(int texId) {
if (shapes_d3d_model == -1) {
shapes_d3d_model = d3d_model_create(model_dynamic);
last_stride = 0;
} else if (texId != shapes_d3d_texture || (d3d_model_get_stride(shapes_d3d_model) != last_stride)) {
last_stride = 0;
shapes_d3d_model = d3d_model_create(model_stream);
} else if (texId != shapes_d3d_texture) {
if (!hasdrawn) {
d3d_model_draw(shapes_d3d_model, shapes_d3d_texture);
d3d_model_clear(shapes_d3d_model);
}
} else {
last_stride = d3d_model_get_stride(shapes_d3d_model);
}
hasdrawn = false;
shapes_d3d_texture = texId;
Expand All @@ -209,7 +203,6 @@ void EndShapesBatching() {
d3d_model_draw(shapes_d3d_model, shapes_d3d_texture);
d3d_model_clear(shapes_d3d_model);
shapes_d3d_texture = -1;
last_stride = 0;
}

void Clear(DWORD Count, const D3DRECT *pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
Expand Down
15 changes: 4 additions & 11 deletions ENIGMAsystem/SHELL/Bridges/General/GL3Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ GLuint bound_tex;
public:

float last_depth;
int last_stride;
bool hasdrawn;
int shapes_d3d_model;
int shapes_d3d_texture;
Expand All @@ -47,7 +46,6 @@ ContextManager() {
hasdrawn = false;
shapes_d3d_model = -1;
shapes_d3d_texture = -1;
last_stride = -1;
last_depth = 0.0f;
bound_tex = 0;
}
Expand Down Expand Up @@ -87,15 +85,11 @@ void RestoreState() {
void BeginShapesBatching(int texId) {
if (shapes_d3d_model == -1) {
shapes_d3d_model = enigma_user::d3d_model_create(enigma_user::model_stream);
last_stride = -1;
} else if (texId != shapes_d3d_texture || (enigma_user::d3d_model_get_stride(shapes_d3d_model) != last_stride && last_stride != -1)) {
last_stride = -1;
} else if (texId != shapes_d3d_texture) {
if (!hasdrawn) {
enigma_user::d3d_model_draw(shapes_d3d_model, shapes_d3d_texture);
enigma_user::d3d_model_clear(shapes_d3d_model);
}
} else {
last_stride = enigma_user::d3d_model_get_stride(shapes_d3d_model);
}
hasdrawn = false;
shapes_d3d_texture = texId;
Expand All @@ -108,8 +102,7 @@ void EndShapesBatching() {
enigma_user::d3d_model_clear(shapes_d3d_model);

shapes_d3d_texture = -1;
last_stride = -1;
last_depth -= 1;
last_depth -= 1;
}

void BeginScene() {
Expand Down Expand Up @@ -180,9 +173,9 @@ void BlendFunc() { //Used when calling blend functions

void ColorFunc() { //Used when calling color functions
if (shapes_d3d_model != -1){
if (enigma_user::d3d_model_has_color(shapes_d3d_model) == false){
//if (enigma_user::d3d_model_has_color(shapes_d3d_model) == false){
EndShapesBatching();
}
//}
}
}

Expand Down
Loading

0 comments on commit 86bde38

Please sign in to comment.