Skip to content

Commit 83e52a3

Browse files
committed
Updated custom_bitmap logic (WIP, CRASHES)
1 parent 1bb14f9 commit 83e52a3

3 files changed

Lines changed: 79 additions & 125 deletions

File tree

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#include "interface/gui_custom_bitmap_storage.hpp"
22

3+
#include "bitmaps/bitmaps.hpp"
4+
#include "cseries/cseries_events.hpp"
35
#include "interface/user_interface_memory.hpp"
46
#include "memory/module.hpp"
57
#include "multithreading/synchronization.hpp"
6-
#include "networking/online/online_error.hpp"
7-
#include "rasterizer/rasterizer.hpp"
8-
#include "rasterizer/rasterizer_d3d_allocations.hpp"
9-
#include "rasterizer/rasterizer_textures_xenon_formats.hpp"
10-
#include "xbox/xgraphics.hpp"
118

12-
#include <stdio.h>
139

1410
#include <d3d9.h>
1511
#include <d3dx9.h>
@@ -34,48 +30,26 @@ void __thiscall c_gui_custom_bitmap_storage_item::dispose()
3430
ASSERT(!m_bitmap_ready);
3531
ASSERT(!m_hardware_format_bitmap.valid());
3632

37-
if (m_bitmap_pixel_buffer)
33+
if (m_bitmap_pixel_buffer_allocation)
3834
{
39-
user_interface_free(m_bitmap_pixel_buffer);
40-
m_bitmap_pixel_buffer = nullptr;
35+
user_interface_free(m_bitmap_pixel_buffer_allocation);
36+
m_bitmap_pixel_buffer_allocation = NULL;
4137
}
4238

4339
m_allocated = false;
4440
}
4541

46-
const uns32 bitmap_pixel_buffer_alignment_bits = 12;
4742
void __thiscall c_gui_custom_bitmap_storage_item::initialize(int32 width, int32 height, bool use_compressed_format)
4843
{
4944
m_use_compressed_format = use_compressed_format;
5045
e_bitmap_format bitmap_format = m_use_compressed_format ? _bitmap_format_dxt5 : _bitmap_format_a8r8g8b8;
51-
D3DFORMAT d3d_format = m_use_compressed_format ? D3DFMT_DXT5 : D3DFMT_A8R8G8B8;
52-
uns16 flags = FLAG(_bitmap_free_on_delete_bit) | FLAG(_bitmap_use_base_address_for_hardware_format_bit);
46+
D3DFORMAT hardware_format = m_use_compressed_format ? D3DFMT_DXT5 : D3DFMT_A8R8G8B8;
47+
uns16 flags = FLAG(_bitmap_free_on_delete_bit) | FLAG(_bitmap_hardware_only_bit);
5348

54-
bitmap_2d_initialize(&m_bitmap, static_cast<int16>(width), static_cast<int16>(height), 0, bitmap_format, flags, false, false);
55-
m_bitmap.curve = 3;
49+
bitmap_2d_initialize(&m_bitmap_data, (int16)width, (int16)height, 0, bitmap_format, flags, false, false);
50+
m_bitmap_data.curve = 3;
5651

57-
c_rasterizer_texture_ref::allocate(m_hardware_format_bitmap, width, height, 1, d3d_format, D3DPOOL_DEFAULT, false, 0, 0);
58-
texture_header = new D3DBaseTexture();
59-
60-
uns32 base_size = 0;
61-
uns32 mip_size = 0;
62-
uns32 resource_bytes = XGSetTextureHeader(m_bitmap.width, m_bitmap.height, 1, 4, d3d_format, 0, 0, -1, 0, texture_header, &base_size, &mip_size);
63-
if (resource_bytes)
64-
{
65-
int32 allocate_bytes = resource_bytes + FLAG(bitmap_pixel_buffer_alignment_bits);
66-
m_bitmap_pixel_buffer = (char*)user_interface_malloc_tracked(allocate_bytes, __FILE__, __LINE__);
67-
if (m_bitmap_pixel_buffer)
68-
{
69-
m_width = width;
70-
m_height = height;
71-
m_bitmap_pixel_buffer_base = (char*)ALIGN((int32)m_bitmap_pixel_buffer, bitmap_pixel_buffer_alignment_bits);
72-
m_bitmap_pixel_buffer_length = allocate_bytes - (m_bitmap_pixel_buffer_base - m_bitmap_pixel_buffer);
73-
ASSERT(m_bitmap_pixel_buffer_length <= allocate_bytes);
74-
75-
XGOffsetResourceAddress(texture_header, m_bitmap_pixel_buffer_base);
76-
m_allocated = true;
77-
}
78-
}
52+
c_rasterizer_texture_ref::allocate(m_hardware_format_bitmap, width, height, 1, hardware_format, D3DPOOL_DEFAULT, false, 0, 0);
7953
}
8054

8155
bool __thiscall c_gui_custom_bitmap_storage_item::initialize_raw(int32 width, int32 height, char* buffer, int32 buffer_length, bool cpu_cached)
@@ -88,13 +62,15 @@ bool __thiscall c_gui_custom_bitmap_storage_item::load_from_buffer(char const* b
8862
ASSERT(buffer);
8963
ASSERT(!m_bitmap_ready);
9064

91-
bool result = false;
92-
93-
if (!m_allocated)
94-
return false;
65+
//if (!m_allocated)
66+
//{
67+
// return false;
68+
//}
9569

9670
if (!m_hardware_format_bitmap.valid())
71+
{
9772
return false;
73+
}
9874

9975
IDirect3DTexture9* d3d_texture = m_hardware_format_bitmap.get_d3d_texture();
10076
if (d3d_texture == NULL)
@@ -106,22 +82,20 @@ bool __thiscall c_gui_custom_bitmap_storage_item::load_from_buffer(char const* b
10682
IDirect3DSurface9* d3d_surface = NULL;
10783
HRESULT get_surface_level_result = d3d_texture->GetSurfaceLevel(0, &d3d_surface);
10884
if (FAILED(get_surface_level_result))
85+
{
10986
return false;
87+
}
11088

111-
HRESULT load_surface_result = D3DXLoadSurfaceFromFileInMemory(d3d_surface, NULL, NULL, buffer, buffer_length, NULL, D3DX_DEFAULT, 0, NULL);
89+
D3DXIMAGE_INFO d3dximage_info{};
90+
HRESULT load_surface_result = D3DXLoadSurfaceFromFileInMemory(d3d_surface, NULL, NULL, buffer, buffer_length, NULL, D3DX_DEFAULT, 0, &d3dximage_info);
11291
d3d_surface->Release();
113-
92+
11493
if (FAILED(load_surface_result))
11594
{
11695
event(_event_error, "ui:custom_bitmaps: D3DXLoadSurfaceFromFile failed with error code 0x%08X", load_surface_result);
11796
return false;
11897
}
11998

120-
uns32 bytes = XGSetTextureHeader(m_bitmap.width, m_bitmap.height, 1, 4, m_use_compressed_format ? D3DFMT_DXT5 : D3DFMT_A8R8G8B8, 1, 0, -1, 0, texture_header, 0, 0);
121-
ASSERT(bytes > 0);
122-
123-
XGOffsetResourceAddress(texture_header, m_bitmap_pixel_buffer_base);
124-
12599
m_bitmap_ready = true;
126100
return true;
127101
}
@@ -157,35 +131,44 @@ c_gui_custom_bitmap_storage_item const* c_gui_custom_bitmap_storage_manager::get
157131

158132
internal_critical_section_enter(k_crit_section_ui_custom_bitmaps_lock);
159133

160-
s_bitmap_storage_handle_datum* bitmap_storage_handle = DATUM_TRY_AND_GET(m_bitmap_storage_items, s_bitmap_storage_handle_datum, bitmap_storage_index);
161-
if (bitmap_storage_handle && bitmap_storage_handle->reference_count > 0 && bitmap_storage_handle->state == 2)
134+
s_bitmap_storage_handle_datum* bitmap_storage_handle_datum = DATUM_TRY_AND_GET(m_bitmap_storage_items, s_bitmap_storage_handle_datum, bitmap_storage_index);
135+
if (bitmap_storage_handle_datum && bitmap_storage_handle_datum->reference_count > 0 && bitmap_storage_handle_datum->state == _bitmap_storage_state_ready)
162136
{
163-
storage_item = &bitmap_storage_handle->storage_item;
137+
storage_item = &bitmap_storage_handle_datum->storage_item;
164138
}
165139

166140
internal_critical_section_leave(k_crit_section_ui_custom_bitmaps_lock);
167141

168142
return storage_item;
169143
}
170144

171-
bool __cdecl c_gui_custom_bitmap_storage_manager::load_bitmap_from_buffer(int32 storage_item_index, char const* buffer, int32 buffer_size, int32 aspect_ratio)
145+
bool __cdecl c_gui_custom_bitmap_storage_manager::load_bitmap_from_buffer(int32 bitmap_storage_index, char const* buffer, int32 buffer_size, int32 aspect_ratio)
172146
{
173-
s_bitmap_storage_handle_datum* storage_item = NULL;
147+
s_bitmap_storage_handle_datum* bitmap_storage_handle_datum = NULL;
174148
{
175149
c_critical_section_scope section_scope(k_crit_section_ui_custom_bitmaps_lock);
176-
if (storage_item = DATUM_TRY_AND_GET(m_bitmap_storage_items, s_bitmap_storage_handle_datum, storage_item_index))
177-
storage_item->state = 1;
150+
if (bitmap_storage_handle_datum = DATUM_TRY_AND_GET(m_bitmap_storage_items, s_bitmap_storage_handle_datum, bitmap_storage_index))
151+
bitmap_storage_handle_datum->state = _bitmap_storage_state_loading;
178152
}
179153

180-
if (!storage_item)
154+
if (!bitmap_storage_handle_datum)
155+
{
181156
return false;
157+
}
182158

183-
bool result = storage_item->storage_item.load_from_buffer(buffer, buffer_size, m_buffer, m_buffer_size, aspect_ratio);
159+
event(_event_message, "ui:custom_bitmaps: load_bitmap_from_buffer starting %d",
160+
bitmap_storage_index);
161+
162+
bool bitmap_ready = bitmap_storage_handle_datum->storage_item.load_from_buffer(buffer, buffer_size, m_buffer, m_buffer_size, aspect_ratio);
184163

185164
{
186165
c_critical_section_scope section_scope(k_crit_section_ui_custom_bitmaps_lock);
187-
storage_item->state = result ? 2 : 0;
166+
ASSERT(bitmap_storage_handle_datum->state == _bitmap_storage_state_loading);
167+
bitmap_storage_handle_datum->state = bitmap_ready ? _bitmap_storage_state_ready : _bitmap_storage_state_none;
188168
}
189169

190-
return result;
170+
event(_event_message, "ui:custom_bitmaps: load_bitmap_from_buffer ending %d",
171+
bitmap_storage_index);
172+
173+
return bitmap_ready;
191174
}
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
#pragma once
22

33
#include "bitmaps/bitmap_group_tag_definition.hpp"
4-
#include "bitmaps/bitmaps.hpp"
54
#include "cseries/cseries.hpp"
65
#include "memory/data.hpp"
76
#include "rasterizer/rasterizer_text.hpp"
87

9-
struct D3DBaseTexture
10-
{
11-
int16 width;
12-
int16 height;
13-
int8 depth;
14-
int8 mipmap_count_plus_one;
15-
bool high_res_pixels_size_nonzero;
16-
int8 type;
17-
uns32 xenon_d3d_format;
18-
byte __dataC[0x28];
19-
};
20-
static_assert(sizeof(D3DBaseTexture) == 0x34);
21-
228
struct c_gui_custom_bitmap_storage_item
239
{
2410
void __thiscall dispose();
@@ -31,50 +17,62 @@ struct c_gui_custom_bitmap_storage_item
3117

3218
int32 m_width;
3319
int32 m_height;
34-
35-
// true for `dxt5`, false for `a8r8g8b8`
3620
bool m_use_compressed_format;
37-
38-
bitmap_data m_bitmap;
39-
21+
bitmap_data m_bitmap_data;
4022
bool m_allocated;
41-
byte __pad3D[0x3];
42-
23+
byte pad[0x3];
4324
c_rasterizer_texture_ref m_hardware_format_bitmap;
4425
bool m_bitmap_ready;
26+
char* m_bitmap_pixel_buffer_allocation;
4527
char* m_bitmap_pixel_buffer;
46-
47-
// unsure on the name
48-
char* m_bitmap_pixel_buffer_base;
49-
5028
int32 m_bitmap_pixel_buffer_length;
51-
52-
// in `halo3_cache_debug` this isn't a pointer making the struct size 0x90
53-
D3DBaseTexture* texture_header;
54-
55-
byte __data58[0x8];
5629
};
57-
static_assert(sizeof(c_gui_custom_bitmap_storage_item) == 0x60);
30+
static_assert(sizeof(c_gui_custom_bitmap_storage_item) == 0x54);
31+
static_assert(0x00 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_width));
32+
static_assert(0x04 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_height));
33+
static_assert(0x08 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_use_compressed_format));
34+
static_assert(0x0C == OFFSETOF(c_gui_custom_bitmap_storage_item, m_bitmap_data));
35+
static_assert(0x3C == OFFSETOF(c_gui_custom_bitmap_storage_item, m_allocated));
36+
static_assert(0x40 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_hardware_format_bitmap));
37+
static_assert(0x44 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_bitmap_ready));
38+
static_assert(0x48 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_bitmap_pixel_buffer_allocation));
39+
static_assert(0x4C == OFFSETOF(c_gui_custom_bitmap_storage_item, m_bitmap_pixel_buffer));
40+
static_assert(0x50 == OFFSETOF(c_gui_custom_bitmap_storage_item, m_bitmap_pixel_buffer_length));
5841

5942
struct c_gui_custom_bitmap_storage_manager
6043
{
44+
enum
45+
{
46+
k_maximum_number_of_bitmap_items = 32
47+
};
48+
49+
enum e_bitmap_storage_state
50+
{
51+
_bitmap_storage_state_none = 0,
52+
_bitmap_storage_state_loading,
53+
_bitmap_storage_state_ready,
54+
};
55+
6156
struct s_bitmap_storage_handle_datum :
6257
s_datum_header
6358
{
6459
int32 reference_count;
65-
int32 state;
60+
e_bitmap_storage_state state;
6661
c_gui_custom_bitmap_storage_item storage_item;
6762
};
68-
static_assert(sizeof(s_bitmap_storage_handle_datum) == 0x6C);
63+
static_assert(sizeof(s_bitmap_storage_handle_datum) == 0x60);
6964

7065
static c_gui_custom_bitmap_storage_manager* __cdecl get();
7166
c_gui_custom_bitmap_storage_item const* get_bitmap(int32 bitmap_storage_index);
72-
bool __cdecl load_bitmap_from_buffer(int32 storage_item_index, char const* buffer, int32 buffer_size, int32 a5);
67+
bool __cdecl load_bitmap_from_buffer(int32 bitmap_storage_index, char const* buffer, int32 buffer_size, int32 a5);
7368

7469
c_smart_data_array<s_bitmap_storage_handle_datum> m_bitmap_storage_items;
7570
void* m_buffer;
7671
int32 m_buffer_size;
7772
};
7873
static_assert(sizeof(c_gui_custom_bitmap_storage_manager) == 0xC);
74+
static_assert(0x0 == OFFSETOF(c_gui_custom_bitmap_storage_manager, m_bitmap_storage_items));
75+
static_assert(0x4 == OFFSETOF(c_gui_custom_bitmap_storage_manager, m_buffer));
76+
static_assert(0x8 == OFFSETOF(c_gui_custom_bitmap_storage_manager, m_buffer_size));
7977

8078
extern c_gui_custom_bitmap_storage_manager& g_gui_custom_bitmap_storage_manager;

game/source/interface/gui_custom_bitmap_widget.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,25 @@ void __thiscall c_gui_custom_bitmap_widget::set_map_image_(e_custom_map_image_ty
2626
map_image_path.clear();
2727

2828
if (get_map_filename(image_type, map_id, &map_image_path))
29+
{
2930
load_from_file_async(use_compressed_format, map_image_path.get_string());
31+
}
3032
else
33+
{
3134
clear();
35+
}
3236
}
3337

3438
void __cdecl c_gui_custom_bitmap_widget::load_from_file_async(bool use_compressed_format, char const* file_path)
3539
{
36-
m_desired_async_file_to_display.set(tag_name_strip_path(file_path));
40+
m_desired_async_file_to_display.set(file_path);
3741
m_use_compressed_format = use_compressed_format;
3842
m_desired_aspect_ratio = 0;
3943
}
4044

4145
void __thiscall c_gui_custom_bitmap_widget::assemble_render_data(s_gui_bitmap_widget_render_data* render_data, rectangle2d* window_bounds, e_controller_index local_controller_index, bool apply_translation, bool apply_scale, bool apply_rotation)
4246
{
43-
//INVOKE_CLASS_MEMBER(0x00AC37B0, c_gui_custom_bitmap_widget, assemble_render_data_, render_data, window_bounds, local_controller_index, apply_translation, apply_scale, apply_rotation);
44-
45-
//if (s_runtime_bitmap_widget_definition* bitmap_widget_definition = (s_runtime_bitmap_widget_definition*)get_core_definition())
46-
//{
47-
// if (bitmap_widget_definition->name.get_value() == STRING_ID(gui, map_image))
48-
// {
49-
// bitmap_widget_definition->bitmap_reference_index = NONE;
50-
//
51-
// // the base cache has over 17K tags so only check the last 256 tags, this is bad but acceptable for now
52-
// for (int32 i = g_cache_file_globals.tag_loaded_count - 1; i >= g_cache_file_globals.tag_loaded_count - 256; i--)
53-
// {
54-
// int32 tag_index = g_cache_file_globals.absolute_index_tag_mapping[i];
55-
//
56-
// cache_file_tag_instance* instance = g_cache_file_globals.tag_instances[i];
57-
// if (!instance)
58-
// {
59-
// continue;
60-
// }
61-
//
62-
// char const* tag_name = (char const*)offset_pointer(instance->base, instance->total_size);
63-
// if (!m_desired_async_file_to_display.is_equal(tag_name_strip_path(tag_name)))
64-
// {
65-
// continue;
66-
// }
67-
//
68-
// bitmap_widget_definition->bitmap_reference_index = tag_index;
69-
// break;
70-
// }
71-
// }
72-
//
73-
// set_visible(true);
74-
//}
47+
//INVOKE_CLASS_MEMBER(0x00AC37B0, c_gui_custom_bitmap_widget, assemble_render_data, render_data, window_bounds, local_controller_index, apply_translation, apply_scale, apply_rotation);
7548

7649
c_gui_bitmap_widget::assemble_render_data(render_data, window_bounds, local_controller_index, apply_translation, apply_scale, apply_rotation);
7750
render_data->flags.set(s_gui_bitmap_widget_render_data::_render_as_custom_storage_bitmap_bit, true);

0 commit comments

Comments
 (0)